matplotlib.subplot()畫子圖并共享y坐標軸的方法
有時候想要把幾張圖放在一起plot,比較好對比,subplot和subplots都可以實現(xiàn),具體對比可以查看參考博文。這里用matplotlib庫的subplot來舉個栗子。
數(shù)據(jù)長什么樣
有兩個數(shù)據(jù)段,第一個數(shù)據(jù)是DataFrame類型,第二個是ndarray類型。每個數(shù)據(jù)都有3列,我想畫1*3的折線子圖,第一個數(shù)據(jù)的第n列和第二個數(shù)據(jù)的第n列畫在一張子圖上。先來看一下兩個數(shù)據(jù)長什么樣兒(為顯示方便,只看前5行)。
In [1]: testing_set.head() # DataFrame類型 Out [1]: Open High Low 0 778.81 789.63 775.80 1 788.36 791.34 783.16 2 786.08 794.48 785.02 3 795.26 807.90 792.20 4 806.40 809.97 802.83 # ndarray類型 In [2]: predicted_stock_price #這里就只看前5行 Out [2]: [[790.6911 796.39215 779.3191 ] [790.24524 796.0866 778.9673 ] [789.5737 795.52606 778.3751 ] [790.1047 796.10864 778.92395] [790.8686 796.94104 779.7281 ]]
實現(xiàn)過程
注:plt.setp()是用來共享y坐標軸
# 創(chuàng)建畫布
fig = plt.figure(figsize = (30,10), dpi = 80)
# 子圖1
ax1 = plt.subplot(131)
ax1.set_title('Open Price')
ax1.plot(testing_set.values[:,0], color = 'red', label = 'Real Open Price')
ax1.plot(predicted_stock_price[:,0], color = 'blue', label = 'Predicted Open Price')
plt.setp(ax1.get_xticklabels(), fontsize=6)
ax1.legend()
# 子圖2
ax2 = plt.subplot(132,sharey=ax1)
ax2.set_title('High Price')
ax2.plot(testing_set.values[:,1], color = 'red', label = 'Real High Price')
ax2.plot(predicted_stock_price[:,1], color = 'blue', label = 'Predicted High Price')
ax2.legend()
# 子圖3
ax3 = plt.subplot(133,sharey=ax1)
ax3.set_title('Low Price')
ax3.plot(testing_set.values[:,2], color = 'red', label = 'Real Low Price')
ax3.plot(predicted_stock_price[:,2], color = 'blue', label = 'Predicted Low Price')
ax3.legend()
plt.show()
結果:

到此這篇關于matplotlib.subplot()畫子圖并共享y坐標軸的方法的文章就介紹到這了,更多相關matplotlib.subplot()子圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python使用seaborn繪圖直方圖displot,密度圖,散點圖
這篇文章主要介紹了python使用seaborn繪圖直方圖displot,密度圖,散點圖,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07
Python如何操作office實現(xiàn)自動化及win32com.client的運用
這篇文章主要介紹了Python如何操作office實現(xiàn)自動化及win32com.client的運用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
Python中threading模塊的Lock和RLock區(qū)別詳解
這篇文章主要介紹了Python中threading模塊的Lock和RLock區(qū)別詳解,Lock鎖是Python的原始鎖,在鎖定時不屬于任何一個線程,在調用了 lock.acquire() 方法后,進入鎖定狀態(tài),lock.release()方法可以解鎖,底層是通過一個函數(shù)來實現(xiàn)的,需要的朋友可以參考下2023-09-09
Python中使用matplotlib模塊errorbar函數(shù)繪制誤差棒圖實例代碼
在matplotlib中,errorbar方法用于繪制帶誤差線的折線圖,下面這篇文章主要給大家介紹了關于Python中使用matplotlib模塊errorbar函數(shù)繪制誤差棒圖的相關資料,需要的朋友可以參考下2022-08-08

