pythonmatplotliberrorbar

plot errorbars but bars for only selected x-values


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

Solution

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

    enter image description here