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.
** 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)
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