我正在制作一个简单的水平堆积条形图:
df = pd.DataFrame({'firstBox':firstL,'secondBox':secondL,'thirdBox':thirdL})
ax = df.plot.barh(stacked=True)
ax.set_title("My Example Plot")
ax.set_yticklabels(labels=['A label this long is cut off','this label is also cut off])
plt.show()
但是我的ytick标签上的值已被截断。如果增加返回的绘图窗口的宽度,则可以看到更多,但是我需要将窗口拉伸到显示器的宽度之外,才能看到整个标签。有没有一种方法可以将图右移以包括我的长标签?
谢谢!
您应该尝试此:
from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
如果使用的是savefig,则bbox_inches='tight'
是一个选项,它会自动尝试找出图形的紧实bbox。
嗨,我最近也遇到了这个问题。这是一些示例代码,它将扩展绘图区域以包括所有轴刻度。注意,扩大绘图区域的位是bbox_inches='tight'
import numpy as np
import pandas as pd
names = ['Name 1','Name 2','Name 3','Name 4','Name 5','Name 6']
y1 = []
y2 = []
for name in names:
y1.append(np.random.rand())
y2.append(np.random.rand())
df = pd.DataFrame({'First ': y1, 'Second ': y2}, index=names)
ax = df.plot.bar(rot=0)
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_title("Sample Plot")
ax.set_xticklabels(ax.get_xticklabels(), rotation=30, ha="right")
fig = ax.get_figure()
fig.savefig("Fig_Name", bbox_inches='tight')
您可以使用ax.get_figure().tight_layout()
或plt.gcf().tight_layout()
。这将修复图形布局。