Phrixus
Phrixus

Reputation: 1219

Pandas line graph x-axis extra values

I have the following:

df.groupby(['A', 'B'])['time'].mean().unstack().plot()

This gives me a line graph like this one:

enter image description here

The circles in the plot indicate data points. The values on x-axis are the values of A which are discrete (100, 200 and 300). That is, I only have data points on the 100th, 200th and 300th point on the x-axis. Pandas/Matplotlib adds the intermediate values on the x-axis (125, 150, 175, 225, 250 and 275) that I don't want.

How can I plot and tell Pandas not to add the extra values on the x-axis?

Upvotes: 0

Views: 897

Answers (1)

Coup
Coup

Reputation: 740

Matplotlib x axis tick locators

You're looking for tick locators

enter image description here

import matplotlib.ticker as ticker
df = pd.DataFrame({'y':[1, 1.75, 3]}, index=[100, 200, 300])
ax = df.plot(legend=False)
ax.xaxis.set_major_locator(ticker.MultipleLocator(100))

Upvotes: 2

Related Questions