Matplotlib 是 Python 的繪圖庫。 它可與 NumPy 一塊兒使用,提供了一種有效的 MatLab 開源替代方案。函數
import numpy as np import pandas as pd import matplotlib.pyplot as plt from pandas import Series,DataFrame #內嵌畫圖 %matplotlib inline #繪圖時能夠顯示中文 plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False
設置圖像標題:plt.title()
設置x軸名稱:plt.xlabel()
設置y軸名稱:plt.ylabel()
設置x軸範圍:plt.xlim()
設置y軸範圍:plt.ylim()
設置x軸刻度:plt.xticks()
設置y軸刻度:plt.yticks()
設置曲線圖例:plt.legend()
畫布:figure fig = plt.figure() 圖:subplot ax1 = fig.add_subplot(2,2,1) 調節子圖間距: subplots_adjust(left, bottom, right, top, wspace, hspace) plt.figure(figsize=(6,8)) 表示figure 的大小爲寬、長(單位爲inch) plt.subplot(121) 表示整個figure分紅1行2列,共2個子圖,這裏子圖在第一行第一列 plt.subplot(122) 表示子圖在第一行第二列
圖一spa
plot函數:繪製折線圖 線型linestyle(-,-.,--,..) 點型marker(v,^,s,*,H,+,x,D,o,…) 顏色color(b,g,r,y,k,w,…) plt.plot([1,2,3,4],[2,3,1,8],color="red",linestyle="--",marker="*" ,label='lineA') # 畫折線() plt.plot([1,2,3,4],[2,4,6,9],color="blue",linestyle="-",marker="o" ,label='lineB') plt.title('matplotlib Test') plt.xlabel('XLabel') plt.ylabel('YLabel') plt.xlim(0,6) # x軸刻度範圍 plt.ylim(0,10) # y軸刻度範圍 plt.xticks(np.arange(0,7,2)) plt.legend()
線條方式3d
點型code
圖二blog
fig = plt.figure() ax1 = fig.add_subplot(1,2,1) #參數一:子圖總行數,子圖總列數,子圖位置 ax1.plot([1,2,3,4],[7,9,10,8],label='ax1') ax2 = fig.add_subplot(1,2,2) ax2.plot([1,2,3,4],[2,4,6,8],label='ax2') ax1.legend() ax2.legend() plt.show()
圖三ip
x=np.linspace(-3,3,50) #在(-3,3)之間生成50個樣本數 y1=2*x+1 y2=x**2 plt.figure(num=1,figsize=(10,5)) #定義編號爲1,大小爲(10,5) plt.plot(x,y1,color='red',linewidth=2,linestyle='--') #顏色線寬及格式 plt.plot(x,y2) plt.xlim(-1,2) #x軸的範圍 plt.ylim(-2,3) #y軸的範圍 plt.xlabel("x軸") plt.ylabel("y軸") plt.show()
移動座標軸unicode
x=np.linspace(-3,3,50) #在(-3,3)之間生成50個樣本數 y1=2*x+1 y2=x**2 plt.figure(num=2,figsize=(8,5)) #定義編號爲2,大小爲(8,5) plt.plot(x,y1,color='red',linewidth=2,linestyle='--') plt.plot(x,y2) plt.xlim(-1,2) #x軸的範圍 plt.ylim(-2,3) #y軸的範圍 plt.xlabel("x軸") plt.ylabel("y軸") new_ticks=np.linspace(-1,2,5) #-1到2分紅5段,包含端點 print(new_ticks) plt.xticks(new_ticks) #進行替換新下標 plt.yticks([-2,-1,0,1,2,], [r'$really\ bad$','$bad$','$0$','$well$','$really\ well$']) ax=plt.gca() #get current axis ax.spines['right'].set_color('none') #邊框屬性設置爲None 不顯示 ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') #設置x座標刻度數字或名稱的位置,全部屬性爲:top,bottom,both,default,none ax.spines['bottom'].set_position(('data',0)) # 設置.spines邊框x軸,設置.set_position設置邊框的位置,y=0位置;位置全部屬性有outward,axes,data ax.yaxis.set_ticks_position('left') ax.spines['left'].set_position(('data',0)) #座標中心點在(0,0)位置
圖例變化get
x=np.linspace(-3,3,50) y1=2*x+1 y2=x**2 plt.figure(num=2,figsize=(8,5)) plt.xlim(-1,2) plt.ylim(-2,3) new_ticks=np.linspace(-1,2,5) plt.xticks(new_ticks) plt.yticks([-2,-1,1,2,], [r'$really\ bad$','$bad$','$well$','$really\ well$']) l1,=plt.plot(x,y1,color='red',linewidth=2,linestyle='--',label='linear line') l2,=plt.plot(x,y2,label='square line') #單獨修改label的信息 plt.legend(loc='upper left',handles=[l1,l2],labels=['up','down']) #顯示在左上位置 plt.show() #顯示圖 #loc參數 best upper right upper left lower left lower right right center right lower center upper center center
data=[20,30,40,50] labels=['Jan','Feb','Mar','Apr'] plt.bar(np.arange(len(data)),data,width=.3) plt.xticks(np.arange(len(data)),labels) plt.show()
import numpy as np import matplotlib.pyplot as plt # 數據 N = 5 x = [20, 10, 30, 25, 15] y = np.arange(N) # 繪圖 y= y軸, left= 水平條的底部, height 水平條的寬度, width 水平條的長度 p1 = plt.barh(y, left=0, height=0.5, width=x) # 展現圖形 plt.show()
N = 5 y = [20, 10, 30, 45, 15] x = np.arange(N) # 添加地名座標 str1 = ("北京", "上海", "武漢", "深圳", "重慶") plt.figure(num=2,figsize=(10,5)) #定義編號爲2,大小爲(8,5) # 繪圖 x x軸, height 高度, 默認:color="blue", width=0.8 p1 = plt.bar(x, height=y, width=0.5, label="城市指標", tick_label=str1) # 添加數據標籤 for a, b in zip(x, y): plt.text(a, b + 0.05, '%.0f' % b, ha='center', va='bottom', fontsize=11) # 添加圖例 plt.legend() # 展現圖形 plt.show()
plt.pie([10,20,30],labels=['x','y','z'],autopct='%.2f%%',explode=[0.1,0,0.1]) plt.axis('equal') plt.show()