Set Xticks frequency to dataframe index

Question:

I currently have a dataframe that has as an index the years from 1990 to 2014 (25 rows). I want my plot to have the X axis with all the years showing. I’m using add_subplot as I plan to have 4 plots in this figure (all of them with the same X axis).

To create the dataframe:

import pandas as pd
import numpy as np

index = np.arange(1990,2015,1)
columns = ['Total Population','Urban Population']

pop_plot = pd.DataFrame(index=index, columns=columns)
pop_plot = df_.fillna(0)

pop_plot['Total Population'] = np.arange(150,175,1)
pop_plot['Urban Population'] = np.arange(50,125,3)

      Total Population  Urban Population
1990               150                50
1991               151                53
1992               152                56
1993               153                59
1994               154                62
1995               155                65
1996               156                68
1997               157                71
1998               158                74
1999               159                77
2000               160                80
2001               161                83
2002               162                86
2003               163                89
2004               164                92
2005               165                95
2006               166                98
2007               167               101
2008               168               104
2009               169               107
2010               170               110
2011               171               113
2012               172               116
2013               173               119
2014               174               122

The code that I currently have:

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(2,2,1, xticklabels=pop_plot.index)
plt.subplot(2, 2, 1)

plt.plot(pop_plot)
legend = plt.legend(pop_plot, bbox_to_anchor=(0.1, 1, 0.8, .45), loc=3, ncol=1, mode='expand')
legend.get_frame().set_alpha(0)

ax1.set_xticks(range(len(pop_plot.index)))

This is the plot that I get:

Plot with ax1.set_xticks

When I comment the set_xticks I get the following plot:

#ax1.set_xticks(range(len(pop_plot.index)))

Regular plot

I’ve tried a couple of answers that I found here, but I didn’t have much success.

Asked By: vgastaldi

||

Answers:

It’s not clear what ax1.set_xticks(range(len(pop_plot.index))) should be used for. It will set the ticks to the numbers 0,1,2,3 etc. while your plot should range from 1990 to 2014.

Instead, you want to set the ticks to the numbers of your data:

ax1.set_xticks(pop_plot.index)

Complete corrected example:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

index = np.arange(1990,2015,1)
columns = ['Total Population','Urban Population']

pop_plot = pd.DataFrame(index=index, columns=columns)

pop_plot['Total Population'] = np.arange(150,175,1)
pop_plot['Urban Population'] = np.arange(50,125,3)


fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(2,2,1)

ax1.plot(pop_plot)
legend = ax1.legend(pop_plot, bbox_to_anchor=(0.1, 1, 0.8, .45), loc=3, ncol=1, mode='expand')
legend.get_frame().set_alpha(0)

ax1.set_xticks(pop_plot.index)
plt.show()
  • The easiest option is to use the xticks parameter for pandas.DataFrame.plot
    • Pass the dataframe index to xticks: xticks=pop_plot.index
# given the dataframe in the OP
ax = pop_plot.plot(xticks=pop_plot.index, figsize=(15, 5))

# move the legend
ax.legend(bbox_to_anchor=(0.1, 1, 0.8, .45), loc=3, ncol=1, mode='expand', frameon=False)

enter image description here

Answered By: Trenton McKinney
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.