之前还在讨论Matplotlib没有很好的第三方主题库呢?这不,又被我发现了一个宝藏库,还专门用于一些学术期刊的图表发表,可谓是弥补了matplotlib 繁琐的自定义设置。好了,话不多说,今天这篇推文的主角就是Github 上拥有1.6k 星之多的科学论文图表绘制库「SciencePlots」。推文的主要内如下:
- SciencePlots 库介绍
- SciencePlots 实例演示
SciencePlots 库介绍
使用Python-matplotlib绘制科研图表,其默认的颜色和格式并不能满足一般的期刊要求,若想要符合要求,就必须自定义设置,而这个步骤在对面对多幅图表时就显得繁琐和重复。在之前的几篇科学图表绘制推文中Python-matplotlib 学术散点图 EE 统计及绘制 Python-matplotlib 横向堆积柱状图绘制 就定制化表格编写了较多定制化代码。而SciencePlots就是为解决科研图表繁琐设置而定制的一系列科研绘图样式库,可以绘制很合适科研发表的图表。
安装
我们直接可使用pip 直接进行安装,但想要安装最新版本可以采用如下方式:
代码语言:javascript复制pip install git https://github.com/garrettj403/SciencePlots.git
如使用pip稳定安装,则直接使用如下代码:
代码语言:javascript复制# for lastest release
pip install SciencePlots
主要样式
SciencePlots 提供了多种用于不同期刊发表要求的主题,接下来我们主要介绍几种主题,注意:由于SciencePlots默认使用Latex字体渲染,而相应软件安装较为麻烦,以下样例均采用禁用Latex字体渲染,即设置 no-latex (数据还是使用官网提供的):
数据构建:
代码语言:javascript复制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)
「原始主题」
代码语言:javascript复制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」
代码语言:javascript复制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」
代码语言:javascript复制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」
代码语言:javascript复制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:
代码语言:javascript复制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:
代码语言:javascript复制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
代码语言:javascript复制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()
结果如下:
当然我们也可以在相关性散点图绘制方法中采用此主题,可视化结果如下:
总结
本期推文我们介绍了matplotlib非常优秀的科学图表绘图库SciencePlots, 在一定程度上极大了缩减了定制化符合期刊图表的绘制时间,也希望大家可以持续关注这个库(希望大家多发sci)。