[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars

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

result

t.png

Contents of the "tst_XY.dat" file used in the example

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

Recommended Posts

[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
[Scientific / technical calculation by Python] Logarithmic graph, visualization, matplotlib
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib
[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
Create 3D scatter plot with SciPy + matplotlib (Python)
3D plot with matplotlib
[Scientific / technical calculation by Python] 3rd order spline interpolation, scipy
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
Create plot animation with Python + Matplotlib
Lognormal probability plot with Python, matplotlib
[Scientific / technical calculation by Python] Numerical integration, trapezoidal / Simpson law, numerical calculation, scipy
[Scientific / technical calculation by Python] Solving simultaneous linear equations, numerical calculation, numpy
[Scientific / technical calculation by Python] 1D-3D discrete fast Fourier transform, scipy
[Scientific / technical calculation by Python] Monte Carlo simulation of thermodynamics of 2D Ising spin system by Metropolis method
Scientific / technical calculation by Python] Drawing and visualization of 3D isosurface and its cross section using mayavi
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
Plot CSV of time series data with unixtime value in Python (matplotlib)
Organize data divided by folder with Python
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
[Scientific / technical calculation by Python] Solving second-order ordinary differential equations by Numerov method, numerical calculation
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
I wanted to visualize 3D particle simulation with the Python visualization library Matplotlib.
[Scientific / technical calculation by Python] Solving one-dimensional Newton equation by the 4th-order Runge-Kutta method
Visualize corona infection data in Tokyo with matplotlib
[Python] limit axis of 3D graph with Matplotlib
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Folium: Visualize data on a map with Python
Visualize grib2 on a map with python (matplotlib)
python --Export 2D histogram as an array by Matplotlib
Try to operate DB with Python and visualize with d3
[Python] How to create a 2D histogram with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
[Scientific / technical calculation by Python] Numerical solution of second-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] List of matrices that appear in Hinpan in numerical linear algebra
[Scientific / technical calculation by Python] List of usage of (special) functions used in physics by using scipy
[Scientific and technical calculation by Python] Drawing of fractal figures [Sierpinski triangle, Bernsley fern, fractal tree]
[Scientific / technical calculation by Python] Wave "beat" and group velocity, wave superposition, visualization, high school physics
[Scientific / technical calculation by Python] Numerical solution of one-dimensional harmonic oscillator problem by velocity Verlet method
[Scientific / technical calculation by Python] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra
Data analysis with python 2
2-axis plot with Matplotlib
Visualize data with Streamlit
Heatmap with Python + matplotlib
2D plot in matplotlib
Data analysis with Python
Numerical calculation with Python