简体   繁体   中英

Drawing a common horizontal line for multiple subplots

I want that all the three subplots have a common horizontal line keeping the distance between the subplots unchanged. Basically I just want to fill the spaces between three horizontal lines to create one large horizontal line. How can this be done? Here is the figure在此处输入图像描述

Here is the code

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()

Here is a solution without using multiple plots. The trick is to use a custom x-values and width.

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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM