一、為什么選擇matplotlib
- python的畫圖庫(kù),用python實(shí)現(xiàn),減少人力勞動(dòng);
-
功能強(qiáng)大,姿勢(shì)多;
可以這樣畫,可以這樣畫,可以360度畫;
matplot1.png
二、開(kāi)始吧,理解幾個(gè)概念
figure,axis
figure.png
figure 相當(dāng)于一個(gè)畫板。畫板上面可以畫一個(gè)圖,也可以畫N個(gè)圖。
axis 就是這個(gè)圖。一個(gè)figure上有一個(gè)或多個(gè)axis。
pyplot
matplotlib.pyplot 大部分命令都是它來(lái)執(zhí)行。如畫各種圖形,顯示,保存等。
三、來(lái)個(gè)簡(jiǎn)單的例子
matplotlib畫圖的幾個(gè)步驟:
步驟一:獲取數(shù)據(jù)
一般包括橫坐標(biāo)和縱坐標(biāo)的數(shù)據(jù)。
步驟二:基本畫出圖形
步驟三:設(shè)置細(xì)節(jié)
如標(biāo)題了,數(shù)字刻度了,坐標(biāo)軸顯示,圖例等細(xì)節(jié)。
如圖,畫一個(gè)下面這樣的圖形
pyplot.png
代碼示例:
#coding:utf8
import matplotlib.pyplot as plt
import numpy as np
x_data = range(0,10)
y1 = [173,827,259,891,540,490,530,913,518,636]
y2 = [860,196,767,710,798,985,330,570,248,498]
#步驟一:獲取數(shù)據(jù)
fig = plt.figure()
ax = fig.add_subplot(111)
#創(chuàng)建圖像
ax.plot(x_data,y1,label="y1")
ax.plot(x_data,y2,label="y2")
#步驟二:畫出圖形
plt.legend()
#步驟三:設(shè)置細(xì)節(jié):顯示圖例等。
plt.savefig("product.png",transparent=True,format='png')
#保存圖片
plt.show()
#顯示圖片
四、參考資料
- matplotlib 官網(wǎng)
-
莫煩python-matplotlib視頻
帶你輕松入門的視頻教程,推薦下。 -
matplotlib教程PPT-黃春林
從整體上清晰理解下。