工具包 | 使用Python绘制Sci学术期刊配图
之前还在讨论Matplotlib没有很好的第三方主题库呢?这不,又被我发现了一个宝藏库,还专门用于一些学术期刊的图表发表,可谓是弥补了matplotlib 繁琐的自定义设置。好了,话不多说,今天这篇推文的主角就是Github 上拥有1.6k 星之多的科学论文图表绘制库「SciencePlots」。推文的主要内如下:
- SciencePlots 库介绍
- SciencePlots 实例演示
SciencePlots 库介绍
使用Python-matplotlib绘制科研图表,其默认的颜色和格式并不能满足一般的期刊要求,若想要符合要求,就必须自定义设置,而这个步骤在对面对多幅图表时就显得繁琐和重复。在之前的几篇科学图表绘制推文中Python-matplotlib 学术散点图 EE 统计及绘制 Python-matplotlib 横向堆积柱状图绘制 就定制化表格编写了较多定制化代码。而SciencePlots就是为解决科研图表繁琐设置而定制的一系列科研绘图样式库,可以绘制很合适科研发表的图表。
安装
我们直接可使用pip 直接进行安装,但想要安装最新版本可以采用如下方式:
pip install git+https://github.com/garrettj403/SciencePlots.git
如使用pip稳定安装,则直接使用如下代码:
# for lastest release
pip install SciencePlots
主要样式
SciencePlots 提供了多种用于不同期刊发表要求的主题,接下来我们主要介绍几种主题,注意:由于SciencePlots默认使用Latex字体渲染,而相应软件安装较为麻烦,以下样例均采用禁用Latex字体渲染,即设置 no-latex (数据还是使用官网提供的):
数据构建:
import numpy as np
import matplotlib.pyplot as plt
def model(x, p):
return x ** (2 * p + 1) / (1 + x ** (2 * p))
x = np.linspace(0.75, 1.25, 201)
「原始主题」
fig, ax = plt.subplots(figsize=(4,3),dpi=200)
for p in [10, 15, 20, 30, 50, 100]:
ax.plot(x, model(x, p), label=p)
ax.legend(title='Order')
ax.set(xlabel='Voltage (mV)')
ax.set(ylabel='Current ($mu$A)')
ax.autoscale(tight=True)
fig.savefig(r'F:DataCharm学术图表绘制Python-matplotlibSciencePlotsfig1.png',
width=4,height=3,dpi=600)
结果如下:
「science」
with plt.style.context(['science','no-latex']):
fig, ax = plt.subplots(figsize=(4,3),dpi=200)
for p in [10, 15, 20, 30, 50, 100]:
ax.plot(x, model(x, p), label=p)
ax.legend(title='Order')
ax.set(xlabel='Voltage (mV)')
ax.set(ylabel='Current ($mu$A)')
ax.autoscale(tight=True)
fig.savefig(r'F:DataCharm学术图表绘制Python-matplotlibSciencePlotsfig1_science.png',
width=4,height=3,dpi=600)
结果如下:
可以发现:刻度、颜色和图例等发生明显改变。
「IEEE」
with plt.style.context(['ieee','no-latex']):
fig, ax = plt.subplots(figsize=(4,3),dpi=200)
for p in [10, 20, 50]:
ax.plot(x, model(x, p), label=p)
ax.legend(title='Order')
ax.set(xlabel='Voltage (mV)')
ax.set(ylabel='Current ($mu$A)')
ax.autoscale(tight=True)
fig.savefig(r'F:DataCharm学术图表绘制Python-matplotlibSciencePlotsfig1_ieee.png',
width=4,height=3,dpi=600)
结果如下:
当然你还可以进行主题组合:「dark_background+science+high-vis」
with plt.style.context(['dark_background', 'science', 'high-vis','no-latex']):
fig, ax = plt.subplots(figsize=(4,3),dpi=200)
for p in [10, 15, 20, 30, 50, 100]:
ax.plot(x, model(x, p), label=p)
ax.legend(title='Order')
ax.set(xlabel='Voltage (mV)')
ax.set(ylabel='Current ($mu$A)')
ax.autoscale(tight=True)
fig.savefig(r'F:DataCharm学术图表绘制Python-matplotlibSciencePlotsfig1_science_dark_background.png',
width=4,height=3,dpi=600)
结果如下:
更多的例子,大家可以直接去官网进行测试,接下来,我们将主题应用到我们之前绘制过的科学图表绘图代码上。
SciencePlots实例应用
- 多类别散点图
science:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use(['science', 'no-latex'])
#plt.rcParams['font.family'] = "Times New Roman"
x = np.arange(0,len(scatter),1)
y1 = scatter.D1.values
y2 = scatter.D2.values
y3 = scatter.D3.values
y4 = scatter.D4.values
y5 = scatter.D5.values
y6 = scatter.D6.values
fig,ax = plt.subplots(figsize=(4,3),dpi=300)
scatter_01 = ax.plot(x,y1,marker='s',lw=.5,label='D1')
scatter_02 = ax.plot(x,y2,marker='s',ls='--',lw=.5,label='D2')
scatter_03 = ax.plot(x,y3,marker='o',lw=.8,ls=':',label='D3')
scatter_04 = ax.plot(x,y4,marker='o',lw=.5,label='D4')
scatter_05 = ax.plot(x,y5,marker='^',lw=.5,ls='-.',label='D5')
scatter_06 = ax.plot(x,y6,marker='^',ls='--',lw=.5,label='D6')
ax.set_ylim(bottom=10,top=45)
ax.legend(frameon=False,ncol=3,loc='upper center',fontsize=8.5)
text_font = {'size':'15','weight':'bold','color':'black'}
ax.text(.88,.88,"(a)",transform = ax.transAxes,fontdict=text_font,zorder=4)
ax.text(.87,-.08,'nVisualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 4,color='black',fontweight='bold',family='Roboto Mono')
plt.savefig(r'F:DataCharm学术图表绘制Python-matplotlibSciencePlotsclass_scatter_science.png',width=5,height=3,
dpi=900,bbox_inches='tight')
plt.show()
结果如下:
ieee:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use(['ieee', 'no-latex'])
#plt.rcParams['font.family'] = "Times New Roman"
x = np.arange(0,len(scatter),1)
y1 = scatter.D1.values
y2 = scatter.D2.values
y3 = scatter.D3.values
y4 = scatter.D4.values
y5 = scatter.D5.values
y6 = scatter.D6.values
fig,ax = plt.subplots(figsize=(4,3),dpi=300)
scatter_01 = ax.plot(x,y1,marker='s',lw=.5,label='D1')
scatter_02 = ax.plot(x,y2,marker='s',ls='--',lw=.5,label='D2')
scatter_03 = ax.plot(x,y3,marker='o',lw=.8,ls=':',label='D3')
scatter_04 = ax.plot(x,y4,marker='o',lw=.5,label='D4')
scatter_05 = ax.plot(x,y5,marker='^',lw=.5,ls='-.',label='D5')
scatter_06 = ax.plot(x,y6,marker='^',ls='--',lw=.5,label='D6')
ax.set_ylim(bottom=10,top=45)
ax.legend(frameon=False,ncol=3,loc='upper center',fontsize=8.5)
text_font = {'size':'15','weight':'bold','color':'black'}
ax.text(.88,.88,"(a)",transform = ax.transAxes,fontdict=text_font,zorder=4)
ax.text(.87,-.08,'nVisualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 4,color='black',fontweight='bold',family='Roboto Mono')
plt.savefig(r'F:DataCharm学术图表绘制Python-matplotlibSciencePlotsclass_scatter_ieee.png',width=5,height=3,
dpi=900,bbox_inches='tight')
plt.show()
结果如下:
- 多类别柱形图
science + high-contrast
plt.style.use(['science', 'high-contrast','no-latex'])
labels = ['L1', 'L2', 'L3', 'L4', 'L5']
data_a = [20, 34, 30, 35, 27]
data_b = [25, 32, 34, 20, 25]
data_c = [12, 20, 24, 17, 16]
x = np.arange(len(labels))
width = .25
#plots
fig,ax = plt.subplots(figsize=(5,3),dpi=200)
bar_a = ax.bar(x-width/2, data_a,width,label='category_A')
bar_b = ax.bar(x+width/2, data_b, width,label='category_B')
bar_c = ax.bar(x+width*3/2, data_c,width,label='category_C')
ax.set_xticks(x+.1)
ax.set_xticklabels(labels,size=10)
ax.legend()
text_font = {'size':'14','weight':'bold','color':'black'}
ax.text(.03,.9,"(a)",transform = ax.transAxes,fontdict=text_font,zorder=4)
ax.text(.87,-.08,'nVisualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 5,color='black',fontweight='bold',family='Roboto Mono')
plt.savefig(r'F:DataCharm学术图表绘制Python-matplotlibSciencePlotsbar_sci_high-contrast.png',width=5,height=3,
dpi=900,bbox_inches='tight')
plt.show()
结果如下:
当然我们也可以在相关性散点图绘制方法中采用此主题,可视化结果如下:
- 被遗忘的软件产品形态
- 区块链是什么,一文给您讲清楚
- ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[下篇]
- 2017科技颠覆生活哪家强?
- 柯洁宣布复出,再次迎战AI!但对手不再是阿尔法狗……
- 【Scikit-Learn 中文文档】协方差估计 / 经验协方差 / 收敛协方差 / 稀疏逆协方差 / Robust 协方差估计 - 无监督学习 - 用户指南 | ApacheCN
- 先搞懂这八大基础概念,再谈机器学习入门!
- 人工智能化的传感器技术
- 带有CSS3的动画3D条形图
- 有故事的微信小游戏“跳一跳”
- 机器学习敲门砖:任何人都能看懂的TensorFlow介绍
- 跟小编来体验一下微信小程序
- DeepVO:基于深度循环卷积神经网络的端到端视觉里程计
- 是人工智障还是蹭热度?我一定是抱走了假的二次元老婆
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Flask表单之WTForms和flask-wtf
- Head First设计模式——适配器和外观模式
- 「2020最新」Spring最易学习教程 4—整合Mybatis 事务控制
- Head First设计模式——模板方法模式
- Head First设计模式——迭代器模式
- PR工具自带的电源网络分析功能靠谱吗?
- flask_admin使用教程
- C#使用FtpWebRequest 基础连接已经关闭:连接被意外关闭(The underlying connection was closed:The connection was closed u
- 自动化运维实践 | Ansible入门
- 通过设置JDK解决存在多个Gradle后台进程的问题
- Head First设计模式——组合模式
- mmap概述
- 什么是计算机程序?操作系统、指令、进程、线程等
- Head First设计模式——状态模式
- Head First设计模式——代理模式