我有以下示例代码,我想在一个图中使用seaborn将其绘制为条形图。我可以将实际数据绘制为条形图,但当我尝试添加错误条形图时,会得到以下错误:
AttributeError: 'NoneType' object has no attribute 'seq'
代码为:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
df1 = pd.DataFrame({
'A': ['7.5'],
'B': ['2.4']
})
df1_err = pd.DataFrame({
'A': ['2.3'],
'B': ['1.2']
})
df2 = pd.DataFrame({
'A': ['5.5'],
'B': ['4.2']
})
df2_err = pd.DataFrame({
'A': ['1.7'],
'B': ['2.1']
})
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(6, 4), sharey=True)
my_pal = {"A": "green", "B":"orange"}
sns.set_style("whitegrid")
plt.tight_layout()
sns.barplot(data=df1, palette=my_pal, yerr = df1_err, linewidth=2,edgecolor=[".1","0.1"], ax=axes[0])
sns.barplot(data=df2, palette=my_pal, yerr = df2_err, linewidth=2,edgecolor=[".1","0.1"], ax=axes[1])
plt.show()
如果我从sns.barplot()
命令中删除yerr
,它确实会创建我想要的条形图,但我无法将预先计算的误差条添加到这些子图中。有什么需要帮忙的吗?
发布于 2019-11-17 13:54:19
也许你的意思是这样的:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df1 = pd.DataFrame({
'A': ['7.5'],
'B': ['2.4']
}).astype(float)
df1_err = pd.DataFrame({
'A': ['2.3'],
'B': ['1.2']
}).astype(float)
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(6, 4), sharey=True)
axes[0].bar(df1.T.index.values, np.squeeze(df1.T.values), yerr=np.squeeze(df1_err.T.values))
plt.show()
https://stackoverflow.com/questions/58900954
复制