Python Pandas 数据帧绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18237453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:16:26 来源:igfitidea点击:
Pandas Data Frame Plotting
提问by Ross Middleton
I have this Pandas DataFrame
我有这个 Pandas DataFrame
which gives me this:
这给了我这个:
How do I
我如何能
- Make a new figure,
- Add the title to the figure "Title Here"
- Somehow create a mapping so that instead of the labels being 29,30 etc, they say "week 29", "Week 30"etc.
- Save a larger version of the chart to my computer (say 10 x 10 inches)
- 创造一个新形象,
- 给图“Title Here”添加标题
- 以某种方式创建一个映射,以便标签不是 29,30 等,而是说“第 29 周”、“第 30 周”等。
- 将更大版本的图表保存到我的电脑(比如 10 x 10 英寸)
I have been puzzling over this for an hour now!
我已经困惑了一个小时了!
采纳答案by Andy Hayden
You can use the rename
DataFrame method:
您可以使用rename
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
You can then plot this with a title:
然后你可以用标题来绘制它:
In [4]: df.plot(title='Title Here')
See more in the visualisation section of the docs.
回答by Chris Barker
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 :(