Wai Lau
Wai Lau

Reputation: 25

How do I format matplotlib stacked area chart axis to show only range given?

I'm trying to create a simple stacked area chart. Here is my code:

x = [1990,1995,2000,2005,2010]
y = [df['a'],df['b'],df['c']]
...
plt.stackplot(x,y, labels=['a,b,c'], ...)
...
plt.show()

However, x-axis on the graph created goes up in intervals of 2.5 so it looks like this: 1990.0, 1992.5, 1995.0....

How do I change it so that the graph created on the x-axis goes up in intervals of 5?

Upvotes: 1

Views: 406

Answers (1)

pis7aller
pis7aller

Reputation: 63

You can create a subplot and set the major and minor locators of x axis like this:

import matplotlib.pyplot as plt
from matplotlib.ticker import (AutoMinorLocator, MultipleLocator)
fig, ax = plt.subplots()
ax.set_xlim(1990, 2015)

ax.xaxis.set_major_locator(MultipleLocator(5))
ax.xaxis.set_minor_locator(AutoMinorLocator(1))
plt.stackplot(x,y, labels=['a,b,c'], ...)
plt.show()

Upvotes: 1

Related Questions