英文:
plot errorbars but bars for only selected x-values
问题
我可以绘制误差条。然而,我的图表有更多的数据,我只想为特定的 x 值绘制误差条。
这意味着我的误差条数据和图表数据的大小不同。我该怎么做?
figure_name = simulation_name+"_eta="+eta+"_arm_length="+arm_length+"_nint="+str(current_nint)
fig1, axs1 = plt.subplots(1)
ys=fitnessesPlot[:,0:2000:100]
ys=np.random.randn(20,2000)
errorbars=np.std(ys,0)
ymean=np.mean(ys,0)
xs=np.linspace(0,1999,20)
plt.errorbar(xs,ymean,errorbars)
plt.savefig(path+"FitVsGen_"+filename+".png")
plt.close()
英文:
I am able to plot error bars. However, I have far more data for the graph and I want to plot the error bars for only particular x values.
This means I that my error bar data and my graph data have different sizes. How can I do this?
figure_name = simulation_name+"_eta="+eta+"_arm_length="+arm_length+"_nint="+str(current_nint)
fig1, axs1 = plt.subplots(1)
ys=fitnessesPlot[:,0:2000:100]
ys=np.random.randn(20,2000)
errorbars=np.std(ys,0)
ymean=np.mean(ys,0)
xs=np.linspace(0,1999,20)
plt.errorbar(xs,ymean,errorbars)
plt.savefig(path+"FitVsGen_"+filename+".png")
plt.close()
答案1
得分: 3
如果您将错误值指定为数组(而不是标量),则可以在那些不想显示误差线的位置将错误值设置为np.nan
:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10.0)
y = np.sin(x)
# 创建误差数组
e = np.full_like(x, y.std())
# 删除每隔一个的错误值
e[range(0, len(x), 2)] = np.nan
plt.errorbar(x, y, e, fmt='-o', ecolor='C1')
英文:
If you specify your error values as an array (instead of a scalar), you can then set the error value to np.nan
at those positions where you don't want to show errorbars:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10.0)
y = np.sin(x)
# make array of errors
e = np.full_like(x, y.std())
# remove every other error value
e[range(0, len(x), 2)] = np.nan
plt.errorbar(x, y, e, fmt='-o', ecolor='C1')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论