1

I am using pandas and matplotlib and I am trying to set the label on x axis by the index in Series of panda

import pandas as pd
import matplotlib.pyplot as plt

index = ['apples','oranges','cherries','bananas']
quantity = [20,30,40,50]

s = pd.Series(quantity, index = index)
s.plot()
plt.title("pandas series")
plt.show()

Output Graph

and it displays the output without the label on the x axis, I need fruits name as label on X-axis. Can anyone please help me to resolve this error?

Thanks in advance !

Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44
Bhavani K
  • 13
  • 3
  • Since there is no intrinsic ordering to your x-values, you probably shouldn't be using a lineplot. If you do `s.plot(kind='bar')` then the labels will show properly. – ALollz Oct 03 '18 at 18:39

3 Answers3

1

There seems to be some problem with pandas (currently?) as also seen from Make pandas plot() show xlabel and xvalues.

Here using matplotlib directly is a good option as well. Just replace s.plot() by

plt.plot(s)

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
1

You just have to define the locations. Do it like so:

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

index = ['apples','oranges','cherries','bananas']
quantity = [20,30,40,50]

s = pd.Series(quantity, index = index)
s.plot()
plt.title("pandas series")
plt.xticks(np.arange(4), index) 
plt.show()
Allen P.
  • 23
  • 1
  • 3
0

it doesn't really make sense to use a line plot here, since the categories are independent.

try a bar plot, which will automatically include the labels. s.plot(kind='bar')

Andrew
  • 950
  • 7
  • 24