【发布时间】:2018-05-17 11:33:39
【问题描述】:
我有一个数据框df,用于在同一个 x 轴上生成两个条形图。我不想将这些值显示为条形图,而是想要一条适合条形的曲线。 IE。它应该有点像高斯或正态分布拟合,但我想保持 x 轴和 y 轴相同,而不是显示频率。我还需要适合从 0 开始并适用于任何负值。我怎样才能做到这一点?我想 scipy curve_fit 函数可能有用或sns.distplot?
df
size a b
0 0.000000 6.20405
1 0.000000 9.262046
2 2.51524 14.28944
3 6.750392 12.756672
4 9.893210 9.733124
5 10.302983 6.690388
6 11.302383 4.86942
7 8.024279 8.32051
8 4.39434 7.228450
9 2.05516 3.767661
x = df['a']
y = df['b']
n = 10
fig, ax = plt.subplots(1)
bar_width = 0.4 # default: 0.8
bar_locations = np.arange(n)
ax.bar(bar_locations, x, bar_width)
ax.bar(bar_locations - bar_width, y, bar_width, color='r')
fig.show()
更新:
fig, ax = plt.subplots()
for a in [x, y]:
sns.distplot(a, bins=range(1, 25, 1), ax=ax, kde=True, fit=stats.gamma)
我如何清理这个数字以 a) 强制 kde 拟合不适合任何负数(数据从 0 开始!)和 b) 删除黑线和绿/蓝条?
【问题讨论】:
标签: python matplotlib plot scipy seaborn