Issue
So, I have a dataset of stock prices, let's call it sp.csv
.
It has a date column that looks like this
A price column that looks like this
Originally, my graph looked like this:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
df = pd.read_csv('sp.csv')
plt.figure(figsize=(10,8))
plt.xlabel('year')
plt.ylabel('price')
plt.plot(df['DATE'], df['PRC'])
I fixed the first graph's shape by converting the dates to a different format and adding a new column of it to the df, but now it has issues with the dates on the x-axis:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
df = pd.read_csv('sp.csv')
dates = df['DATE']
newdates = []
for i in dates:
date = datetime.strptime(str(i), '%Y%m%d').strftime('%m%d%Y')
newdates.append(date)
df['DATE2'] = newdates
plt.figure(figsize=(10,8))
plt.xlabel('year')
plt.ylabel('price')
plt.plot(df['DATE2'], df['PRC'])
I tried fixing it, but I'm not getting anywhere. I tried adding locators and formatters, but then the years just disappear and start from the default 1970:
ax = plt.gca()
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.gcf().autofmt_xdate()
I don't really know what I'm doing, I just want the x-axis to display either the years or the years and months.
Solution
When you convert the date using strptime()
, that will convert it to datetime. Do not use strftime()
, which will convert it back to string. Check by using df.info()
and make sure you see that the column you are plotting is datetime. Only then will you be able to use the dateformatters...
Below is the updated code. As I only had under 2 years AT&T data and my input date format was DD-MM-YYYY, this is the code and the graph I got. But, should work with years only as well.
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
df = pd.read_csv('sp.csv')
dates = df['DATE']
newdates = []
for i in dates:
date = datetime.strptime(str(i), '%d-%m-%Y')#.strftime('%m%d%Y')
newdates.append(date)
df['DATE2'] = newdates
plt.figure(figsize=(10,8))
plt.xlabel('year')
plt.ylabel('price')
plt.plot(df['DATE2'], df['PRC'])
ax = plt.gca()
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.gcf().autofmt_xdate()
Answered By - Redox
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.