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?

1

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

thanx! I tried to use df.index but in ticks got an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.