Open avnikadubey opened 1 year ago
Did you make the date into a datetime function? This could help with isolating the year. To do this you would type the following command I believe [let df be your data frame name]:
df = pd.to_datetime(df)
Then you could make a variable of a new truncated data frame with only specific years:
main_data = df[df.index.year >= 2019]
I hope this helps with your process :) [sorry if it didn't]
Matplotlib supports date plotting through DateTime class. You can try this code for example; however, the exact code depends on the string format of your date. Please let me know if any lines are confusing.
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
dates = ['01/02/1991','01/09/1991','01/22/1991','02/04/1991']
# convert string date to DateTime class
x = [dt.datetime.strptime(d,'%m/%d/%Y').date() for d in dates]
y = [1, 2, 5, 9]
fig, ax = plt.subplots()
ax.plot(x,y,'o-b')
# format how date will look like on x axis
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%Y'))
# choose tick frequency (day, month, year)
ax.xaxis.set_major_locator(mdates.MonthLocator())
# slant the date to make it more readable
fig.autofmt_xdate()
I've come to an issue with analyzing my data where, when I try to put it into a graph, the X-axis looks quite messy as there are over 1,042 dates being analyzed. I was wondering if anyone knew a way to convert the dates on the X-axis to a range of years from 2019-2023 or maybe create an interval of dates? I've attached my code below! Thank you in advance!