0

Take a simple example which displays the bar chart with axes

import pandas as pd
import matplotlib.pyplot as plt
    
s = pd.Series([1, 2, 3])
fig, ax = plt.subplots(tight_layout=True)
plot = s.plot.bar()

enter image description here

However when save the chart using...

fig.savefig('ex.png', format='png', bbox_inches='tight')

...the saved chart has the axes cut off. If I try plot.get_figure() the result is the same.

enter image description here

How can I get the axes produced by pandas.plot into the figure so I can then save the chart to file?

user3188040
  • 671
  • 9
  • 24
  • I'm not sure if this matters, but the more standard way to use an existing figure is to change `plot=s.plot.bar()` to `s.plot.bar(ax=ax)` – tdy Mar 04 '22 at 04:23
  • But I can't reproduce this either way. What are your `pd.__version__` and `matplotlib.__version__`? – tdy Mar 04 '22 at 04:24
  • `pd.__version__ = 1.2.3` `matplotlib.__version__ = 3.3.4` `s.plot.bar(ax=ax)` does not make a difference for me – user3188040 Mar 04 '22 at 04:55
  • I don't know. I still can't reproduce the issue even with those versions. Must be some other factor causing it. This is my `ex.png` output when I copy-paste your code on pd 1.2.3 and mpl 3.3.4: https://i.stack.imgur.com/pC5rq.png – tdy Mar 04 '22 at 07:20
  • Can't reproduce it either. Have you tried the following: https://stackoverflow.com/questions/16032389/pad-inches-0-and-bbox-inches-tight-makes-the-plot-smaller-than-declared-figsiz – braml1 Mar 04 '22 at 08:30
  • Hmmm the issue seems to only appear when the code is run inside a JupyterHub notebook. I just tried running it in normal python and it works fine – user3188040 Mar 04 '22 at 09:28

0 Answers0