0

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 enter image description here

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()
Navdeep
  • 823
  • 1
  • 16
  • 35

1 Answers1

1

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)

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Just a simple query, what if I want to add legend corresponding to each color. How can this be done? – Navdeep Oct 30 '19 at 15:05
  • @Navdeep : Check [this](https://stackoverflow.com/questions/18974928/how-to-create-custom-legend-in-matplotlib-based-on-the-value-of-the-barplot) answer and search for other similar posts using "custom legends in bar chart matplotlib" – Sheldore Oct 30 '19 at 21:45