前言:
matplotlib是python 2D最常用的繪圖庫(kù)晌姚,可以生成各種硬拷貝格式和跨平臺(tái)交互式環(huán)境的出版物質(zhì)量數(shù)據(jù)。Matplotlib可用于Python腳本抵恋,Python和IPython shell宝磨,Jupyter筆記本,Web應(yīng)用程序服務(wù)器和四個(gè)圖形用戶界面工具包世囊。
下面來介紹matplotlib的具體操作:
-
導(dǎo)入
包含了中文顯示窿祥,屏外顯示
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
%matplotlib tk
#解決中文不顯示問題壁肋,防止中文亂碼
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
-
畫出第一個(gè)圖形
figure圖形,畫的每個(gè)圖只有一個(gè)figure對(duì)象
x= np.arange(-3,3,0.1)
y1=np.sin(x)
#創(chuàng)建第一個(gè)figure
plt.figure()
#繪圖
plt.plot(x,y1)
#表現(xiàn)出來
plt.show()
正弦圖.png
有多條線
x= np.arange(-3,3,0.1)
y1=np.sin(x)
y2=np.cos(x)
plt.figure(num=3,figsize=(8,5))
plt.plot(x,y1,x,y2)
plt.show()
image.png
-
顏色,標(biāo)記跛锌,線型
主要是plt.plot的一些參數(shù)
plt.figure(num=3,figsize=(8,5))
plt.plot([1,2,3],[5,7,4],color="red",linestyle="dashed",marker="o",markersize="10",alpha=0.8)
plt.show()
# plt.plot([1,2,3],[5,7,4],'ro--')#為簡(jiǎn)寫方式
image.png
-
刻度菠赚、標(biāo)題、標(biāo)簽和圖例!
內(nèi)容代碼中已經(jīng)注釋
x1=[1,2,3]
y1=[5,7,4]
x2=[1,2,3]
y2=[10,14,12]
plt.figure(num=3,figsize=(8,5))
plt.plot(x1,y1,'ro-',label="進(jìn)口")
plt.plot(x2,y2,'bo--',label="出口")#label設(shè)置線條標(biāo)簽
#設(shè)置標(biāo)題衡查,x拌牲,y軸標(biāo)簽
plt.xlabel('月份')
plt.ylabel("數(shù)額")
plt.title("進(jìn)出口數(shù)據(jù)")
#設(shè)置x歌粥,y軸范圍
plt.xlim(0,6)
plt.ylim(0,15)
# #設(shè)置x,y軸刻度
# plt.xticks(np.arange(0,6,1))
#按照上面設(shè)置不美觀土居,推薦用np.linspace
plt.xticks(np.linspace(1,6,6),[str(i)+"月" for i in range(1,7)])
plt.yticks(np.arange(1,15,3),[200,300,400,500,600])
#設(shè)置邊框,先獲得坐標(biāo)軸信息plt.gca()
ax=plt.gca()
ax.spines['top'].set_color('red')
# ax.spines['right'].set_color('none')
#生成默認(rèn)圖例
plt.legend()
plt.show()
image.png
-
創(chuàng)建子圖
在一個(gè)figure中顯示多個(gè)圖片
- 面向過程的方法,一步一步創(chuàng)建
x1=[1,2,3]
y1=[5,7,4]
x2=[1,2,3]
y2=[10,14,12]
plt.figure()
plt.subplot(221)#第一個(gè)子圖
plt.plot(x1,y1,'ro--')
plt.subplot(223)
plt.plot(x2,y2,'bo-')#第二個(gè)子圖
plt.show
- 面向?qū)ο髣?chuàng)建子圖
#創(chuàng)建圖形
fig=plt.figure()
#創(chuàng)建子圖
ax1=fig.add_subplot(221)
ax2=fig.add_subplot(222)
ax3=fig.add_subplot(212)
#m和n代表在一個(gè)圖像窗口中顯示m行n列個(gè)圖像迄损,也就是整個(gè)figure中有n個(gè)圖是排成一行的账磺,一共m行,后面的p
代表現(xiàn)在選定第p個(gè)圖像區(qū)域氏捞,即在第p個(gè)區(qū)域作圖冒版。如果m=2就是表示2行圖。p表示圖所在的位置捆等,p=1表示從左到右
從上到下的第一個(gè)位置续室。
#在子圖上畫圖
ax1.plot(np.random.randn(50).cumsum(),'r-')
ax2.plot(np.random.randn(50).cumsum(),'b-')
ax3.plot(np.random.randn(50).cumsum(),'g--')
plt.show
image.png
- subplots創(chuàng)建多個(gè)子圖
fig,axes=plt.subplots(nrows=4,ncols=1,sharex=True)
axes[0].plot(range(10),'ro--')
axes[1].plot(range(10),'bo--')
axes[2].plot(range(10),'yo--')
axes[3].plot(range(10),'go--')
image.png
- 例子
fig,axes=plt.subplots(2,2,sharex=True,sharey=True)
for i in range(2):
for j in range(2):
axes[i][j].hist(np.random.randn(100),5,color='g',alpha=0.75)
#調(diào)整子圖之間的距離
fig.subplots_adjust(wspace=0.2,hspace=0.3)
fig.suptitle("text",fontsize=20)#設(shè)置標(biāo)題和格式
#保存
# plt.savefig("aaa",dpi=200)
plt.show()
image.png
后記:
線圖先到這明郭,還有柱狀圖丰泊,散點(diǎn)圖,3d圖等待續(xù)……
你可能感冒的文章:
我的機(jī)器學(xué)習(xí)numpy篇
我的機(jī)器學(xué)習(xí)pandas篇
我的機(jī)器學(xué)習(xí)微積分篇