3

I'm currently working on a pandas plot. It is my desire to move the legend of the plot to the right of my plot, unfortunately the solution I have found seems to crop the legend, rather than resizing the plot to fit the width of the legend.

My Code:

data = [
        ['Sted 1',9,101,30,50,23,30],
        ['Sted 2',-13,83,31,44,36,31],
        ['Sted 3',17,93,22,42,23,35],
        ['Sted 4',-6,89,24,41,34,29]        
        ]
headers = [
    'Sted',
    'Køb af grise og andre indægter',
    'Foderomkostninger',
    'Dyrlæge, medicin og diverse',
    'Arbejdsomkostninger',
    'Energi, vedl. bygn. mv.',
    'Forrentning driftskapital og øvrige omk.'
    ]
df = pd.DataFrame(data, columns=headers)
df = df.set_index('Sted')
df.plot(kind='bar', stacked=True, rot=0, figsize=(10,6), legend=False, zorder=3)
plt.grid(zorder=0)
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))

plt.show()

Resulting Plot enter image description here

How would I go about resizing the plot to fit the legend with. (the overall figsize is fixed)

DavidG
  • 24,279
  • 14
  • 89
  • 82
Henrik Poulsen
  • 935
  • 2
  • 13
  • 32

0 Answers0