Pandas和Matplotlib - 如何调整绘图区域以适应更多x轴文本?
假设我把这个csv文件读入了一个pandas的数据框:
文件内容:
Strings, Values, Letters Made up data, 55.0, A with long text strings for index, 125.5, B with long text strings for index, 85.5, B how does one, 1.3, A how does one, 12.3, A change the plot area to, 96.1, B fit all of this text in?, 0.2, B fit all of this text in?, 47.2, B
代码:
c=pd.read_csv('myfile.csv')
得到的结果:
Strings Values Letters 0 Made up data 55.0 A 1 with long text strings for index 125.5 B 2 with long text strings for index 85.5 B 3 how does one 1.3 A 4 how does one 12.3 A 5 change the plot area to 96.1 B 6 fit all of this text in? 0.2 B 7 fit all of this text in? 47.2 B
然后我用 groupby
来对字符串进行求和:
g=c.groupby(u'Strings').sum()
得到的结果:
Values Strings Made up data 55.0 change the plot area to 96.1 fit all of this text in? 47.4 how does one 13.6 with long text strings for index 211.0
最后我这样绘图:
g.plot(kind='bar')
得到的效果:
你可以看到,x轴的文字被裁剪得很糟糕。我该怎么解决这个问题呢?
注意,我尝试过使用 rot
参数(例如 rot=-45
),但这并不够。
我想解决方案可能涉及设置绘图区域,但我不太确定该怎么做。
1 个回答
1
你试过用 plt.tight_layout() 吗?