Axes容器:
Axes容器是用來創建具體的圖形的,比如畫曲線,柱狀圖,都是畫在上面,所以之前我們學的使用plt.xx繪制各種圖形(比如條形圖,直方圖,散點圖等)都是對Axes的封裝,比如plt.plot對應的是axes.plot,比如plt.hist對應的是axes.hist,針對圖的所有操作,都可以在Axes上找到對應的API,另外后面要講到的Axis容器,是軸的物件,也是系結在Axes上面,
Axes的類定義介紹:https://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes
1. 設定x和y軸的最大值和最小值:
設定完刻度后,我們還可以設定x軸和y軸的最大值和最小值,可以通過set_xlim/set_ylim來實作:
fig = plt.figure() axes = fig.add_subplot(111) axes.plot(np.random.randn(10)) # 設定x軸的最大值和最小值 axes.set_xlim(-2,12) # 設定y軸的最大值和最小值 axes.set_ylim(-3,3)
2. 添加文本:
之前添加文本我們用的是annotate,但是如果不是需要做注釋,其實還有另外一種更加簡單的方式,那就是使用text方法:
data = https://www.cnblogs.com/qshhl/p/np.random.randn(10) fig = plt.figure() axes = fig.add_subplot(111) axes.plot(data) # 添加文本,比annotate更加方便 axes.text(0,0,"hello")
3. 繪制雙Y軸:
fig = plt.figure() ax1 = fig.add_subplot(211) ax1.bar(np.arange(0,10,2),np.random.rand(5)) ax1.set_yticks(np.arange(0,1,0.25)) ax2 = ax1.twinx() #克隆一個共享x軸的axes物件 ax2.plot(np.random.randn(10),c="b") plt.show()
效果圖如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/278352.html
標籤:Python
