search
HomeBackend DevelopmentPython TutorialHow to use Python to draw academic paper charts

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)

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)

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) #通过索引顺序获取

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)

The command to read a certain cell data is:

table.cell(i,j).value

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)

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]

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'')]

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

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')


##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)

后面的命令与前述一致,值得一提的是,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)

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

 

三.说点题外话:

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

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

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

 


Statement
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
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),