Current location - Education and Training Encyclopedia - Graduation thesis - How to draw a list of academic papers in Python
How to draw a list of academic papers in Python
The charts in this paper have various forms, and the commonly used processing tools are excel, MATLAB, Python and so on. Excel's self-processing method has two defects:

1. excel tends to roll its eyes when there are many data.

2. When subplot function or batch processing is needed, it is more convenient to use MATLAB or Python;

3.3. The graphics processed by Excel have a certain distance from the standard of paper charts in aesthetics. Compared with the plot function of MATLAB and Python, Python has a slight advantage from the aesthetic point of view. Here is a brief introduction to the process of extracting data from excel into Python and drawing with Python's matplotlib library.

1. How to import data from Excel into Python:

This section mainly introduces how to import data from Excel into python (the original data may be in. Txt or. Out of the file, and the data is very complicated. You can use MATLAB or python to process the data first and save it in Excel, including some simple calculations and sorting to facilitate later drawing, or directly use python to extract the data in. Txt, not described in detail here). The main method is to use the xlrd library of python, and the process is as follows:

# Import xlrd Library

Import xlrd

# Read the data in the specified Excel file (excel_dir in this case).

data = xlrd . open _ workbook(excel _ dir)

# Read the data in the worksheet (named sheetname here) in the specified data.

Table=data.sheet_by_name (worksheet name)

# Read the data of a column in the table

Needs=table.col_values(0) has several points to explain:

How to define 1.excel_dir and sheetname?

Excel_dir should be written as a string (that is, ""or "") and should end with the suffix of Excel, for example. Xls or. xlsx。 For example, reading a file named result.xls on the administrator's desktop can be written as:

excel _ dir = ' C:UsersAdministratorDesktop

Result. Data of xls = xlrd. Open _ workbook (Excel _ dir) table name is defined in the same way as above, except that the reading method of the table is in index order:

Table = data.sheets()[0] # Obtained in index order.

Table = data.sheet_by_index(0) # obtained in index order 2. How to read the data of a cell or a row?

The process shown is to read the data of a column in the worksheet. Similarly, the data of a row is:

Table.col_values(i) The command to read cell data is:

Table.cell(i,j)。 Value 3. When reading a row or column of data, what is the data area?

For example, when the table in the worksheet is as follows, the data in the 4×3 table will be read.

In other words, if you want to read the data in column B, the code should be written as follows

Table.col_values( 1) Note: python starts counting from 0, so the column number should be 1, and the counting information at this time is: [u'', u'', 4, u''].

Please note that the size of the table is determined by each worksheet. If an excel table has multiple worksheets, the length of the list may be different.

4. What should I do if I want to take out 2-5 rows from a column?

Table. col _ values (I) [ 1: 5] 5。 How to write code if you want to get the last number?

Because the data types of each row/column may be different, there may be a similar situation that the first column has only 4 bits, while the second column has 9 bits, and the most column has 100 bits. If you use the whole column command directly, you will mix u'' in the list, which will lead to the next drawing error. At this time, you can use the following methods:

a_col=table.col_values(i)

A=a_col[0, a_col.index(u'')] However, it should be noted that this command cannot be used to control the number of columns, otherwise an error will be reported, and other judgment statements can be written to determine whether to use this command.

2. How to draw with Python;

This paper introduces the method of drawing with matplotlib library. First, we should import the matplotlib library:

Import numpy as NP import matplotlib. pyplot as PLT drawing method is very simple, and the general process is as follows:

# Drawing command, 1 is the drawing number, and set figsize.

fig_drift= plt.figure( 1,figsize=( 12,4))

# Set the coordinates of data on the X axis and Y axis, as well as attributes such as color and label. Two sets of data are used here.

plt.plot(drift[0],story,“g-”,label='$Damped$ ')

plt.plot(drift[ 1],story," r-",label = ' $ Undamped $ ')

# Set labels for X and Y axes

Plt.xlabel ('drift')

Plt.ylabel ("floor")

# Select Show Data Group Label

Plt. Legend ()

# Set the interval and range of X axis and Y axis.

plt.xticks((0.000,0.005,0.0 10,0.0 15))

Plt.yticks (range (1, 5, 1))

# Set the drawing name

plt.title('minor ')

Among them, there are several points that need to be explained:

1. How to draw multiple graphs?

Very simple, after setting the drawing name, insert:

ax2 = plt.subplot( 132)

The command after plt.sca(ax2) is the same as above. It is worth mentioning that plt.subplot( 13 1) refers to drawing a graph with 1×3 subgraph, ax 1 represents the second graph, and plt.sca(ax2).

2. How to determine which image is endowed with attributes by setting commands such as X axis and Y axis?

Matplotlib directly assigns this attribute to the previous plot object.

3. How to draw a scatter plot?

Just change pl.plot(x, y) to pl.plot(x, y,' o').

4. Other setting parameters:

# Set the upper and lower limits of the X and Y axes.

pl.xlim(i,j)

pl.ylim(m,n)

# Show chart

pl.show()

# Save the chart and automatically save it in png format.

PLT。 SaveFIG (directory+name. PNG, DPI = 600) supports other functions, such as setting X-axis (Y-axis) coordinate display, drawing pie charts, histograms, etc., which will not be introduced here.

3. Say something beside the point:

I once read a sentence: "There is no bad language in the world, only bad people who write languages."

Each language has its own advantages and disadvantages, so I won't comment too much here. How to use language to achieve painting or achieve more goals depends on how we choose. Proper use of for or def, class and other statements can make a piece of code even more powerful. Write it here to remind yourself: when encountering problems, first make clear the methods, form a system, and then enter writing. Don't blindly piece together seemingly correct languages, and the rework rate is extremely high.

For more articles about how to draw academic papers in Python, please pay attention to PHP Chinese website!