1

I have already spent quite some time searching for a solution online. I have created a stacked horizontal barplot with Matplotlib. I would like to increase the space between the bars but don't know how to do that.

import pandas as pd
import matplotlib.pyplot as plt


d = {
'id': [823,234,213,343,329,289,569,294,295,832],
'dogs': [1,2,3,4,3,5,2,3,4,5], 
'cats': [3,4,5,2,1,3,4,5,3,2,], 
'birds': [1,4,2,3,4,2,3,1,4,2]}

df = pd.DataFrame(data=d)

df.plot(
    x = 'id',
    kind = 'barh',
    stacked = True,
    title = 'Stacked Bar Graph',
    mark_right = True, 
    color=['#D3E0F9','#7AA3F1','#3D68CF'],
    width=0.6)


plt.rc('axes', titlesize=6) #fontsize of the title
plt.rc('axes', labelsize=10) #fontsize of the x and y labels
plt.rc('xtick', labelsize=10) #fontsize of the x tick labels
plt.rc('ytick', labelsize=10) #fontsize of the y tick labels
plt.rc('legend', fontsize=10) #fontsize of the legend

plot

Alex
  • 6,610
  • 3
  • 20
  • 38
Mario W
  • 17
  • 2
  • Have you tried increasing the width/height of your figure? – Alex May 15 '21 at 15:09
  • I did. But I want to stick to the current width. However changing height doesn't work I always get the error "barh() got multiple values for argument 'height'" – Mario W May 15 '21 at 15:11

1 Answers1

0

Setup your figure size ahead of plotting:

fig, ax = plt.subplots(figsize=(6, 10))
df.plot(
    x="id",
    kind="barh",
    stacked=True,
    title="Stacked Bar Graph",
    mark_right=True,
    color=["#D3E0F9", "#7AA3F1", "#3D68CF"],
    width=0.6,
    # Then plot onto the ax for the `fig`
    ax=ax,
)

enter image description here

Alex
  • 6,610
  • 3
  • 20
  • 38
  • That's not the correct way to set the figure size when using [`pandas.DataFrame.plot`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html). `df.plot(..., figsize=(w, h))`. – Trenton McKinney May 15 '21 at 16:49