Pandas数据框绘制

2024-05-13 23:58:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有熊猫数据框

enter image description here

给我这个:

enter image description here

我该怎么办

  1. 创造一个新的形象
  2. 在图“title Here”中添加标题
  3. 以某种方式创建一个映射,使标签不是29、30等,而是说“第29周”、“第30周”等
  4. 将图表的较大版本保存到我的计算机(例如10 x 10英寸)

我已经为此困惑了一个小时了!


Tags: 数据版本标题heretitle计算机方式图表
2条回答

您可以使用^{}DataFrame方法:

In [1]: df = pd.DataFrame(np.random.randn(7, 5),
                          index=['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
                          columns=[29, 30, 31, 32, 33])

In [2]: df
Out[2]: 
           29        30        31        32        33
Mon -0.080946 -0.072797 -1.019406  1.149162  2.727502
Tue  1.041598 -0.730701 -0.079450  1.323332 -0.823343
Wed  0.338998  1.034372 -0.273139  0.457153  0.007429
Thu -2.239857 -0.439499  0.675963  0.966994  1.348100
Fri  0.050717 -0.506382  1.269897 -0.862577  1.205110
Sat -1.380323  0.200088 -0.685536 -0.425614  0.148111
Sun -0.248540 -1.056943  1.550433  0.651707 -0.041801

In [3]: df.rename(columns=lambda x: 'Week ' + str(x), inplace=True)

In [5]: df
Out[5]: 
      Week 29   Week 30   Week 31   Week 32   Week 33
Mon -0.080946 -0.072797 -1.019406  1.149162  2.727502
Tue  1.041598 -0.730701 -0.079450  1.323332 -0.823343
Wed  0.338998  1.034372 -0.273139  0.457153  0.007429
Thu -2.239857 -0.439499  0.675963  0.966994  1.348100
Fri  0.050717 -0.506382  1.269897 -0.862577  1.205110
Sat -1.380323  0.200088 -0.685536 -0.425614  0.148111
Sun -0.248540 -1.056943  1.550433  0.651707 -0.041801

然后,可以使用标题绘制此图:

In [4]: df.plot(title='Title Here')

请参阅visualisation section of the docs中的更多信息。

注意:to save the figure you can use savefig

import matplotlib.pyplot as plt
# 1, 4
f = plt.figure(figsize=(10, 10)) # Change the size as necessary
# 2
dataframe.plot(ax=f.gca()) # figure.gca means "get current axis"
plt.title('Title here!', color='black')
# 3
# Not sure :(

相关问题 更多 >