Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show categorical x-axis values when making line plot from pandas Series in matplotlib

How do I get the x-axis values of [a, b, c] to show up?

import pandas as pd
import matplotlib.pyplot as plt

s = pd.Series([1, 2, 10], index=['a', 'b', 'c'])
s.plot()
plt.show()

enter image description here

like image 439
Max Ghenis Avatar asked Apr 21 '18 23:04

Max Ghenis


People also ask

How do I show X-axis values in MatPlotLib?

MatPlotLib with Python To show all X coordinates (or Y coordinates), we can use xticks() method (or yticks()).

How do I change the x-axis value in pandas?

To set Dataframe column value as X-axis labels in Python Pandas, we can use xticks in the argument of plot() method.


1 Answers

You can get your xtick labels to show using plt.xticks:

import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series([1, 2, 10], index=['a', 'b', 'c'])
s.plot()
plt.xticks(np.arange(len(s.index)), s.index)
plt.show()

Output:

enter image description here

like image 126
Scott Boston Avatar answered Oct 06 '22 00:10

Scott Boston