Home > Software engineering >  Why are only a few of my x axis labels plotting in Matplotlib?
Why are only a few of my x axis labels plotting in Matplotlib?

Time:04-02

I am trying to make a line graph, but not all of my x axis labels are showing. All the data are showing, just not the labels. I would like for the labels to go from April 4 to June 27. Though, here it only displays thru May 30. Below is my code. The data I sued to plot this as well as the x axis are form a csv file.

#CSV data
df = pd.read_csv(r'C:\Downloads\Planting_Corn.csv', index_col=0)

#plot set up
fig = plt.figure(figsize=(10, 6))

# Creating subplot/axes
ax = fig.add_subplot(111)

#plotting each year
df.y_2000.plot(color='pink',lw=1.3, label='2000')
df.y_2001.plot(color='pink',lw=1.3, linestyle='--', label='2001')
df.y_2002.plot(color='magenta',lw=1.3, label='2002')
df.y_2003.plot(color='magenta',lw=1.3, linestyle='--', label='2003')
df.y_2004.plot(color='purple',lw=1.3, label='2004')
df.y_2005.plot(color='purple',lw=1.3, linestyle='--', label='2005')
df.y_2006.plot(color='deepskyblue',lw=1.3, label='2006')
df.y_2007.plot(color='deepskyblue',lw=1.3, linestyle='--', label='2007')
df.y_2008.plot(color='blue',lw=1.3, label='2008')
df.y_2009.plot(color='blue',lw=1.3, linestyle='--', label='2009')
df.y_2010.plot(color='navy',lw=1.3, label='2010')
df.y_2011.plot(color='navy',lw=1.3, linestyle='--', label='2011')
df.y_2012.plot(color='cyan',lw=1.3, label='2012')
df.y_2013.plot(color='cyan',lw=1.3, linestyle='--', label='2013')
df.y_2014.plot(color='limegreen',lw=1.3, label='2014')
df.y_2015.plot(color='limegreen',lw=1.3, linestyle='--', label='2015')
df.y_2016.plot(color='green',lw=1.3, label='2016')
df.y_2017.plot(color='green',lw=1.3, linestyle='--', label='2017')
df.y_2018.plot(color='orange',lw=1.3, label='2018')
df.y_2019.plot(color='orange',lw=1.3, linestyle='--', label='2019')
df.y_2020.plot(color='red',lw=1.3, label='2020')
df.y_2021.plot(color='red',lw=1.3, linestyle='--', label='2021')
df.y_2022.plot(color='black',lw=2, label='2022', zorder=25)
df.av_20.plot(color='darkgray',lw=2, linestyle='--', label='5-yr')
df.av_5.plot(color='gray',lw=2, linestyle='--', label='20-yr')

#Table info

ax.set_title('U.S. Corn Planting Progress', fontsize=18, fontweight='bold')
ax.set_ylim([0, 100])
ax.set_ylabel('Progress (%)', fontsize=15)
ax.set_xlabel('Date', fontsize=15)
plt.xticks(fontsize=13, rotation=25)
plt.yticks(fontsize=13)


#legend
ax.legend(frameon=False, loc='lower center', bbox_to_anchor=(.5, -.45), ncol=5, fontsize=11)

plt.grid(True, color='lightgray')
plt.show()

output

I have tried other solutions such as manually setting my labels, but would like to pull them directly from the csv file.CSV screenshot

CodePudding user response:

You can use xticks argument to plot to set the number of ticks that you want like so:

df.plot(lw=1.3, label='2020', xticks=np.arange(N_weeks))

But that can make your plot pretty dense and unreadable. This can be mitigated by rotating the xticks so they show up vertical.

Check out a live implementation here with mock data.

  • Related