JVR*_*JVR 3 matplotlib python-2.7 errorbar
是否可以仅更改误差线的透明度?使用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()
Run Code Online (Sandbox Code Playgroud)
这可以通过检查调用时返回的内容来完成plt.errorbar()。查看文档,它返回一个
plotline:Line2D实例
x,y绘图标记和/或线
caplines:Line2D实例列表
误差条帽
barlinecols:LineCollection的列表
水平和垂直误差范围
这些都可以修改使用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()
Run Code Online (Sandbox Code Playgroud)
这使:
更新:处理多个数据列表时(除了简单地重复上面的代码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()
Run Code Online (Sandbox Code Playgroud)
该示例给出:
| 归档时间: |
|
| 查看次数: |
2142 次 |
| 最近记录: |