1

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)

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.

Thanks in advance.

vgastaldi
  • 13
  • 5
  • Why would you expect `range(len())` will give values starting at 1900? – roganjosh Oct 15 '17 at 13:41
  • I wouldn't. The labels are provided by xticklabels=pop_plot.index. – vgastaldi Oct 15 '17 at 13:46
  • I'm not sure I can make sense of the question. I don't understand the intended function of `ax1.set_xticks(range(len(pop_plot.index))` then at all, based on your response. Also, without seeing data, it's not possible to see why you don't get the full range of years. – roganjosh Oct 15 '17 at 14:01
  • I'm sorry it is not clear, english is not my main language. What would be the best way to show you my data? It is a csv file. From what I read ax1.set_xticks(range(len(pop_plot.index)) would be able to set the number of ticks on X axis to the same lenght of my index. I want to show how the population changes during the years. Currently I only have labels for 6 years, but I want all of them to show in the plot. By removing xticklabels=pop_plot.index the labels in the X axis change to: 1990, 1995, 2000, 2005, 2010 and 2015. The range is correct, but still only six values are showed. – vgastaldi Oct 15 '17 at 14:04
  • Generally you should provide an [MCVE](https://stackoverflow.com/help/mcve). It might just be me not grasping the issue in this case, but it's best if you can post an example that can be run locally to illustrate the issue. – roganjosh Oct 15 '17 at 14:07

1 Answers1

2

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()
ImportanceOfBeingErnest
  • 289,005
  • 45
  • 571
  • 615