Incorrect x-axis ticks on chart

Hi all,

I am trying to generate line chart from the following dataframe, named year_data.
Screen Shot 2020-05-21 at 12.51.49 PM

My code:

fig = plt.figure(figsize=(25,7)) 
ax = fig.add_subplot(1,2,1)

ax.plot(year_data.index,'AllPosts',data=year_data, marker='o',color='red')
ax.plot(year_data.index,'TotalDL_Posts',data=year_data, marker='o',color='blue')
ax.spines["right"].set_visible(False)    
ax.spines["left"].set_visible(False)
ax.spines["top"].set_visible(False)    
ax.spines["bottom"].set_visible(False)
# ax.set_xlim(2015, 2019)
plt.show()

What I expected to happen:
The X-axis in the chart to display years through 2015 -2019, ie the index in the dataframe.

What actually happened:
image

I am not able to figure out what is it that Iā€™m missing here. Any help will be appreciated.

Thanks,
Anjali

Figured it out myself.
Was missing these:

plt.xticks(year_data.index)
ax.set_xticklabels(year_data.index)

image
:slight_smile:

1 Like