为了账号安全,请及时绑定邮箱和手机立即绑定

如何为绘图上的分布范围填充另一种颜色

如何为绘图上的分布范围填充另一种颜色

哈士奇WWW 2022-06-28 17:47:23
我有一个使用 a)Seaborn 和 b)Matplotlib 获得的均匀分布的直方图,我希望曲线下或直方图上的特定区域用不同的颜色突出显示。import matplotlib.pyplot as pltimport numpy as npimport seaborn as snssns.set()x = np.random.uniform(45,60, 1000)#With Seabornsns.distplot(x)plt.fill_between(50,55, color='g')#With Matplotlibplt.hist(x)plt.fill_between(50,55)但是,我收到一条错误消息,说我有太多的数组索引。如何在同一图上用另一种颜色填充给定范围的曲线或直方图?
查看完整描述

1 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.fill_between.html的注释


import matplotlib.pyplot as plt

import numpy as np

import seaborn as sns


sns.set()


x = np.random.uniform(45,60, 1000)



#With Seaborn

sns.distplot(x)

# fill square defined by x, y0, y1

# need to get pdf and bins generated by sns to do fill between here


## With numpy is easier for me


# density function

y, ybin = np.histogram(x, bins=np.arange(45,61), density=True)


x_points = np.arange(50,56)

x_point_inds = np.arange(5,11)

y0 = np.zeros(6)

y1 = y[x_point_inds]


#With Matplotlib

plt.hist(x,bins=ybin,density=True, zorder=1)

plt.fill_between(x_points, y0, y1, color='g', alpha=0.5, zorder=5)


查看完整回答
反对 回复 2022-06-28
  • 1 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号