Python 熊猫散点图日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27472548/
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
pandas scatter plotting datetime
提问by jma
I have a dataframe with two columns of datetime.time's. I'd like to scatter plot them. I'd also like the axes to display the times, ideally. But
我有一个包含两列 datetime.time 的数据框。我想散点图它们。理想情况下,我还希望轴显示时间。但
df.plot(kind='scatter', x='T1', y='T2')
dumps a bunch of internal plot errors ending with a KeyError on 'T1'.
转储一堆以“T1”上的 KeyError 结尾的内部绘图错误。
Alternatively, I try
或者,我尝试
plt.plot_date(x=df.loc[:,'T1'], y=df.loc[:,'T2'])
plt.show()
and I get 'Exception in Tkinter callback' with a long stack crawl ending in
我得到“Tkinter 回调中的异常”,并以长堆栈爬网结束
return _from_ordinalf(x, tz)
File "/usr/lib/python3/dist-packages/matplotlib/dates.py", line 224, in _from_ordinalf
microsecond, tzinfo=UTC).astimezone(tz)
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'str'
Any pointers?
任何指针?
回答by Aaron Schumacher
Not an answer, but I can't edit the question or put this much in a comment, I think.
不是答案,但我认为我无法编辑问题或在评论中写这么多。
Here is a reproducible example:
这是一个可重现的示例:
from datetime import datetime
import pandas as pd
df = pd.DataFrame({'x': [datetime.now() for _ in range(10)], 'y': range(10)})
df.plot(x='x', y='y', kind='scatter')
This gives KeyError: 'x'.
这给KeyError: 'x'.
Interestingly, you doget a plot with just df.plot(x='x', y='y'); it chooses poorly for the default x range because the times are just nanoseconds apart, which is weird, but that's a separate issue. It seems like if you can make a line graph, you should be able to make a scatterplot too.
有趣的是,你确实得到了一个情节df.plot(x='x', y='y'); 它对默认 x 范围的选择很差,因为时间间隔只有纳秒,这很奇怪,但这是一个单独的问题。看起来如果你能制作一个折线图,你也应该能够制作一个散点图。
There is a pandas github issueabout this problem, but it was closed for some reason. I'm going to go comment there and see if we can re-start that conversation.
有一个关于这个问题的 pandas github问题,但由于某种原因被关闭了。我要去那里发表评论,看看我们是否可以重新开始对话。
Is there some clever work-around for this? If so, what?
是否有一些聪明的解决方法?如果是这样,是什么?
回答by Aaron Schumacher
Not a real answer but a workaround, as suggested by Tom Augspurger, is that you can just use the working line plot type and specify dots instead of lines:
正如 Tom Augspurger 所建议的,这不是一个真正的答案,而是一种解决方法,即您可以只使用工作线图类型并指定点而不是线:
df.plot(x='x', y='y', style=".")
回答by J Wang
Here's a basic work around to get you started.
这是帮助您入门的基本工作。
import matplotlib, datetime
import matplotlib.pyplot as plt
def scatter_date(df, x, y, datetimeformat):
if not isinstance(y, list):
y = [y]
for yi in y:
plt.plot_date(df[x].apply(
lambda z: matplotlib.dates.date2num(
datetime.datetime.strptime(z, datetimeformat))), df[yi], label=yi)
plt.legend()
plt.xlabel(x)
# Example Usage
scatter_date(data, x='date', y=['col1', 'col2'], datetimeformat='%Y-%m-%d')
回答by Mike N
It's not pretty, but as a quick hack you can convert your DateTime to a timestamp using .timestamp()before loading into Pandas and scatters will work just fine (although a completely unusable x-axis).
这并不漂亮,但作为一个快速的技巧,您可以.timestamp()在加载到 Pandas 之前将 DateTime 转换为时间戳,并且分散将工作得很好(尽管完全无法使用 x 轴)。
回答by dvmlls
building on Mike N's answer...convert to unix time to scatter properly, then transform your axis labels back from int64s to strings:
以 Mike N 的回答为基础...转换为 unix 时间以正确分散,然后将轴标签从 int64s 转换回字符串:
type(df.ts1[0])
pandas.tslib.Timestamp
pandas.tslib.Timestamp
df['t1'] = df.ts1.astype(np.int64)
df['t2'] = df.ts2.astype(np.int64)
fig, ax = plt.subplots(figsize=(10,6))
df.plot(x='t1', y='t2', kind='scatter', ax=ax)
ax.set_xticklabels([datetime.fromtimestamp(ts / 1e9).strftime('%H:%M:%S') for ts in ax.get_xticks()])
ax.set_yticklabels([datetime.fromtimestamp(ts / 1e9).strftime('%H:%M:%S') for ts in ax.get_yticks()])
plt.show()


