0

I have a dataframe with a column 'Date' and a column 'Close Price' for the S&P500 from 2010 to 2017. The column date has format YYYY:MM:DD.

I am trying to make a close price vs date graph in matplotlib. The problem is that there are no default x-axis ticks for the date, there is just a black line. I guess the reason is that there are too many dates. Is it possible to make at least a few dates to show? Such as the 1st of each year? Thank you so much for your help in advance!

fig = plt.figure()
ax = plt.axes()
x = df['Date']
y = df['Close']
ax.plot(x,y,label='S&P500')
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels)

enter image description here

Lauren
  • 337
  • 1
  • 2
  • 4
  • 1
    [Probably helpful](https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/date.html), though probably best to work with a datetime and not an object which presumably the `date` column is currently. – BigBen Jan 06 '21 at 18:53
  • Thank you so much! Date was an object. I converted it to datetime and now it works perfectly, giving automatically year ticks. Thank you! – Lauren Jan 06 '21 at 19:07
  • 1
    Does this answer your question? [X-axis tick labels are too dense when drawing plots with matplotlib](https://stackoverflow.com/questions/54783160/x-axis-tick-labels-are-too-dense-when-drawing-plots-with-matplotlib) – zabop Jan 06 '21 at 19:07

0 Answers0