我正在制作一个简单的水平堆叠条形图:
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标签上的值被切断了。如果我增加返回的绘图窗口的宽度,我可以看到更多的标签,但我需要将窗口伸展到显示器的宽度之外才能看到整个标签。有没有一种方法可以将绘图向右推,以包括我的长标签?
谢谢!
发布于 2017-06-22 00:11:34
你应该试试这个:
from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
发布于 2017-11-03 21:17:36
如果您使用的是savefig,bbox_inches='tight'
是一个自动尝试找出图形的紧bbox的选项。
发布于 2019-02-19 19:29:44
嗨,我最近也遇到了这个问题。下面是一些示例代码,它将扩展绘图区域以包括所有轴标记。请注意,用于扩展绘图区域的位是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')
https://stackoverflow.com/questions/44688019
复制相似问题