SciencePlots basic syntax and features

Introduction

Users sometimes need to make customized modifications to layer attributes such as fonts, scale axes, spines, legends, etc. according to the journal's drawing requirements. This is time-consuming and can easily lead to users overlooking some layer detail requirements.

SciencePlots, as a third-party extension toolkit specifically used for drawing scientific research papers, provides Matplotlib Styles for mainstream English scientific and technological journals (such as Nature, Science, IEEE, etc.).

The installation code for SciencePlots is as follows:

pip install SciencePlots

Install LaTeX

In order to better display the illustrations of academic papers and facilitate subsequent printing, the font style in the illustrations is generally required to be written in LaTeX, and SciencePlots can easily implement this requirement. The SciencePlots library implements the LaTeX writing style requiring users to install LaTeX on their computers .

For the installation steps of other types of operating systems, just refer to the official SciencePlots tutorial.

  1. Installing MikTex and Ghostscript
    ScienePlots library officially recommends users to use MikTex software to install LaTeX. Users can download the latest version directly from the MikTex official website and install it. Ghostscript is a set of free software compiled based on Adobe, PostScript and Portable Document Format (PDF) page description languages. Users can download the latest version from its official website and install it.
  2. Add the installation path of the software to the system environment variable
    . After installing the above two software, the user also needs to add their installation path to the system environment variable, specifically " \...\miktex\bin\x64”and “\...\gs__( 版本号)\bin”." After adding the system environment variable, restart, related The configuration will take effect.

SciencePlots plot example

If the journal the reader submits to has special font requirements, the reader can set not to use LaTeX drawing:: plt.style.use(['science',' no-latex']).

The following figure shows examples of various plotting styles in SciencePlots.
(a) is the default color theme and plotting style of Matplotlib.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


data = pd.read_excel(r"\分组误差线图构建.xlsx")

#(a)Matplotlib的默认颜色主题和绘图风格
selsect = ["A","B","C","D"]
colors = ["#2FBE8F","#459DFF","#FF5B9B","#FFCC37"]
fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")

for index,color in zip(selsect,colors):
    data_selcet = data.loc[data['type']==index,:]
    ax.errorbar(x=data_selcet["time"],y=data_selcet["mean"],yerr=data_selcet["sd"],
                linewidth=1,marker='o',ms=10,mew=1,mec='k',capsize=5,label=index)
    ax.legend()
    ax.set(xlabel='Time', ylabel='Values',
           xlim=(-2,40),ylim=(-8,30))

plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 ScienecePlots_matplotlib.png', 
         bbox_inches='tight',dpi=600)
plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 ScienecePlots_matplotlib.pdf', 
         bbox_inches='tight') 
plt.show()

(b) Plot results for Science series journal style

#(b)Science系列期刊风格绘制结果

selsect = ["A","B","C","D"]
colors = ["#2FBE8F","#459DFF","#FF5B9B","#FFCC37"]
plt.style.use('science')
fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
for index,color in zip(selsect,colors):
    data_selcet = data.loc[data['type']==index,:]
    ax.errorbar(x=data_selcet["time"],y=data_selcet["mean"],yerr=data_selcet["sd"],
                linewidth=1,marker='o',ms=10,mew=1,mec='k',capsize=5,label=index)
    ax.legend()
    ax.set(xlabel='Time', ylabel='Values',
           xlim=(-2,40),ylim=(-8,30))
plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_science.png', 
         bbox_inches='tight',dpi=600)
plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_science.pdf', 
         bbox_inches='tight') 
plt.show()

(c) Plot the results for IEEE journal style

#(c)IEEE期刊风格绘制结果
selsect = ["A","B","C","D"]
colors = ["#2FBE8F","#459DFF","#FF5B9B","#FFCC37"]
plt.style.use(['science','ieee'])
fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
for index,color in zip(selsect,colors):
    data_selcet = data.loc[data['type']==index,:]
    ax.errorbar(x=data_selcet["time"],y=data_selcet["mean"],yerr=data_selcet["sd"],
                linewidth=1,marker='o',ms=10,mew=1,mec='k',capsize=5,label=index)
    ax.legend()
    ax.set(xlabel='Time', ylabel='Values',
           xlim=(-2,40),ylim=(-8,30))
plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_ieee.png', 
         bbox_inches='tight',dpi=600)
plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_ieee.pdf', 
         bbox_inches='tight') 
plt.show()

(d) Plot the results for Nature journal style

#(d)Nature期刊风格绘制结果
colors = ["#2FBE8F","#459DFF","#FF5B9B","#FFCC37"]
selsect = ["A","B","C","D"]
plt.style.use(['science','nature'])
fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
for index,color in zip(selsect,colors):
    data_selcet = data.loc[data['type']==index,:]
    ax.errorbar(x=data_selcet["time"],y=data_selcet["mean"],yerr=data_selcet["sd"],
                linewidth=1,marker='o',ms=10,mew=1,mec='k',capsize=5,label=index)
    ax.legend()
    ax.set(xlabel='Time', ylabel='Values',
           xlim=(-2,40),ylim=(-8,30))

plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_nature.png', 
         bbox_inches='tight',dpi=600)
plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_nature.pdf', 
         bbox_inches='tight') 
plt.show()

(e) Drawing style for Science journal using vibrant color theme

#(e)使用了vibrant 颜色主题的Science期刊绘图风格
selsect = ["A","B","C","D"]
plt.style.use(['science','vibrant'])
fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
for index,color in zip(selsect,colors):
    data_selcet = data.loc[data['type']==index,:]
    ax.errorbar(x=data_selcet["time"],y=data_selcet["mean"],yerr=data_selcet["sd"],
                linewidth=1,marker='o',ms=10,mew=1,mec='k',capsize=5,label=index)
    ax.legend()
    ax.set(xlabel='Time', ylabel='Values',
           xlim=(-2,40),ylim=(-8,30))

plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_vibrant.png', 
         bbox_inches='tight',dpi=600)
plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_vibrant.pdf', 
         bbox_inches='tight') 
plt.show()

(f) Drawing style for Science journal using bright color theme

#(f)使用了bright颜色主题的Science期刊绘图风格
selsect = ["A","B","C","D"]
plt.style.use(['science','bright'])
fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
for index,color in zip(selsect,colors):
    data_selcet = data.loc[data['type']==index,:]
    ax.errorbar(x=data_selcet["time"],y=data_selcet["mean"],yerr=data_selcet["sd"],
                linewidth=1,marker='o',ms=10,mew=1,mec='k',capsize=5,label=index)
    ax.legend()
    ax.set(xlabel='Time', ylabel='Values',
           xlim=(-2,40),ylim=(-8,30))
           
plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_bright.png', 
         bbox_inches='tight',dpi=600)
plt.savefig('\第2章 绘制工具及其重要特征\图2-3-8 SciencePlots_bright.pdf', 
         bbox_inches='tight') 
plt.show()

For more drawing styles, please refer to the SciencePlots official website.

Tip: The SciencePlots library not only provides drawing style templates for mainstream English science and technology journals, but also enables the mixed use of different drawing styles. In addition, when using the drawing style of this library, readers can plt.style.use('science')set the global drawing style through or use the following statement to temporarily use the drawing style.

with plt.style.context('science '):
	plt.figure()
	plt.plot(x,y)
	plt.show()

It is recommended to use global settings because when using temporary drawing styles, especially when using LaTeX characters, the LaTeX character style will not be used when drawing legends, axis labels and other layer attributes, causing overall inconsistency in the drawing results. The way to introduce SciencePlots drawing theme styles may vary with version updates. Readers should check the SciencePlots official website to use the latest introduction method.

Reference books: Ning Haitao. Guide to illustrating scientific research papers - based on Python[M]. Beijing: People's Posts and Telecommunications Press, 2023: 42-44.

Guess you like

Origin blog.csdn.net/m0_52316372/article/details/132526000