Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

python - Matplotlib-绘制不均匀的线分布(Matplotlib - draw a non-uniform line distribution)

Consider the following matplotlib example: https://matplotlib.org/3.1.1/gallery/recipes/fill_between_alpha.html

(考虑以下matplotlib示例: https ://matplotlib.org/3.1.1/gallery/recipes/fill_between_alpha.html)

With fill_between it can show a standard deviation:

(使用fill_between它可以显示标准偏差:)

ax.fill_between(t, mu1+sigma1, mu1-sigma1, facecolor='blue', alpha=0.5)

However, the area is filled uniformly, while the actual distribution is normal for each x-coordinate (I think).

(但是,该区域被均匀填充,而每个x坐标的实际分布是??正态的(我认为)。)

Is it possible to fill the area to reflect it (so that the color is the denser the closer the point is to mu1 ).

(是否可以填充该区域以反映该区域(以使颜色越靠近该点, mu1 )。)

You can assume that I have arrays mean and std , which define a normal distribution for each x-coordinate, and I want to draw these distributions: alpha should be proportional to density.

(您可以假设我有meanstd数组,它们为每个x坐标定义了一个正态分布,并且我想绘制这些分布: alpha应该与密度成比例。)

As a solution, I can probably use several layers of fill_between , but it's not precise and is very hacky.

(作为解决方案,我可能可以使用几层fill_between ,但这并不精确,而且很hacky。)

  ask by aaa translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably you could use a filled contour with X,Y,Z data created with np.meshgrid and norm.pdf .

(可能您可以使用由np.meshgridnorm.pdf创建的X,Y,Z数据填充轮廓 。)

I removed the fill_between 's in the fill between example and added the following:

(我在示例之间填充中删除了fill_between ,并添加了以下内容:)

import matplotlib.cm as cm
from scipy.stats import norm

width = 2.0
YS = np.linspace(np.min(mu1 - sigma1 * width),
                 np.max(mu1 + sigma1 * width), 101)

X,Y = np.meshgrid(t,YS)
Z = np.zeros_like(X, dtype=np.float)
for i, (mu,sigma) in enumerate(zip(mu1,sigma1)):
    Z[:,i] = norm.pdf(YS, loc=mu, scale=sigma) * sigma

plt.contourf(X,Y,Z, cmap=cm.BuGn, levels=100)

This results in:

(结果是:) 在此处输入图片说明

This does not use alpha yet, so it is only useful for one line, probably you can use alpha with customized colormaps, but this is trickier.

(它尚不使用Alpha ,因此仅对一行有用,可能您可以将Alpha与自定义颜色表一起使用,但这比较棘手。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...