Python中使用matplotlib庫(kù)繪制各種圖
1.繪制折線圖(pyplot.plot(x,y))
from matplotlib import pyplot x = range(2,28,2) y = [15,13,14,17,20,25,26,26,24,22,18,15,1] //設(shè)置圖片大小 fig = pyplot.figure(figsize=(5,5),dpi=80) //繪圖 pyplot.plot(x,y) //保存圖片 pyplot.savefig("./sig_size.png") //展示圖片 pyplot.show()
繪制兩條折線
# import matplotlib from matplotlib import pyplot x = range(11,31) y1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1] y2 = [1,0,3,1,2,2,3,3,2,1,2,1,1,1,1,1,1,1,1,2] #set the pic size pyplot.figure(figsize=(10,5),dpi=80) #ploting pyplot.plot(x,y1,label="Luna",color="pink",linestyle='--',linewidth=1) pyplot.plot(x,y2,label="Jim",color="tomato",linestyle='--',linewidth=1) # set x/y-axis step _xtick_labels = ["{}".format(i) for i in x] pyplot.xticks(x,_xtick_labels) pyplot.yticks(range(0,7)) # set x-asix desciption pyplot.xlabel("Age") # set y-asix description pyplot.ylabel("Num") # add legend pyplot.legend() # plot grid pyplot.grid(alpha=0.2,linestyle=':') # show pyplot.show()
2.繪制散點(diǎn)圖(pyplot.scatter(x,y))
from matplotlib import pyplot from matplotlib import font_manager y_3 = [11,12,12,13,14,11,12,17,20,25,22,27,21,29,23,22,24,12,11,21,10,12,11,13,34,34,23,22,24,12,11] y_10 = [34,34,33,33,34,32,31,31,30,32,21,23,23,13,22,21,24,25,23,23,11,12,12,13,34,34,23,22,24,12,11] x_3 = range(1,32) x_10 = range(41,72) pyplot.figure(figsize=(20,10),dpi=80) pyplot.scatter(x_3,y_3,color="blue",label="March") pyplot.scatter(x_10,y_10,color="cyan",label="Noverber") _x = list(x_3)+list(x_10) _xticks_label = ["March{}".format(i) for i in x_3] _xticks_label += ["Noverber{}".format(i-40) for i in x_10] pyplot.xticks(_x[::10],_xticks_label[::10]) pyplot.xlabel("date") pyplot.ylabel("temperature") pyplot.legend() pyplot.show()
3.繪制條形圖(pyplot.bar())
from matplotlib import pyplot from matplotlib import font_manager my_font = font_manager.FontProperties(fname="") a = ["Friends","My Heart go on","kongfu"] b = [57.1,25,12] pyplot.figure(figsize=(4,6),dpi=80) pyplot.bar(a,b,width=0.3) pyplot.xlabel("movie") pyplot.ylabel("score") pyplot.xticks(range(len(a)),a,rotation=15) pyplot.show()
pyplot.barh()=>繪制橫著的條形圖
from matplotlib import pyplot from matplotlib import font_manager my_font = font_manager.FontProperties(fname="") a = ["Friends","My Heart go on","kongfu"] b = [57.1,25,12] pyplot.figure(figsize=(8,6),dpi=80) pyplot.barh(a,b,height=0.3) pyplot.ylabel("movie") pyplot.xlabel("score") pyplot.yticks(range(len(a)),a) pyplot.grid(alpha=0.2) pyplot.show()
繪制對(duì)比條形圖(繪制三次)
from matplotlib import pyplot a = ["movie1","movie2","movie3","movie4","movie5"] b_1 = [3124,123,5431,3411,2344] b_2 = [3456,2123,1455,8764,2323] b_3 = [213,431,124,56,120] bar_width=0.2 x_1 = list(range(len(a))) x_2 = [i+bar_width for i in x_1] x_3 = [i+bar_width*2 for i in x_1] pyplot.figure(figsize=(10,4),dpi=80) pyplot.bar(range(len(a)),b_1,width=bar_width,color="blue",label="1") pyplot.bar(x_2,b_2,width=bar_width,color="pink",label="2") pyplot.bar(x_3,b_3,width=bar_width,label="3") pyplot.xticks(x_2,a) pyplot.legend() pyplot.show()
4.繪制直方圖(pyplot.hist())
頻數(shù)分布直方圖pyplot.hist(a,num_bins)
from matplotlib import pyplot a = [9,34,13,73,44,34,76,34,72,17,96,46,84,52,72,26,81,64,79,45,99] d = 10 num_bins = (max(a)-min(a))//d pyplot.figure(figsize=(10,6),dpi=80) pyplot.hist(a,num_bins) pyplot.xlabel("time") pyplot.ylabel("num") pyplot.xticks(range(min(a),max(a)+d,d)) pyplot.grid() pyplot.show()
頻率分布直方圖pyplot.hist(a,num_bins,density=True)
from matplotlib import pyplot a = [9,34,13,73,44,34,76,34,72,17,96,46,84,52,72,26,81,64,79,45,99] d = 10 num_bins = (max(a)-min(a))//d pyplot.figure(figsize=(10,6),dpi=80) pyplot.hist(a,num_bins,density=True) pyplot.xlabel("time") pyplot.ylabel("percentage") pyplot.xticks(range(min(a),max(a)+d,d)) pyplot.grid() pyplot.show()
繪制組距變化的直方圖(pyplot.bar())
這里的組距為一個(gè)數(shù)組
from matplotlib import pyplot interval = [0,5,10,15,20,25,30,35,40,45,60,90] width = [5,5,5,5,5,5,5,5,5,15,30,60] quality = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47] pyplot.bar(range(12),quality,width=1) _x = [i-0.5 for i in range(13)] _xtick_label = interval+[150] pyplot.xticks(_x,_xtick_label) pyplot.grid() pyplot.show()
總結(jié)
1.如何選擇哪種圖來(lái)呈現(xiàn)數(shù)據(jù)?
2.matplotlib.plot(x,y)
繪制的是折線圖,x為代表x軸的list,y為代表y軸的值的list,這里的x和y的元素個(gè)數(shù)必須是一致的
3.matplotlib.bar(x,height,width)
繪制的是條形圖,x為代表x軸的list,height為對(duì)應(yīng)的x的值,width為條形圖的寬度
4.matplotlib.barh(y,width,height)
繪制橫著的條形圖,x和y的含義相反
5.matplotlib.scatter(x,y)
繪制散點(diǎn)圖
6.matplotlib.hist(x,bin,density)
繪制直方圖,這里的x為源數(shù)據(jù)的數(shù)組,bin為分多少組顯示,這里的圖的y值代表在某個(gè)范圍內(nèi)的頻率或頻數(shù),通過(guò)參數(shù)density可以繪制頻數(shù)直方圖或頻率直方圖,默認(rèn)為頻數(shù)直方圖一般設(shè)置一個(gè)組距dbin = (max(a)-min(x))//d
7.xticks和yticks的設(shè)置
設(shè)置x軸和y軸的坐標(biāo)
8.label和title,grid的設(shè)置
9.繪圖的大小(figure)和保存圖片(savefig)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python使用Matplotlib庫(kù)創(chuàng)建3D 圖形和交互式圖形詳解
- 使用Python的matplotlib庫(kù)繪制柱狀圖
- 使用Python的數(shù)據(jù)可視化庫(kù)Matplotlib實(shí)現(xiàn)折線圖
- Python中如何使用Matplotlib庫(kù)繪制圖形
- Python連接數(shù)據(jù)庫(kù)使用matplotlib畫柱形圖
- python matplotlib庫(kù)的基本使用
- Python三維繪圖之Matplotlib庫(kù)的使用方法
- Python如何使用內(nèi)置庫(kù)matplotlib繪制折線圖
- python使用matplotlib庫(kù)生成隨機(jī)漫步圖
- Python Matplotlib 庫(kù)使用指南
相關(guān)文章
Python實(shí)現(xiàn)復(fù)雜的事件驅(qū)動(dòng)架構(gòu)
事件驅(qū)動(dòng)架構(gòu)(Event-Driven?Architecture,?EDA)是一種軟件設(shè)計(jì)模式,它基于事件的產(chǎn)生、傳播和處理進(jìn)行系統(tǒng)的構(gòu)建,下面我們來(lái)看看如何在?Python?中實(shí)現(xiàn)復(fù)雜的事件驅(qū)動(dòng)架構(gòu)吧2024-12-12利用Python快速搭建Markdown筆記發(fā)布系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了使用Python生態(tài)的成熟工具,在30分鐘內(nèi)搭建一個(gè)支持Markdown渲染、分類標(biāo)簽、全文搜索的私有化知識(shí)發(fā)布系統(tǒng),感興趣的小伙伴可以參考下2025-04-04python中yield的用法詳解——最簡(jiǎn)單,最清晰的解釋
這篇文章主要介紹了python中yield的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04簡(jiǎn)述Python中的進(jìn)程、線程、協(xié)程
這篇文章主要介紹了Python中的進(jìn)程、線程、協(xié)程的相關(guān)資料,需要的朋友可以參考下2016-03-03Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將序列分解為單獨(dú)變量的方法
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將序列分解為單獨(dú)變量的方法,結(jié)合實(shí)例形式分析了Python序列賦值實(shí)現(xiàn)的分解成單獨(dú)變量功能相關(guān)操作技巧,需要的朋友可以參考下2018-02-02python+flask實(shí)現(xiàn)API的方法
這篇文章主要為大家詳細(xì)介紹了python+flask實(shí)現(xiàn)API的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11python使用win32com在百度空間插入html元素示例
這篇文章主要介紹了python使用win32com在百度空間插入html元素的示例,大家參考使用吧2014-02-02