0

I have a dataframe as below

import pandas as pd
import matplotlib.pylab as plt
df = pd.DataFrame({'name':['one', 'two', 'three'], 'assess':[100,200,300]})

I want to build errorbar like this

c = 30
plt.errorbar(df['name'], df['assess'], yerr=c, fmt='o')

and of course i get

ValueError: could not convert string to float

I can convert string to float, but I'm losing value signatures and maybe there's a more elegant way?

Edward
  • 4,443
  • 16
  • 46
  • 81
  • possible dublicate of http://stackoverflow.com/questions/40266187/matplotlib-cannot-plot-categorical-values or http://stackoverflow.com/questions/40510070/plotting-in-python3-histogram or http://stackoverflow.com/questions/31029560/plotting-categorical-data-with-pandas-and-matplotlib or http://stackoverflow.com/questions/32294586/categorical-data-in-subplots or http://stackoverflow.com/questions/33958068/matplotlib-how-to-plot-a-line-with-categorical-data-on-the-x-axis – ImportanceOfBeingErnest Nov 15 '16 at 18:38

1 Answers1

4

Matplotlib can indeed only work with numerical data. There is an example in the matplotlib collection showing how to handle cases where you have categorical data. The solution is to plot a range of values and set the labels afterwards using plt.xticks(ticks, labels) or a combination of ax.set_xticks(ticks) and ax.set_xticklabels(labels).

In your case the former works fine:

import pandas as pd
import matplotlib.pylab as plt
df = pd.DataFrame({'name':['one', 'two', 'three'], 'assess':[100,200,300]})

c = 30
plt.errorbar(range(len(df['name'])), df['assess'], yerr=c, fmt='o')
plt.xticks(range(len(df['name'])), df['name'])

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712