此问题已在此处有答案:
How to add errorbars to stripplot(1个答案)
Plotting errorbars on top of swarmplot(1个答案)
5小时前关闭
我用这个问题末尾发布的代码和数据创建了以下图表:
黑点表示所有零售商的R2得分的平均值,黑线表示相应的标准差。
我想实现以典型的方式显示平均值和标准差,如下所示:
我猜这在matplotlib errorbar或seaborn pointplot中一定是可能的。但我在这方面的工作年龄和不能找到一个解决方案。
下面是相应的代码:
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
test = pd.read_csv('test.csv')
# Calculate mean and standard deviation
mean_data = test.groupby('featureset')['r2_score'].mean().values
std_data = test.groupby('featureset')['r2_score'].std().values
featuresets = ["c", "fc", "f", "s", "sc", "w"]
p = sns.stripplot(x="featureset",
y="r2_score",
hue="retailer",
data=test,
marker="^",
size=8)
# Plot stripplot with mean and standard deviation
sns.pointplot(x=featuresets,
y=mean_data,
join=False,
color='black',
markers='o',
scale=2)
sns.pointplot(x=featuresets,
y=mean_data - std_data,
join=False,
color='black',
markers='_',
scale=4)
sns.pointplot(x=featuresets,
y=mean_data + std_data,
join=False,
color='black',
markers='_',
scale=4)
plt.legend(title='Retailer')
sns.move_legend(p, loc="upper left", bbox_to_anchor=(1, 1))
p.set(xlabel='Featureset', ylabel='R2 Score')
plt.savefig("plot.png", format="png", bbox_inches='tight')
为了获得完整的可再现性,在这里添加使用的数据集:
r2_score,featureset,retailer
0.7055950484,c,S
0.942584686,c,K
0.8651950609,c,B
0.9051873402,c,H
0.5877088336,c,P
0.7944303127,c,O
0.6370605237,fc,S
0.9755270173,fc,K
0.9065356558,fc,B
0.921142567,fc,H
0.5798048892,fc,P
0.6580349995,fc,O
0.7217345443,f,S
0.9755270173,f,K
0.8839177116,f,B
0.921142567,f,H
0.5070612616,f,P
0.6580349995,f,O
0.5678318495,s,S
0.9637899061,s,K
0.9369641498,s,B
0.9297479733,s,H
0.5029283363,s,P
0.6580349995,s,O
0.5678318495,sc,S
0.9729308458,sc,K
0.8471079755,sc,B
0.9297479733,sc,H
0.497615548,sc,P
0.6580349995,sc,O
0.6624239947,w,S
0.889206858,w,K
0.7810312601,w,B
0.8562172874,w,H
0.4446346851,w,P
0.6580349995,w,O
1条答案
按热度按时间clj7thdc1#
你的想法是对的
Errorbar
工作。您还需要yerr
和capsize
参数。输出: