![](/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.