问题
I'm trying to generate bar plots from a DataFrame like this:
Pre Post
Measure1 0.4 1.9
These values are median values I calculated from elsewhere, and I have also their variance and standard deviation (and standard error, too). I would like to plot the results as a bar plot with the proper error bars, but specifying more than one error value to yerr yields an exception:
# Data is a DataFrame instance
fig = data.plot(kind="bar", yerr=[0.1, 0.3])
[...]
ValueError: In safezip, len(args[0])=1 but len(args[1])=2
If I specify a single value (incorrect) all is fine. How can I actually give each column its correct error bar?
回答1:
What is your data shape?
For an n-by-1 data vector, you need a n-by-2 error vector (positive error and negative error):
import pandas as pd
import matplotlib.pyplot as plt
df2 = pd.DataFrame([0.4, 1.9])
df2.plot(kind='bar', yerr=[[0.1, 3.0], [3.0, 0.1]])
plt.show()
回答2:
if lucasg's answer does not work
For whatever reason, the accepted answer did not work properly for me (compare my comment there). I am using the following versions
Versions: Python 3.7.6 (anaconda), mpl: 3.1.3, pd: 1.0.1, np: 1.18.1
What worked, however is a modification of this question. That questions discussion points also in the direction that, there might be some problems with different versions and asymmetric error bar plotting.
Code Plots asymmetric errorbars from min to max around the mean of a dataframe column.
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
print('Versions, mpl: {:}, pd: {:}, np: {:}'.format(mpl.__version__, pd.__version__, np.__version__))
df = pd.DataFrame()
df['a'] = [1, 1, 2, 3, 3]
df['b'] = [0.5, 0.5, 2, 3, 4]
print(df)
plt.bar(np.arange(df.shape[1]), df.mean(), yerr=[df.mean()-df.min(), df.max()-df.mean()], capsize=6)
plt.grid()
plt.show()
来源:https://stackoverflow.com/questions/13030488/using-pandas-to-plot-barplots-with-error-bars