Stop x-axis labels from shrinking the plot in Matp

2019-08-26 01:08发布

I'm trying to make a bar graph with the following code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

test = {'names':['a','b','abcdefghijklmnopqrstuvwxyz123456789012345678901234567890'], 'values':[1,2,3]}
df = pd.DataFrame(test)

plt.rcParams['figure.autolayout'] = False
ax = sns.barplot(x='names', y='values', data=df)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
plt.show()

But I get the following error because the long value in 'names' as a label on the x-axis is making the image shrink until the bottom is above the top.

Traceback (most recent call last):
  File "C:/Users/Adam/.PyCharm2018.2/config/scratches/scratch.py", line 11, in <module>
    plt.show()
  File "C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 253, in show
    return _show(*args, **kw)
  File "C:\Program Files\JetBrains\PyCharm 2018.2.3\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 25, in __call__
    manager.show(**kwargs)
  File "C:\Program Files\JetBrains\PyCharm 2018.2.3\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 107, in show
    self.canvas.show()
  File "C:\Program Files\JetBrains\PyCharm 2018.2.3\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 62, in show
    self.figure.tight_layout()
  File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 2276, in tight_layout
    self.subplots_adjust(**kwargs)
  File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 2088, in subplots_adjust
    self.subplotpars.update(*args, **kwargs)
  File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 245, in update
    raise ValueError('bottom cannot be >= top')
ValueError: bottom cannot be >= top

Here is what it looks like if I reduce the length of that name slightly: enter image description here

How can I get it to expand the figure to fit the label instead of shrinking the axes?

2条回答
smile是对你的礼貌
2楼-- · 2019-08-26 01:56

If you want this in an interactive backend I didn't find any other way than manually adjust the figure size. This is what I get using the qt5agg backend:

ax = sns.barplot(x='names', y='values', data=df)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
ax.figure.set_size_inches(5, 8)  # manually adjust figure size
plt.tight_layout()  # automatically adjust elements inside the figure
plt.show()

enter image description here

Note that pycharm's scientific mode might be doing some magic that prevents this to work so you might need to deactivate it or just run the script outside pycharm.

查看更多
The star\"
3楼-- · 2019-08-26 02:12

One workaround is to create the Axes instance yourself as axes, not as subplot. Then tight_layout() has no effect, even if it's called internally. You can then pass the Axes with the ax keyword to sns.barplot. The problem now is that if you call plt.show() the label may be cut off, but if you call savefig with bbox_inches='tight', the figure size will be extended to contain both the figure and all labels:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])

test = {'names':['a','b','abcdefghijklmnopqrstuvwxyz123456789012345678901234567890'], 'values':[1,2,3]}
df = pd.DataFrame(test)

#plt.rcParams['figure.autolayout'] = False
ax = sns.barplot(x='names', y='values', data=df, ax=ax)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
#plt.show()
fig.savefig('long_label.png', bbox_inches='tight')

PROCLAIMER: I don't have pycharm, so there goes the assumption in this code, that matplotlib behaves the same with and without pycharm. Anyway, for me the outcome looks like this:

result of above code

查看更多
登录 后发表回答