How to Use Python to draw academic papers and charts,

Source: Internet
Author: User
Tags sca

How to Use Python to draw academic papers and charts,

The charts in the thesis are various in forms. Commonly Used processing tools include excel, MATLAB and Python. The excel self-processing method has two defects: 1. when there is a large amount of data, it is easy to see the excel "White-Eye" phenomenon; 2. it is more convenient to use MATLAB or Python when subplot function or batch processing is required; 3. in terms of appearance, excel Charts are relatively different from the chart standards. Compared with the plot functions of MATLAB and Python, Python is slightly dominant in terms of the appearance of graphs, the following section briefly introduces the process of extracting data from excel to Python and plotting using the matplotlib library of Python.

 

1. How to import data in Excel to Python:

The main method is to use the python xlrd library. The process is as follows:

# Import xlrd library import xlrd # Read data = xlrd in the specified Excel file (excel_dir here. open_workbook (excel_dir) # Read the data table = data in a sheet (named sheetname here) in the specified data. sheet_by_name (sheetname) # read data from a column in table needs = table. col_values (0)

There are several points to note:

1. How are excel_dir and sheetname defined?

Excel_dir should be written as a string (that is, ''or" "delimiter files can be written:

excel_dir = 'C:\Users\Administrator\Desktop\result.xls'data=xlrd.open_workbook(excel_dir)

The sheetname is defined in the same way as the preceding method. In addition, sheet also reads data through the index sequence:

Table = data. sheets () [0] # obtain table = data. sheet_by_index (0) in the order of indexes # obtain table = data. sheet_by_index (0) in the order of Indexes

2. How to read data from a cell or row?

The process shows how to read the data of a column in a sheet. Similarly, the data of a row is:

table.col_values(i)

To read data from a cell, run the following command:

table.cell(i,j).value

3. What is the data range when reading data from a row or column?

For example, if the table in a sheet is as follows, data in Table 4 × 3 is read.

 

 

 

 

 

 

If you want to read Column B data, the code should be written

table.col_values(1)

Note: python starts counting from 0, so column number should be 1. The calculation information is: [u'', u'', 4, u'']

Note that the table dimension is determined by each sheet. If an excel table has multiple sheets, the list length may vary.

4. What should I do if I want to retrieve 2-5 rows of a column?

table.col_values(i)[1:5]

5. How do I write code if I want to get the last number?

Because the data types of each row/column may be different, the first column may have only four numbers, while the second column has nine numbers, A maximum of 100 numbers in a column is similar. If you directly use the command to retrieve the entire column, u'' will be added to the list, leading to an error in the next plot, you can use the following method:

a_col=table.col_values(i)a=a_col[0,a_col.index(u'')]

Note that this command cannot be used for columns that control the number of rows. Otherwise, an error is reported. You can write other judgment statements to identify whether to use this command.

 

Ii. How to plot using Python:

This section describes how to use the matplotlib library for plotting. First, import the matplotlib Library:

import numpy as npimport matplotlib.pyplot as plt

The plotting method is simple. The general process is as follows:

# Drawing command, 1 is the figure number, and set figsizefig_drift = plt. figure (1, figsize = () # sets the coordinates of the data on the X and Y axes, as well as attributes such as color and tag. Two groups of data plt are used here. plot (drift [0], story, "g-", label = '$ Damped $') plt. plot (drift [1], story, "r -. ", label = '$ Undamped $') # sets the label plt for the X and Y axes. xlabel ('drift ') plt. ylabel ('store') # select show data group label plt. legend () # Set the gap between the X axis and the Y axis and the range plt. xticks (0.000, 0.005, 0.010, 0.015) plt. yticks (range (, 1) # sets the image name plt. title ('minor ')

Note the following:

1. How to draw multiple charts?

It is very easy to insert after setting the image number Name:

ax2 = plt.subplot(132)plt.sca(ax2)

The subsequent commands are consistent with those mentioned above. It is worth mentioning that plt. subplot (131) refers to drawing a graph containing a 1 × 3 subgraph, and ax2 represents 2nd graphs, while plt. sca (ax2) indicates selecting this subgraph.

2. Set the x-axis, Y-axis, and other commands. How can I determine the attributes of the graph to be attached?

Matplotlib will directly assign this property to the previous plot object.

3. How to draw a scatter chart?

Change pl. plot (x, y) to pl. plot (x, y, 'O.

4. Other parameters:

# Set the upper and lower limits of the X and Y axes pl. xlim (I, j) pl. ylim (m, n) # show graph pl. show () # Save graph, automatically save it as the pngformat plt.savefig(dir1_name.png, dpi = 600)

In addition, you can also set the X axis (Y axis) Coordinate display, and draw pie charts and histograms.

 

Iii. digress:

I have read one sentence: "There are no bad languages in the world, but only bad people who write languages ".

Each language has its own advantages and disadvantages, so we will not comment too much here. How to use a language to draw or achieve more targets lies in how we choose. The proper use of for, def, class, and other statements can make a piece of code even more powerful, written here, as a reminder to yourself: in case of problems, first clarify the method, form a system, and then enter the writing, do not forcibly splice seemingly correct languages. The return rate is extremely high.

After all, the first article is a bit excited. Let's talk about our team members (although there are only two people): Forget it.

 

If you have any questions about the above content, please contact excelting@qq.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.