Home > OS >  How do I include error bars on multple line plots in matplotlib
How do I include error bars on multple line plots in matplotlib

Time:03-19

I want to plot the error bars using on the multiple line plots with the data in this code. I have encountered many bugs which makes it impossible for the code to run e.g the turple is out of range.

    from numpy import *
    import math
    import seaborn as sns
    import numpy as np
    import matplotlib.pyplot as plt
    import  matplotlib.colors as mcolors
    from matplotlib.pyplot import figure
    from matplotlib.ticker import(MultipleLocator, FormatStrFormatter, AutoMinorLocator)

    t = np.array([0.5,1, 2, 5, 10, 19]) 
    raw_1 =np.array([0.82, 0.84, 0.86, 0.88, 0.89, 0.895] )
    raw_2 = np.array([0.79, 0.83, 0.84, 0.845, 0.85, 0.865])
    raw_3 = np.array([0.80, 0.81,0.83,0.845, 0.853, 0.80])   
  
    plt.xticks(t, t)

    plt.figure(figsize=(8, 6), dpi=150)
    plt.xlabel("Temporal Context length(s) seconds")
    plt.ylim(0.76, 0.90)
    plt.xlim(0.5, 19)
    plt.ylabel("Balanced Accuracy")

    plt.xticks(t, t)

    plt.xticks(t) 
    plt.errorbar(t, raw_1,yerr=np.std(raw_1, axis = 1),color='r',marker='o')                    
    plt.errorbar(t, raw_2 , yerr= np.std(raw_2, axis = 1)color='g', marker='x')  
    plt.errorbar(t, raw_3 , yerr= np.std(raw_3, axis = 1),color='b')
    plt.gca().set_xscale('log')
    plt.xticks(t,t)
    plt.legend()
    plt.savefig('context_bb.png',bbox_inches='tight')
    plt.show()

CodePudding user response:

As mentioned in the comment, I would advice to calculate the mean and standard deviation of the data and use this result in the errorbar

import numpy as np
import matplotlib.pyplot as plt

t = np.array([0.5, 1, 2, 5, 10, 19])
raw_1 = np.array([0.82, 0.84, 0.86, 0.88, 0.89, 0.895])
raw_2 = np.array([0.79, 0.83, 0.84, 0.845, 0.85, 0.865])
raw_3 = np.array([0.80, 0.81, 0.83, 0.845, 0.853, 0.80])

# combine data into single matrix
data = np.stack((raw_1, raw_2, raw_3))
# calculate mean and standard deviation
data_mean = data.mean(axis=0)
data_std = data.std(axis=0)

# plot mean with standard deviations as errorbars
plt.errorbar(t, data_mean, yerr=data_std, label='mean and standard deviation')
plt.show()

CodePudding user response:

Based on your example, with small changes and corrections you could do the following:

import numpy as np
import matplotlib.pyplot as plt

t = np.array([0.5, 1, 2, 5, 10, 19])
raw_1 = np.array([0.82, 0.84, 0.86, 0.88, 0.89, 0.895])
raw_2 = np.array([0.79, 0.83, 0.84, 0.845, 0.85, 0.865])
raw_3 = np.array([0.80, 0.81, 0.83, 0.845, 0.853, 0.80])

plt.figure(figsize=(8, 6), dpi=150)
plt.xlabel("Temporal Context length(s) seconds")
plt.ylim(0.76, 0.90)
plt.xlim(0.5, 19)
plt.ylabel("Balanced Accuracy")

plt.errorbar(t, raw_1, yerr=np.std(raw_1, axis=0), color='r', marker='o')
plt.errorbar(t, raw_2, yerr=np.std(raw_2, axis=0), color='g', marker='x')
plt.errorbar(t, raw_3, yerr=np.std(raw_3, axis=0), color='b')
plt.gca().set_xscale('log')
plt.xticks(t, t)
plt.savefig('context_bb.png', bbox_inches='tight')
plt.show()

enter image description here

  • Related