Home Backend Development Python Tutorial How to use Python to draw academic paper charts

How to use Python to draw academic paper charts

Mar 08, 2017 am 09:41 AM
python

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中文网!

 


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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the reason why PS keeps showing loading? What is the reason why PS keeps showing loading? Apr 06, 2025 pm 06:39 PM

PS "Loading" problems are caused by resource access or processing problems: hard disk reading speed is slow or bad: Use CrystalDiskInfo to check the hard disk health and replace the problematic hard disk. Insufficient memory: Upgrade memory to meet PS's needs for high-resolution images and complex layer processing. Graphics card drivers are outdated or corrupted: Update the drivers to optimize communication between the PS and the graphics card. File paths are too long or file names have special characters: use short paths and avoid special characters. PS's own problem: Reinstall or repair the PS installer.

How to solve the problem of loading when PS is started? How to solve the problem of loading when PS is started? Apr 06, 2025 pm 06:36 PM

A PS stuck on "Loading" when booting can be caused by various reasons: Disable corrupt or conflicting plugins. Delete or rename a corrupted configuration file. Close unnecessary programs or upgrade memory to avoid insufficient memory. Upgrade to a solid-state drive to speed up hard drive reading. Reinstalling PS to repair corrupt system files or installation package issues. View error information during the startup process of error log analysis.

How to solve the problem of loading when the PS opens the file? How to solve the problem of loading when the PS opens the file? Apr 06, 2025 pm 06:33 PM

"Loading" stuttering occurs when opening a file on PS. The reasons may include: too large or corrupted file, insufficient memory, slow hard disk speed, graphics card driver problems, PS version or plug-in conflicts. The solutions are: check file size and integrity, increase memory, upgrade hard disk, update graphics card driver, uninstall or disable suspicious plug-ins, and reinstall PS. This problem can be effectively solved by gradually checking and making good use of PS performance settings and developing good file management habits.

How to use mysql after installation How to use mysql after installation Apr 08, 2025 am 11:48 AM

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

How does PS feathering control the softness of the transition? How does PS feathering control the softness of the transition? Apr 06, 2025 pm 07:33 PM

The key to feather control is to understand its gradual nature. PS itself does not provide the option to directly control the gradient curve, but you can flexibly adjust the radius and gradient softness by multiple feathering, matching masks, and fine selections to achieve a natural transition effect.

How to optimize database performance after mysql installation How to optimize database performance after mysql installation Apr 08, 2025 am 11:36 AM

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

What should I do if the PS card is in the loading interface? What should I do if the PS card is in the loading interface? Apr 06, 2025 pm 06:54 PM

The loading interface of PS card may be caused by the software itself (file corruption or plug-in conflict), system environment (due driver or system files corruption), or hardware (hard disk corruption or memory stick failure). First check whether the computer resources are sufficient, close the background program and release memory and CPU resources. Fix PS installation or check for compatibility issues for plug-ins. Update or fallback to the PS version. Check the graphics card driver and update it, and run the system file check. If you troubleshoot the above problems, you can try hard disk detection and memory testing.

See all articles