![](/img/trans.png)
[英]matplotlib: drawing a horizontal line spanning over multiple subplots, with interactive mode
[英]Drawing a common horizontal line for multiple subplots
我希望所有三個子圖都有一條共同的水平線,保持子圖之間的距離不變。 基本上我只想填充三條水平線之間的空間來創建一條大水平線。 如何才能做到這一點? 這是圖
這是代碼
import numpy as np
import matplotlib.pyplot as plt
x_axis=['HE','BBHE','BHEPL','DOTHE','RSIHE','QDHE','RSWHE']
y_drive=[0.480,0.478,0.638,0.508,0.475,0.587,0.846]
y_stare=[0.759,0.766,0.856,0.659,0.777,0.767,0.865]
y_chase=[0.482,0.515,0.809,0.763,0.520,0.802,0.802]
x=np.array(np.arange(0,1.30,0.2))
x=np.arange(0,7)
width=1
fig,(ax1,ax2,ax3)=plt.subplots(1,3,sharey=True,sharex=True) #returns subplot axes or object
ax1.bar(x,y_drive,width,color=['r','g','b','c','m','y','k'],alpha=0.80)
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.tick_params(axis='both',which='both',bottom=False,left=False,labelbottom=False)
ax1.set_xlabel('DRIVE')
ax1.set_ylim(0,1)
ax2.bar(x,y_stare,width,color=['r','g','b','c','m','y','k'],alpha=0.80)
ax2.spines['top'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.spines['right'].set_visible(False)
ax2.tick_params(axis='both',which='both',bottom=False,left=False,labelbottom=False)
ax2.set_xlabel('STARE')
ax3.bar(x,y_chase,width,color=['r','g','b','c','m','y','k'],alpha=0.80)
ax3.spines['top'].set_visible(False)
ax3.spines['left'].set_visible(False)
ax3.spines['right'].set_visible(False)
ax3.tick_params(axis='both',which='both',bottom=False,left=False,labelbottom=False)
ax3.set_xlabel('CHASE')
plt.show()
這是一個不使用多個圖的解決方案。 訣竅是使用自定義 x 值和寬度。
fig, ax1 = plt.subplots() #returns subplot axes or object
x=np.arange(-0.75, 0.8, 0.25)
width=1/4.
ax1.bar(x,y_drive,width,color=['r','g','b','c','m','y','k'],alpha=0.80)
ax1.bar(x+2,y_stare,width,color=['r','g','b','c','m','y','k'],alpha=0.80)
ax1.bar(x+4,y_chase,width,color=['r','g','b','c','m','y','k'],alpha=0.80)
ax1.set_xticks(range(0, 6, 2))
ax1.set_xticklabels(['DRIVE', 'STARE', 'CHASE'])
ax1.tick_params(axis='both',which='both',bottom=False,left=False,labelbottom=True)
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.set_ylim(0,1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.