pandas 如何在 Matplotlib 中更改时间轴限制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/18773638/
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
How to change axis limits for time in Matplotlib?
提问by marillion
I have a data set stored in a Pandas dataframe object, and the first column of the dataframe is a datetimetype, which looks like this:
我有一个存储在 Pandas 数据框对象中的数据集,数据框的第一列是一个datetime类型,如下所示:
0    2013-09-09 10:35:42.640000
1    2013-09-09 10:35:42.660000
2    2013-09-09 10:35:42.680000
3    2013-09-09 10:35:42.700000
In another column, I have another column called eventno, and that one looks like:
在另一列中,我有另一列名为eventno,它看起来像:
0     0
1     0
2     0
3     0
I am trying to create a scatter plot with Matplotlib, and once I have the scatter plot ready, I would like to change the range in the date axis (x-axis) to focus on certain times in the data. My problem is, I could not find a way to change the range the data will be plotted over in the x axis. I tried this below, but I get a Not implemented for this typeerror.
我正在尝试使用 Matplotlib 创建散点图,一旦我准备好散点图,我想更改日期轴(x 轴)中的范围以专注于数据中的特定时间。我的问题是,我找不到改变数据将在 x 轴上绘制的范围的方法。我在下面试过这个,但我收到一个Not implemented for this type错误。
plt.figure(figsize=(13,7), dpi=200)
ax.set_xlim(['2013-09-09 10:35:00','2013-09-09 10:36:00'])
scatter(df2['datetime'][df.eventno<11],df2['eventno'][df.eventno<11])
If I comment out the ax.set.xlimline, I get the scatter plot, however with some default x axis range, not even matching my dates.
如果我注释掉这ax.set.xlim条线,我会得到散点图,但是有一些默认的 x 轴范围,甚至不匹配我的日期。
Do I have to tell matplotlib that my data is of datetime type, as well? If so, then how can I do it? Assuming this part is somehow accomplished, then how can I change the range of my data to be plotted?
我是否必须告诉 matplotlib 我的数据也是日期时间类型?如果是这样,那我该怎么做?假设这部分以某种方式完成,那么如何更改要绘制的数据范围?
Thanks!
谢谢!
PS: I tried uploading the picture, but I got a "Framing not allowed" error. Oh well... It just plots it from Jan 22 1970 to Jan 27 1970. No clue how it comes up with that :)
PS:我尝试上传图片,但出现“不允许取景”错误。哦,好吧......它只是从 1970 年 1 月 22 日到 1970 年 1 月 27 日绘制它。不知道它是如何得出的:)
回答by user206300
Try putting ax.set_xlimafter the scattercommand. 
试着把命令放在ax.set_xlim后面scatter。

