绘制误差线,但仅针对选定的 x 值绘制条形图。

huangapple go评论68阅读模式
英文:

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')

绘制误差线,但仅针对选定的 x 值绘制条形图。

英文:

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')

绘制误差线,但仅针对选定的 x 值绘制条形图。

huangapple
  • 本文由 发表于 2023年6月19日 11:30:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定