简体   繁体   English

熊猫和Matplotlib-如何更改绘图区域以适合更多X轴文本?

[英]Pandas and Matplotlib - how can I change plot area to fit in more x-axis text?

Say I read in this csv file into a pandas dataframe: 假设我已将此csv文件读入pandas数据框:

File contents: 文件内容:

 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 

Code: 码:

c=pd.read_csv('myfile.csv')

Gives: 得到:

  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 

I then use groupby to sum the strings: 然后,我使用groupby对字符串求和:

g=c.groupby(u'Strings').sum()

Giving: 赠送:

  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 

And finally plot like so: 最后像这样绘制:

g.plot(kind='bar')

Giving: 赠送:

在此处输入图片说明

As you can see the x axis text is badly cropped. 如您所见,x轴文本被严重裁剪。 How can I fix this? 我怎样才能解决这个问题?

Note that I have tried using the rot parameter (eg rot=-45 ) but this is insufficient. 请注意,我尝试使用rot参数(例如rot=-45 ),但这还不够。

I imagine that a solution might involve setting the plot area but I'm unsure how to do this. 我认为解决方案可能涉及设置绘图区域,但是我不确定如何执行此操作。

您是否尝试过:plt.tight_layout()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM