[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file

Create a basic 2D graph using matplotlib.

Status: ● XY data is stored in a file named testing_plot.dat ● I want to read data from that file and plot it ● In some cases, you want to shape the axis by giving it a name.

"""
Read data from a file and plot it
"""
import matplotlib.pyplot as plt  #Import maplotlib pyplot with the name plt


x_list=[] # x_define list(Create an empty list)
y_list=[] # y_define list

f=open('testing_plot.dat','rt') #R the file that contains the data you want to plot(Read) t(text)Read in mode

##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]))
##

###draw
plt.plot(x_list, y_list) #Data specification for plotting:Here x on the x-axis_list, y on y axis_Specify list. Line graph
plt.plot(x_list, y_list,color='RED',linewidth=4.0) #Output in red.Line thickness 4.0pt
#plt.plot(x_list, y_list,marker='o') #Make lines and dots
#plt.plot(x_list, y_list,'o') #Make a point
 
plt.xlabel('X ') #x-axis label
plt.ylabel('Y') #y-axis label
#plt.legend(loc='best') # legend

#Other drawing options
plt.xticks(fontsize=7) 
plt.yticks(fontsize=7) 
plt.grid(True) #Create a frame for the graph

#plt.xlim(xmin, xmax) #The range of x to draw[xmin,xmax]To
#plt.ylim(ymin, x¥ymax) #The range of y to draw[ymin,ymax]To
#plt.hlines([y1,y2], xmin, xmax, linestyles="dashed")  # y=Draw dashed lines on y1 and y2



plt.show()    #Output the drawing result. Be sure to write.

result

スクリーンショット 2017-07-18 16.23.51.png


Addendum: Optional arguments and abbreviations for plot () (hereafter, the strings in parentheses are equivalent)

** alpha **: Specify transparency as a decimal ** color (c) **: Specify the color with a character string. red (r), blue (b), etc. ** linestyle (ls) **: Specify the line style. '-', '--', ':'Such. ** linewidth (lw) **: Specify line thickness ** marker **: Specify the marker type. '+',',','.', '1', '2', etc. ** markerfacecolor (mfc) **: Specify by a character string of the color inside the marker. ** markersize (ms) ** Specify the marker size. Boundary thickness can be specified with marker edgewidth (mew). ** antialiased (aa) ** Specify antialiasing processing (set aa = True or aa = False)


[Supplement] The XY dataset used in the example: Save it as testing_plot.dat.

0.0 164.26 27.9 147.83 35.7 144.55 44.4 141.26 54.2 137.98 65.1 134.69 77.3 131.41 90.9 128.12 106.2 124.84 123.2 121.55 142.3 118.27 163.6 114.98 187.5 111.70 214.4 108.41 244.6 105.13 278.6 101.84 317.1 98.56 360.6 95.27 410.0 91.99 466.2 88.70 530.4 85.42 604.0 82.13 688.8 78.84 786.8 75.56 900.7 72.27 1033.8 68.99 1190.4 65.70

Recommended Posts

[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
[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] 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] Vector field visualization example, electrostatic field, matplotlib
Scientific / technical calculation by Python] Drawing and visualization of 3D isosurface and its cross section using mayavi
[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
[Scientific / technical calculation by Python] Monte Carlo simulation of thermodynamics of 2D Ising spin system by Metropolis method
[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
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
Read line by line from a file with Python
[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
Visualization of data by prefecture
Python application: data visualization # 2: matplotlib
[Scientific / technical calculation by Python] 3rd order spline interpolation, scipy
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
Python> Read from a multi-line string instead of a file> io.StringIO ()
[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
[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
Impressions of touching Dash, a data visualization tool made by python
[Python] Visualization of longitudinal data (plot, boxplot, violin plot, confidence interval, histogram)
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
Plot CSV of time series data with unixtime value in Python (matplotlib)
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Implementation example] Read the file line by line with Cython (Python) from the last line
Example of 3D skeleton analysis by Python
Read the file line by line in Python
Read the file line by line in Python
Recommendation of Altair! Data visualization with Python
[Scientific / technical calculation by Python] Solving the boundary value problem of ordinary differential equations in matrix format, numerical calculation
[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
Python --Read data from a numeric data file and find the multiple regression line.
[Scientific / technical calculation by Python] Solving one-dimensional Newton equation by the 4th-order Runge-Kutta method
[Python] Plot data by prefecture on a map (number of cars owned nationwide)
[Python] limit axis of 3D graph with Matplotlib
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Read table data in PDF file with Python
[Python] How to read data from CIFAR-10 and CIFAR-100
Real-time visualization of thermography AMG8833 data in Python
[Scientific / technical calculation by Python] Numerical solution of one-dimensional and two-dimensional wave equations by FTCS method (explicit method), hyperbolic partial differential equations
Calculation of technical indicators by TA-Lib and pandas
[Scientific / technical calculation by Python] Generation of non-uniform random numbers giving a given probability density function, Monte Carlo simulation
Create 3D scatter plot with SciPy + matplotlib (Python)