Laura
Laura

Reputation: 1282

How to reduce the space between barh plot while maintaining the width

I cannot find the proper parameter to modify in Matplotlib documentation.

I have this:

df.plot(kind='barh',x='Attributes',y='Counts', ax=ax1, color='#C0C0C0', width=0.3,legend= False)

which produces an horizontal bar plot.

I want to mantain the width = 0.3 but reduce the space between the bars.

Upvotes: 2

Views: 1185

Answers (1)

Ruthger Righart
Ruthger Righart

Reputation: 4921

It can be tweaked in the following way:

Modules & Example data:

import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'x':['A','B','C'], 'y':[4,6,3]})

Determine position:

xn = [i for i, _ in enumerate(df['x'])]

This results in xn = [0, 1, 2].

Plot:

plt.barh(xn, df['y'], height=0.5)
plt.yticks(xn, df['x'])

The first parameter, in this case xn, is the one that should be adapted to change the spacing between the bars. For ex.: if you set xn=[0, 0.8, 1.6], the space will reduce.

Upvotes: 1

Related Questions