Python matplotlib 在 Pandas DataFrame 中绘制日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21961360/
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-18 23:57:55  来源:igfitidea点击:

matplotlib plot datetime in pandas DataFrame

pythonmatplotlibpandas

提问by Kevin Thompson

I have a pandas DataFrame that looks like this training.head()

我有一个看起来像这样的 Pandas DataFrame training.head()

enter image description here

在此处输入图片说明

The DataFrame has been sorted by date. I'd like to make a scatterplot where the date of the campaign is on the x axis and the rate of success is on the y axis. I was able to get a line graph by using training.plot(x='date',y='rate'). However, when I changed that to training.plot(kind='scatter',x='date',y='rate')I get an error: KeyError: u'no item named date'

DataFrame 已按日期排序。我想制作一个散点图,其中活动日期在 x 轴上,成功率在 y 轴上。我能够通过使用training.plot(x='date',y='rate'). 但是,当我将其更改training.plot(kind='scatter',x='date',y='rate')为错误时:KeyError: u'no item named date'

Why does my index column go away when I try to make a scatterplot? Also, I bet I need to do something with that date field so that it doesn't get treated like a simple string, don't I?

当我尝试制作散点图时,为什么我的索引列消失了?另外,我敢打赌我需要对那个日期字段做一些事情,这样它就不会被当作一个简单的字符串来对待,不是吗?

Extra credit, what would I do if I wanted each of the account numbers to plot with a different color?

额外的功劳,如果我希望每个帐号都用不同的颜色绘制,我该怎么办?

采纳答案by TomAugspurger

If I remember correctly, the plotting code only considers numeric columns. Internally it selects just the numeric columns, so that's why you get the key error.

如果我没记错的话,绘图代码只考虑数字列。在内部,它只选择数字列,所以这就是你得到关键错误的原因。

What's the dtype of date? If it's a datetime64, you can recast it as an np.int64:

的 dtype 是date什么?如果是datetime64,则可以将其重铸为np.int64

df['date_int'] = df.date.astype(np.int64)

And then you're plot.

然后你是阴谋。

For the color part, make a dictionary of {account number: color}. For example:

对于颜色部分,制作一个字典{account number: color}。例如:

color_d = {1: 'k', 2: 'b', 3: 'r'}

Then when you plot:

然后当你绘图时:

training.plot(kind='scatter',x='date',y='rate', color=df.account.map(color_d))

回答by MarkNS

I've found it simpler to change the styleof a line chart to not include the connecting lines:

我发现将style折线图的更改为不包括连接线更简单:

cb_df.plot(figsize=(16, 6), style='o')

cb_df.plot(figsize=(16, 6), style='o')

enter image description here

在此处输入图片说明

回答by Samira Khodai

The plotting code only considers numeric columns, so the piece of code bellow will give you error:

绘图代码只考虑数字列,所以下面的代码片段会给你错误:

df['Date'] = pd.to_datetime(df.Date) 

try pd.to_numericas below and finnaly use scatter plot. It worked for me!

尝试pd.to_numeric如下并最终使用scatter plot. 它对我有用!

df['Date'] = pd.to_numeric(df.Date)