gpt4 book ai didi

python-2.7 - 透明的误差线,不影响标记

转载 作者:行者123 更新时间:2023-12-04 00:38:38 33 4
gpt4 key购买 nike

是否可以仅更改误差线的透明度?使用plt.errorbar()时,更改alpha会同时影响标记和错误栏。

编辑:

就我而言,我有几个不同的数据集,每个值都有自己的错误,因此我使用plt.errorbar()绘制每个数据集。这是使用3个不同数据集的MWE:

import matplotlib.pyplot as plt
import numpy as np

x1 = [np.random.uniform(0,10,5)]
x2 = [np.random.uniform(0,10,5)]
x3 = [np.random.uniform(0,10,5)]
y1 = [np.random.uniform(0,10,5)]
y2 = [np.random.uniform(0,10,5)]
y3 = [np.random.uniform(0,10,5)]
err1 = [np.random.uniform(1,2, 5)]
err2 = [np.random.uniform(1,2, 5)]
err3 = [np.random.uniform(1,2, 5)]

plt.errorbar(x1, y1, xerr=err1, yerr=err1, fmt='ro', ms=10)
plt.errorbar(x2, y2, xerr=err2, yerr=err2, fmt='bs', ms=10)
plt.errorbar(x3, y3, xerr=err3, yerr=err3, fmt='g^', ms=10)
plt.show()

最佳答案

这可以通过检查调用plt.errorbar()时返回的内容来完成。查看documentation它返回一个

plotline : Line2D instance

x, y plot markers and/or line

caplines : list of Line2D instances

error bar cap

barlinecols : list of LineCollection

horizontal and vertical error ranges


每一个都可以使用 set_alpha()进行修改。因此,为避免更改标记的透明度,请不要更改 plotline
一个完整的例子:
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)

# example variable error bar values
yerr = 0.1 + 0.2*np.sqrt(x)
xerr = 0.1 + yerr

fig, ax = plt.subplots()
markers, caps, bars = ax.errorbar(x, y, yerr=yerr, xerr=xerr,
fmt='o', ecolor='black',capsize=2, capthick=2)

# loop through bars and caps and set the alpha value
[bar.set_alpha(0.5) for bar in bars]
[cap.set_alpha(0.5) for cap in caps]

plt.show()
这使:
enter image description here
更新:处理多个数据列表时(除了简单地重复上述代码x的时间量),一种可行的解决方案是将事物(例如x值,y值等)放在另一个列表中,然后循环遍历这些,意味着您不必手动进行编码。使用修改后的示例:
# Put all your data into other lists
x_list = [x1, x2, x3]
y_list = [y1, y2, y3]
err_list = [err1, err2, err3]
formats = ['ro', 'bs', 'g^']

# Loop through data and plot
for x, y, err, f in zip(x_list, y_list, err_list, formats):
markers, caps, bars = plt.errorbar(x, y, xerr=err, yerr=err, fmt=f, ms=10)
[bar.set_alpha(0.5) for bar in bars]
[cap.set_alpha(0.5) for cap in caps]

plt.show()
该示例给出了以下内容:
enter image description here

关于python-2.7 - 透明的误差线,不影响标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48213884/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com