使用 pandas 和日期时间格式进行绘图 [英] Plotting using Pandas and datetime format

查看:150
本文介绍了使用 pandas 和日期时间格式进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有两列的数据框,日期和ClosingPrice.我正在尝试使用df.plot()绘制它们,但不断出现此错误:

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:

ValueError:最小查看限制-36785.37852小于1,并且是无效的Matplotlib日期值.如果您将非datetime值传递给具有datetime单位的轴,通常会发生这种情况

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

我从matplotlib找到了有关此文档,但是该文档说明了如何确保格式为datetime.这是我必须确保格式为日期时间的代码,并在尝试绘制之前还要打印每列的数据类型.

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.

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 任何帮助表示赞赏.

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.

推荐答案

编辑2 .为了使新手了解python,您应该首先导入熊猫以使下面的代码起作用:

EDIT 2 after 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:

import pandas as pd

编辑1 :(简短快速答案)

如果³您不想删除原始索引(在阅读原始且冗长的答案之后,这很有意义),您可以:

If³ you don't want to drop your original index (this makes sense after reading the original and long answer bellow) you could:

df[['Date','ClosingPrice']].plot('Date', figsize=(15,8))

原始答案和长答案:

首先尝试将索引设置为日期时间"列:

Try setting your index as your Datetime column first:

df.set_index('Date', inplace=True, drop=True)

可以肯定的是,请尝试设置索引dtype(可能不需要像以前一样进行此操作):

Just to be sure, try setting the index dtype (edit: this probably wont be needed as you did it previously):

df.index = pd.to_datetime(df.index)

然后绘制

df.plot()

如果这解决了问题,那是因为当您使用DataFrame对象中的.plot()时,X轴将自动成为DataFrame的索引.

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.

如果²您的DataFrame具有Datetimeindex和另外2列(例如['Currency','pct_change_1']),而您只想绘制其中之一(也许是pct_change_1),则可以:

If² 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:

# single [ ] transforms the column into series, double [[ ]] into DataFrame
df[['pct_change_1']].plot(figsize=(15,8)) 

figsize=(15,8)处,您要设置图(width, height)的大小.

Where figsize=(15,8) you're setting the size of the plot (width, height).

这篇关于使用 pandas 和日期时间格式进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆