【问题标题】:Pandas and Matplotlib - how can I change plot area to fit in more x-axis text?Pandas 和 Matplotlib - 如何更改绘图区域以适应更多 x 轴文本?
【发布时间】:2014-04-12 14:01:31
【问题描述】:

假设我将这个 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),但这还不够。

我认为解决方案可能涉及设置绘图区域,但我不确定如何执行此操作。

【问题讨论】:

    标签: python numpy matplotlib pandas graphing


    【解决方案1】:

    你试过了吗:plt.tight_layout()

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 2022-12-17
      • 2020-12-15
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      相关资源
      最近更新 更多