Pandas bar plot ignores the xlim parameter

Question:

I would like to only make a plot with only the year between 1990 and 2000.

import random
import pandas as pd

df = pd.DataFrame({'year':range(1950,2020),
                   'value':[random.randint(100, 1000) for x in range(70)]})
df.plot(kind = 'bar', x = 'year', y ='value', xlim = (1990,2000))

Whatever I set the value of xlim to: (1990,2000) or [1990,2000] or 1990, it still displays the full range of the year from 1950 to 2019.

I know that one solution is to first select only the rows of year 1990 to 2000, then make the plot, like this:

df.query('1990 <= year <= 2000').plot(kind = 'bar', x = 'year', y ='value')

But what is the problem for the above code?

Asked By: sam

||

Answers:

A bar plot is categorical. Internally, the bars are numbered 0,1,2,.... Pandas’ bar plot doesn’t seem to look at the xlim= argument, but you can call plt.xlim(40, 50) after creating the plot.

The following code looks up the categorical position of the values to set the x limits. 0.5 is added to avoid bars cut into two.

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

df = pd.DataFrame({'year': range(1950, 2020),
                   'value': [random.randint(100, 1000) for x in range(70)]})
ax = df.plot(kind='bar', x='year', y='value', rot=0)
ax.set_xlim(np.where(df['year'] == 1990)[0] - 0.5, np.where(df['year'] == 2000)[0] + 0.5)
plt.show()

pandas bar plot with x limits

Answered By: JohanC