There are two defects in Excel self-processing methods, such as Excel, Matlab and Python, which are widely used in the paper .
1. When the data is more, Excel "pounder" phenomenon is easy to appear;
2. It is more convenient to use MATLAB or Python when using subplot function or batch processing;
3.excel processing of the graph in the aesthetic degree than the paper standard has a certain distance. Comparing the plot functions of Matlab and Python, Python has a slight advantage from an aesthetically pleasing perspective, with a brief introduction to extracting data from Excel into Python and using Python's matplotlib library mapping process.
one. How to import data from Excel into Python:
This section mainly describes how to import data from Excel into Python (the original data may be in a. txt or. out file, and the data is more complex, you can first use MATLAB or Python to process the data into Excel, including some simple calculations, permutations, etc., In order to facilitate subsequent drawing, or directly using Python to extract the data in. txt, this is not described in detail), the main method is to use the Python xlrd library, the process is as follows:
# import XLRD Libraries import xlrd# read data from the specified Excel file (Excel_dir here) Data=xlrd.open_workbook (EXCEL_DIR) # Reads a sheet in the specified data ( Data in the name SheetName) table=data.sheet_by_name (sheetname) #读取table中某一列的数据needs =table.col_values (0)
There are several points to note:
How are 1.excel_dir and SheetName defined?
Excel_dir should be written in the form of a string (that is, "or") and end up with an Excel suffix such as. xls or. xlsx, such as reading a file named Result.xls on the administrator desktop that can be written as:
Excel_dir = ' C:\Users\Administrator\Desktop\result.xls ' Data=xlrd.open_workbook (excel_dir)
SheetName defines the same method as above, in addition to the sheet also have the method of reading by index order:
Table = data.sheets () [0] #通过索引顺序获取table = data.sheet_by_index (0) #通过索引顺序获取
2. How can I read data from a cell or a row?
The process shows the reading of data from a column in a sheet, with the same line of data as:
Table.col_values (i)
The command to read a cell data is:
Table.cell (i,j). Value
3. What is the range of data when reading a row or column of data?
For example, when the table in a sheet is as follows, the data in the 4X3 table is read.
Even if want to read column B data, the code should be written as
Table.col_values (1)
Note: Python is counted from 0, so column number should be 1, at which point the information included is: [U ', U ', 4,u ']
Note that table dimensions are determined by each sheet, and if an Excel table has multiple sheet, a different list length may occur.
4. What do I do if I want to take a column of 2-5 rows?
Table.col_values (i) [1:5]
5. How do I write code if I want to take the last number?
Because the data types of each row/column may be different, the first column may appear only 4 digits, and the second column has 9 numbers, and the most of the column has 100 numbers, and so on, if the direct use of the command to take an entire column, you will be added to the list of ", resulting in the next drawing error, you can use the following methods
A_col=table.col_values (i) A=a_col[0,a_col.index (U ")
However, it is important to note that this command cannot be used for columns that control the number of rows, otherwise an error will be made, and other judgment statements can be written to identify whether the command is used.
Two. How to draw with Python:
This article describes the method of drawing with the Matplotlib library, which should first be imported into the Matplotlib library:
Import NumPy as Npimport Matplotlib.pyplot as Plt
The drawing method is simple and the approximate process is as follows:
# Drawing Command, 1 is the chart number, and set figsizefig_drift= plt.figure (1,figsize= (12,4)) # Set the coordinates of the data on the X and Y axes, as well as the colors, labels and other properties, where two sets of data are used Plt.plot (Drift[0] , Story, "G", Label= ' $Damped $ ') plt.plot (Drift[1], story, "R.", Label= ' $Undamped $ ') # set the x-axis and y-axis label Plt.xlabel (' Drift ') Plt.ylabel (' Storey ') # Select Show Data Group label Plt.legend () # Set the interval and range of the x and Y axes plt.xticks ((0.000,0.005,0.010,0.015)) Plt.yticks (range ( 1,5,1) # Set the map name Plt.title (' minor ')
Some of the points that need to be explained:
1. How do I draw multiple graphs?
Very simple, just after setting the name of the diagram, insert:
AX2 = Plt.subplot (Plt.sca) (AX2)
The following commands are consistent with the foregoing, and it is worth mentioning that Plt.subplot (131) refers to drawing a graph with a 1x3, and Ax1 represents the 2nd graph, and Plt.sca (AX2) represents the selection of the sub-graph.
2. Set the x-axis, y-axis, and other commands how do you determine which graph is given the attribute?
Matplotlib directly assigns the property to the previous plot object.
3. How do I plot a scatter plot?
Change Pl.plot (x, y) to Pl.plot (x, y, ' o ').
4. Some other setup parameters:
# set the upper and lower bounds of the x and Y axes Pl.xlim (i, J) Pl.ylim (m,n) # Show Graph Pl.show () # Save diagram, automatically save as PNG format Plt.savefig (dir+name.png,dpi=600)
In addition, it also supports setting x-axis (y-axis) coordinate display, drawing pie chart, histogram and other functions, which are not introduced here.
Three. Say humorous digression:
Once read a word: "There is no rotten language in the world, only the rotten person who writes the language."
Each language has its own advantages and disadvantages, and does not make much of a comment here. How to use the language to achieve drawing or achieve more goals is how we choose. Just right to use for or Def, class and other statements, can make a piece of code, written here, as a reminder to yourself: encounter problems first Clear method, form a system to enter the writing, do not blindly will appear to be the correct language forcibly pieced together, rework rate is very high.