使用 Pandas 和日期时间格式绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52266076/
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
Plotting using Pandas and datetime format
提问by MathPhiz
I have a dataframe with just two columns, Date, and ClosingPrice. I am trying to plot them using df.plot() but keep getting this error:
我有一个只有两列、Date 和 ClosingPrice 的数据框。我正在尝试使用 df.plot() 绘制它们,但不断收到此错误:
ValueError: view limit minimum -36785.37852 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units
ValueError: view limit minimum -36785.37852 小于 1 并且是无效的 Matplotlib 日期值。如果您将非日期时间值传递给具有日期时间单位的轴,则通常会发生这种情况
I have found documentation about this from matplotlib but that says how to make sure that the format is datetime. Here is code that I have to make sure the format is datetime and also printing the data type for each column before attempting to plot.
我从 matplotlib 中找到了关于此的文档,但它说明了如何确保格式为日期时间。这是我必须确保格式为日期时间并在尝试绘图之前打印每列的数据类型的代码。
df.Date = pd.to_datetime(df.Date)
print(df['ClosingPrice'].dtypes)
print(df['Date'].dtypes)
The output for these print statements are:
这些打印语句的输出是:
float64 datetime64[ns]
float64 datetime64[ns]
I am not sure what the problem is since I am verifying the data type before plotting. Here is also what the first few rows of the data set look like:
我不确定问题是什么,因为我在绘图之前验证了数据类型。这也是数据集的前几行的样子:
Date ClosingPrice
0 2013-09-10 64.7010
1 2013-09-11 61.1784
2 2013-09-12 61.8298
3 2013-09-13 60.8108
4 2013-09-16 58.8776
5 2013-09-17 59.5577
6 2013-09-18 60.7821
7 2013-09-19 61.7788
Any help is appreciated.
Date ClosingPrice
0 2013-09-10 64.7010
1 2013-09-11 61.1784
2 2013-09-12 61.8298
3 2013-09-13 60.8108
4 2013-09-16 58.8776
5 2013-09-17 59.5577
6 2013-09-18 60.7821
7 2013-09-19 61.7788
任何帮助表示赞赏。
回答by mrbTT
EDIT 2after seeing more people ending up here. To be clear for new people to python, you should first import pandas for the codes bellow to work:
看到更多人来到这里后编辑 2。为了让 Python 新人清楚,您应该首先导入 Pandas 以使下面的代码正常工作:
import pandas as pd
EDIT 1: (short quick answer)
编辑1:(简短的快速回答)
If3 you don't want to drop your original index (this makes sense after reading the original and long answer bellow) you could:
如果3您不想删除原始索引(在阅读下面的原始和长答案后这是有道理的),您可以:
df[['Date','ClosingPrice']].plot('Date', figsize=(15,8))
Original and long answer:
原始而长的答案:
Try setting your index as your Datetime column first:
首先尝试将索引设置为日期时间列:
df.set_index('Date', inplace=True, drop=True)
Just to be sure, try setting the index dtype (edit: this probably wont be needed as you did it previously):
可以肯定的是,尝试设置索引 dtype(编辑:这可能不会像您以前那样需要):
df.index = pd.to_datetime(df.index)
And then plot it
然后绘制它
df.plot()
If this solves the issue it's because when you use the .plot()
from DataFrame object, the X axis will automatically be the DataFrame's index.
如果这解决了问题,那是因为当您使用.plot()
from DataFrame 对象时,X 轴将自动成为 DataFrame 的索引。
If2 your DataFrame had a Datetimeindex and 2 other columns (say ['Currency','pct_change_1']
) and you wanted to plot just one of them (maybe pct_change_1
) you could:
如果 2 您的 DataFrame 有一个 Datetimeindex 和 2 个其他列(例如['Currency','pct_change_1']
),并且您只想绘制其中的一个(也许pct_change_1
),您可以:
# single [ ] transforms the column into series, double [[ ]] into DataFrame
df[['pct_change_1']].plot(figsize=(15,8))
Where figsize=(15,8)
you're setting the size of the plot (width, height)
.
figsize=(15,8)
您在哪里设置 plot 的大小(width, height)
。
回答by Mahdi
Here is a simple solution:
这是一个简单的解决方案:
my_dict = {'Date':['2013-09-10', '2013-09-11', '2013-09-12', '2013-09-13', '2013-09-16', '2013-09-17', '2013-09-18',
'2013-09-19'], 'ClosingPrice': [ 64.7010, 61.1784, 61.8298, 60.8108, 58.8776, 59.5577, 60.7821, 61.7788]}
df = pd.DataFrame(my_dict)
df.set_index('Date', inplace=True)
df.plot()