Draw 2D XY data with an error bar using the ** errorbar ** method of matplotlib.pyplot. As an example, an error bar of 5% for the x value and 20% for the y value is added.
import numpy as np
import matplotlib.pyplot as plt
x_list=[] # x_define list(Create an empty list)
y_list=[] # y_define list
f=open('tst_XY.dat','rt') # tst_XY.r the file named dat(Read) t(text)Read in mode
##Less than,Read the data, x_list and y_Store the value in list
for line in f:
data = line[:-1].split(' ')
x_list.append(float(data[0]))
y_list.append(float(data[1]))
##
plt.plot(x_list, y_list,'o',color='red') #Raw data plot
plt.xlabel('X-axis',fontsize=18) #x-axis label
plt.ylabel('Y-axis',fontsize=18) #y-axis label
#Other drawing options
plt.xticks(np.arange(0,901,300),fontsize=18)
plt.yticks(np.arange(40,180,30),fontsize=18)
plt.grid(True)
#Add an error bar
yerr_list=[]
error_y=20 # error (%):20 to y value%Add an error
error_x=5 # error (%):5 to x value%Add an error
for i in range(len(x_list)):
yerr_list=(error_y/100.0)*y_list[i] #20 to y value%Yerr with error_Store in list
xerr_list=(error_x/100.0)*x_list[i] #5 to x value%Add error xerr_Store in list
plt.errorbar(x_list,y_list,xerr=xerr_list, yerr=yerr_list,fmt='ro',ecolor='blue',capsize=4.0) #Illustration of error bar
plt.show()
0.0 164.26
54.2 137.98
106.2 124.84
142.3 118.27
187.5 111.70
317.1 98.56
530.4 85.42
688.8 78.84
900.7 72.27