How to use Python to draw academic paper charts

高洛峰
Release: 2017-03-08 09:41:07
Original
2156 people have browsed it

The charts in the paper come in various forms. Commonly used processing tools include excel, MATLAB, and Python. The self-processing method of excel has two flaws:

1. When the data When there are too many times, it is easy to "roll your eyes" in excel;

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

3. There is a certain distance between the beauty of the graphs processed by Excel and the standard of the paper graphs. Comparing the plot functions of MATLAB and Python, Python has a slight advantage from the aesthetic point of view of the graph. The following section briefly introduces the process of extracting data from excel into Python and using Python's matplotlib library to draw graphs.

1. How to import data from Excel into Python:

This section mainly introduces how to import data from Excel into Python. Import the data into python (the original data may be in a .txt or .out file, and the data is more complicated. You can first use MATLAB or python to process the data and save it in Excel, including some simple calculations, arrangements, etc., for later convenience drawing, or directly use python to extract the data in .txt (no detailed introduction here), the main method is to use python's xlrd library, the process is as follows:

# 导入xlrd库
import xlrd
# 读取指定Excel文件(此处为excel_dir)中的数据
data=xlrd.open_workbook(excel_dir)
# 读取指定data中某sheet(此处命名为sheetname)中的数据
table=data.sheet_by_name(sheetname)
#读取table中某一列的数据
needs=table.col_values(0)
Copy after login

There are several of them Points that need clarification:

#1. How to define excel_dir and sheetname?

excel_dir should be written in the format of a string (i.e. ' ' or " "), and should end with an Excel suffix such as .xls or .xlsx, such as reading the name on the Administrator desktop The file for result.xls can be written as:

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

The method of defining sheetname is the same as above. In addition, sheet also has indexing Sequential reading method:

table = data.sheets()[0]       #通过索引顺序获取
table = data.sheet_by_index(0) #通过索引顺序获取
Copy after login

2. How to read the data of a certain cell or row?

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

table.col_values(i)
Copy after login

The command to read a certain cell data is:

table.cell(i,j).value
Copy after login

3. Read a row or When selecting a certain column of data, what is the range of the data?

For example: When the table in a sheet is as follows, the data in the 4×3 table will be read.

How to use Python to draw academic paper charts

That is, if you want to read the data in column B, the code should be written as

table.col_values(1)
Copy after login

Note: Python starts counting from 0, so the column number should be 1. The information included at this time is: [u'' ,u'',4,u'']

And note that the dimensions of the table are determined by each sheet, and if an excel table has multiple sheets, the list lengths may be different. .

4. What if you want to get 2-5 rows from a certain column?

table.col_values(i)[1:5]
Copy after login

5. If you want to get the last number, how should you write the code?

Since the data types of each row/column may be different, it may happen that the first column has only 4 numbers, while the second column has 9 numbers, and the highest column has 100 numbers. In similar situations, if you directly use the command to round the column, u'' will be mixed into the list, causing errors in the next drawing. In this case, you can use the following method:

##

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

But please note that this command cannot be used on columns that control the number of rows, otherwise an error will be reported. You can write other judgment statements to identify whether to use this command.

2. How to use Python for drawing:

Here is an introduction to the method of drawing using the matplotlib library. You should first import the matplotlib library:

import numpy as npimport matplotlib.pyplot as plt
Copy after login

The drawing method is very simple, and the general process is as follows :

# 绘图命令,1为图号,并设置figsize
fig_drift= plt.figure(1,figsize=(12,4))
# 设置数据在x轴和y轴的坐标,以及颜色、标签等属性,此处用了两组数据
plt.plot(drift[0], story,"g-",label='$Damped$')
plt.plot(drift[1], story,"r-.",label='$Undamped$')
# 设置x轴和y轴的标签
plt.xlabel('Drift')
plt.ylabel('Storey')
# 选择显示数据组标签
plt.legend()
# 设置x轴和y轴的间隔及范围
plt.xticks((0.000,0.005,0.010,0.015))
plt.yticks(range(1,5,1))
# 设置图名
plt.title('minor')
Copy after login


##A few points that need to be explained:

1. How to draw multiple pictures?

It’s very simple, just after setting the picture number name, insert:

ax2 = plt.subplot(132)
plt.sca(ax2)
Copy after login

后面的命令与前述一致,值得一提的是,plt.subplot(131)指的是绘制一个含有1×3子图的图,且ax1代表的是第2张图,而plt.sca(ax2)表示选择该子图。

2.设置x轴、y轴以及其他命令如何判断是赋予哪张图的属性呢?

matplotlib会直接将该属性赋予上一plot对象。

3.如何绘制散点图?

把pl.plot(x, y)改成pl.plot(x, y, 'o')即可。

4.其他一些设置参数:

# 设置x轴和y轴的上下限
pl.xlim(i, j)
pl.ylim(m,n)
# 显示图
pl.show()
# 保存图,自动存为png格式
plt.savefig(dir+name.png,dpi=600)
Copy after login

除此之外还支持设置x轴(y轴)坐标显示,绘制饼状图、直方图等功能,在此不再一一介绍。

 

三.说点题外话:

曾经看过一句话:“世界上没有烂语言,只有写语言的烂人”。

每种语言各自有其优势,也有其劣势,在此不做过多评价。如何利用语言实现绘图或达到更多目标,在于我们如何选择。恰到好处地使用for或def、class等语句,能使一段代码如虎添翼,写在这里,作为给自己的提醒:遇到问题先明确方法、形成体系再进入编写,不要盲目地将看似正确的语言进行强行拼凑,返工率极高。

更多How to use Python to draw academic paper charts相关文章请关注PHP中文网!

 


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template