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

我有一个使用 a)Seaborn 和 b)Matplotlib 获得的均匀分布的直方图,我希望曲线下或直方图上的特定区域用不同的颜色突出显示。


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)

plt.fill_between(50,55, color='g')


#With Matplotlib

plt.hist(x)

plt.fill_between(50,55)

但是,我收到一条错误消息,说我有太多的数组索引。


如何在同一图上用另一种颜色填充给定范围的曲线或直方图?


哈士奇WWW
浏览 132回答 1
1回答

梦里花落0921

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.fill_between.html的注释import matplotlib.pyplot as pltimport numpy as npimport seaborn as snssns.set()x = np.random.uniform(45,60, 1000)#With Seabornsns.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 functiony, 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 Matplotlibplt.hist(x,bins=ybin,density=True, zorder=1)plt.fill_between(x_points, y0, y1, color='g', alpha=0.5, zorder=5)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python