matplotlib在 pandas DataFrame中绘制日期时间 [英] matplotlib plot datetime in pandas DataFrame
问题描述
我有一个像这样的熊猫数据框training.head()
I have a pandas DataFrame that looks like this training.head()
DataFrame已按日期排序.我想作一个散点图,其中广告活动的日期在x轴上,成功率在y轴上.我可以通过使用training.plot(x='date',y='rate')
来获得折线图.但是,当我将其更改为training.plot(kind='scatter',x='date',y='rate')
时,出现错误:KeyError:u'no item named date'
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'
为什么我尝试创建散点图时索引列会消失?另外,我敢打赌我需要对该日期字段进行一些操作,以免将它当作简单的字符串来对待,对吗?
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?
推荐答案
如果我没记错的话,绘图代码仅考虑数字列.在内部它仅选择数字列,所以这就是为什么会出现键错误的原因.
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.
date
的dtype是什么?如果是datetime64
,则可以将其重铸为np.int64
:
What's the dtype of date
? If it's a datetime64
, you can recast it as an np.int64
:
df['date_int'] = df.date.astype(np.int64)
然后您进行绘图.
对于彩色部分,制作一个{account number: color}
字典.例如:
For the color part, make a dictionary of {account number: color}
. For example:
color_d = {1: 'k', 2: 'b', 3: 'r'}
然后在绘制时:
training.plot(kind='scatter',x='date',y='rate', color=df.account.map(color_d))
这篇关于matplotlib在 pandas DataFrame中绘制日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!