简体   繁体   English

Pandas / NumPy —将日期绘制为X轴

[英]Pandas/NumPy — Plotting Dates as X axis

My Goal is just to plot this simple data, as a graph, with x data being dates ( date showing in x-axis) and price as the y-axis. 我的目标只是将这些简单数据绘制为图形,其中x数据为日期(日期在x轴上显示),而价格为y轴。 Understanding that the dtype of the NumPy record array for the field date is datetime64[D] which means it is a 64-bit np.datetime64 in 'day' units. 了解字段日期的NumPy记录数组的dtype是datetime64 [D],这意味着它是一个以“天”为单位的64位np.datetime64。 While this format is more portable, Matplotlib cannot plot this format natively yet. 尽管此格式更具可移植性,但Matplotlib尚无法原生绘制该格式。 We can plot this data by changing the dates to DateTime.date instances instead, which can be achieved by converting to an object array: which I did below view the astype('0'). 我们可以通过将日期更改为DateTime.date实例来绘制此数据,这可以通过转换为对象数组来实现:我在下面查看了astype('0')。 But I am still getting 但我仍然

this error : 这个错误:

 view limit minimum -36838.00750000001 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

code: 码:

import pandas as pd
import matplotlib.pyplot as plt


df = pd.read_csv(r'avocado.csv')

df2 = df[['Date','AveragePrice','region']]
df2 = (df2.loc[df2['region'] == 'Albany'])

df2['Date'] = pd.to_datetime(df2['Date'])
df2['Date'] = df2.Date.astype('O')

plt.style.use('ggplot')
ax = df2[['Date','AveragePrice']].plot(kind='line', title ="Price Change",figsize=(15,10),legend=True, fontsize=12)
ax.set_xlabel("Period",fontsize=12)
ax.set_ylabel("Price",fontsize=12)

plt.show()

df.head(3) df.head(3)

    Unnamed: 0  Date    AveragePrice    Total Volume    4046    4225    4770    Total Bags  Small Bags  Large Bags  XLarge Bags type    year    region
0   0   2015-12-27  1.33    64236.62    1036.74 54454.85    48.16   8696.87 8603.62 93.25   0.0 conventional    2015    Albany
1   1   2015-12-20  1.35    54876.98    674.28  44638.81    58.33   9505.56 9408.07 97.49   0.0 conventional    2015    Albany
2   2   2015-12-13  0.93    118220.22   794.70  109149.67   130.50  8145.35 8042.21 103.14  0.0 conventional    2015    Albany
df2 = df[['Date', 'AveragePrice', 'region']]
df2 = (df2.loc[df2['region'] == 'Albany'])
df2['Date'] = pd.to_datetime(df2['Date'])
df2 = df2[['Date', 'AveragePrice']]
df2 = df2.sort_values(['Date'])
df2 = df2.set_index('Date')

print(df2)

ax = df2.plot(kind='line', title="Price Change")
ax.set_xlabel("Period", fontsize=12)
ax.set_ylabel("Price", fontsize=12)

plt.show()

output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM