1
votes

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. 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. While this format is more portable, Matplotlib cannot plot this format natively yet. 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'). 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)

    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
1
reason doing this df2['Date'] = df2.Date.astype('O') ? - Nihal
you may have wrong date in your data set - Nihal
what do you mean wrong date? Regarding you first question, to create an object. I am following this tutorial-->matplotlib.org/gallery/recipes/common_date_problems.html and it seemed to point me that way - 0004
remove those 2 zeroes from link - Nihal
can you show me df2.head() ? - Nihal

1 Answers

0
votes
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:

enter image description here