本次学习了《科研论文配图绘制指南—— 基于Python 》的第一节,科研论文配图(插图)是实验数据和分析结论的可视化表达。科研论文绘图是一种将科学与艺术相结合的工作,既能用图片的艺术感来吸引读者,又能展现实验数据和分析结论的科学性,帮助读者理解科研工作者所研究的内容。
科研论文绘制基础
科研论文配图的分类与构成
科研配图的配色基础
部分配图代码实现
虚拟环境配置
# 创建conda虚拟环境
conda create --name science_plots python=3.9
#激活虚拟环境
conda activate science_plots
# 安装依赖
conda install -c conda-forge proplot -y conda install matplotlib==3.4.3 -y
conda install seaborn==0.11.2 -y
pip install SciencePlots
pip install openpyxl
pip install Pillow
#虚拟环境中安装ipykernel
conda install -c anaconda ipykernel
#虚拟环境添加到jupyter
python -m ipykernel install --user --name=science_plots
科研论文配图基本构成
import numpy as np
import pandas as pd
import seaborn as sns
import proplot as pplt
import matplotlib.pyplot as plt
import scienceplots # 新版必须先导入scienceplots才能绘图
from proplot import rc
rc["font.family"] = "Times New Roman"
rc["axes.labelsize"] = 15
rc['tick.labelsize'] = 13
rc["suptitle.size"] = 15
# plt.style.use('science') #需安装Scienceplots 库(pip install SciencePlots),注意版本不同,引用方式可能有所改变
# 如果不想装Latex那一堆依赖,可以不依赖Latex渲染
plt.style.use(['science', 'no-latex'])
data = pd.read_excel(r"基本构成示意绘图数据.xlsx")
#proplot 方法绘制
fig = pplt.figure(figsize=(4.5,3.5))
ax = fig.subplot()
ax.format(abc='(a.)', abcloc='ul',abcsize=20,
xlabel='Time', ylabel='Values',
xlim=(-2,40),ylim=(-8,30))
selsect = ["A","B","C","D"]
colors = ["#2FBE8F","#459DFF","#FF5B9B","#FFCC37"]
for index,color in zip(selsect,colors):
data_selcet = data.loc[data['type']==index,:]
# 要支持新版的seaborn必须使用numpy数组,在后面加上values即可
ax.errorbar(x=data_selcet["time"].values,y=data_selcet["mean"].values,yerr=data_selcet["sd"].values,color="k",
linewidth=1,marker='o',ms=10,mew=1,mec='k',mfc=color,capsize=5,label=index)
ax.legend(ncols=4, frame=True,loc='t')
plt.savefig("./图1-1-1 科研论文配图基本构成示意图.png", dpi=300)
plt.show()
色轮配色方案
单色配色方案
将色相相近的一组颜色进行组合
import numpy as np
import pandas as pd
import seaborn as sns
import proplot as pplt
import matplotlib.pyplot as plt
from proplot import rc
rc["font.family"] = "Times New Roman"
rc["axes.labelsize"] = 15
rc['tick.labelsize'] = 13
rc["suptitle.size"] = 15
# 图1-2-5 利用单色配色方案绘制的可视化配图示例_a
menMeans = (5, 15, 30, 40)
menStd = (2, 3, 4, 5)
ind = np.arange(4)
width= 0.7
labels = ('A', 'B', 'C', 'D')
colors=["#FFAAAA","#D46A6A","#AA3939","#801515"]
fig,ax = plt.subplots(figsize=(4,3),dpi=100,facecolor="w")
for name,value,err,label,color in zip(labels,menMeans,menStd,labels,colors):
ax.bar(name,value,yerr=err,label=label,color=color,ec="k",lw=.8,
capsize=5,error_kw={'linewidth':1})
for spine in ["top","right"]:
ax.spines[spine].set_visible(False)
ax.grid(False)
ax.set_ylim(0,50)
ax.legend(frameon=False,handlelength=1.5,handleheight=1.5)
#plt.savefig(r'r"\第1章 科研论文配图的绘制与配色基础\图1-2-5 利用单色配色方案绘制的可视化配图示例_a.pdf',bbox_inches='tight')
plt.savefig(".\图1-2-5 利用单色配色方案绘制的可视化配图示例_a.png",
bbox_inches='tight',dpi=300)
plt.show()
#
互补配色方案
import numpy as np
import pandas as pd
import seaborn as sns
import proplot as pplt
import matplotlib.pyplot as plt
from proplot import rc
rc["font.family"] = "Times New Roman"
rc["axes.labelsize"] = 15
rc['tick.labelsize'] = 13
rc["suptitle.size"] = 15
#图1-2-6 利用互补色配色方案绘制的可视化配图示例_a
menMeans = (5, 15)
menStd = (2, 5)
ind = np.arange(2)
labels = ('A', 'B', )
colors=["#7F81B2","#D4B96A"]
fig,ax = plt.subplots(figsize=(4,3),dpi=100,facecolor="w")
for name,value,err,label,color in zip(labels,menMeans,menStd,labels,colors):
ax.bar(name,value,yerr=err,label=label,color=color,ec="k",lw=.8,
capsize=5,error_kw={'linewidth':1})
for spine in ["top","right"]:
ax.spines[spine].set_visible(False)
ax.grid(False)
ax.set_ylim(0,25)
ax.legend(loc="upper left",frameon=False,
handlelength=1.5,handleheight=1.5)
plt.savefig(".\图1-2-6 利用互补色配色方案绘制的可视化配图示例_a.png",
bbox_inches='tight',dpi=300)
plt.show()
#图1-2-6 利用互补色配色方案绘制的可视化配图示例_b
sizes = [30, 70]
labels = ['A','B']
colors=["#7F81B2","#D4B96A"]
fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
explode = (0, 0.,)
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=False, startangle=90,colors=colors,
wedgeprops={'linewidth':.8, 'edgecolor': 'k'},
textprops={'size': 12,"color":"w"})
ax.legend(labels,loc ='upper left',fontsize=9,handlelength=1.2,handleheight=1.2)
plt.tight_layout()
plt.savefig(".\图1-2-6 利用互补色配色方案绘制的可视化配图示例_b.png",
bbox_inches='tight',dpi=300)
plt.show
Seaborn色系配图示例
import numpy as np
import pandas as pd
import seaborn as sns
import proplot as pplt
import matplotlib.pyplot as plt
from matplotlib import cm
from colorspacious import cspace_converter
import scienceplots
from proplot import rc
rc["font.family"] = "Times New Roman"
rc["axes.labelsize"] = 13
rc['tick.labelsize'] = 11
rc["suptitle.size"] = 15
tips = sns.load_dataset("tips")
#使用Scienceplots包绘图主题绘制(需安装Scienceplots包(pip install SciencePlots),不同版本引用方式有所不同,请注意!)
# a)单色系可视化绘制示例
with plt.style.context(['science']):
from proplot import rc
rc["xtick.minor.visible"] = False
rc["ytick.minor.visible"] = False
rc["xtick.major.pad"] =5
fig,ax = plt.subplots(figsize=(3,3.2),dpi=100,facecolor="w")
scatter = ax.scatter(x="total_bill",y="tip",c="tip",data=tips,ec="k",
cmap="YlGnBu")
for spine in ["top","right"]:
ax.spines[spine].set_visible(False)
ax.tick_params(top=False,right=False)
cax = ax.inset_axes([0.1, .85, 0.6, 0.05], transform=ax.transAxes)
colorbar = fig.colorbar(scatter, ax=scatter, cax=cax,
orientation="horizontal")
colorbar.ax.tick_params(bottom=True,direction="in",labelsize=8,pad=3)
colorbar.ax.xaxis.set_ticks_position('top')
colorbar.outline.set_linewidth(.4)
ax.grid(False)
ax.set_ylim(0,12)
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
plt.tight_layout()
# plt.savefig(r'r"\第1章 科研论文配图的绘制与配色基础\图1-2-14 使用seaborn库中Tips数据集绘制的单色系、双色渐变色系和多色系可视化配图示例_a.pdf',bbox_inches='tight')
plt.savefig(r'.\图1-2-14 使用seaborn库中Tips数据集绘制的单色系、双色渐变色系和多色系可视化配图示例_a.png',
bbox_inches='tight',dpi=300)
plt.show()
# b)双色渐变色系可视化绘制示例
with plt.style.context(['science']):
from proplot import rc
rc["xtick.minor.visible"] = False
rc["ytick.minor.visible"] = False
rc["xtick.major.pad"] =5
fig,ax = plt.subplots(figsize=(3,3.2),dpi=100,facecolor="w")
scatter = ax.scatter(x="total_bill",y="tip",c="tip",data=tips,ec="k",
cmap="seismic")
for spine in ["top","right"]:
ax.spines[spine].set_visible(False)
ax.tick_params(top=False,right=False)
cax = ax.inset_axes([0.1, .85, 0.6, 0.05], transform=ax.transAxes)
colorbar = fig.colorbar(scatter, ax=scatter, cax=cax,
orientation="horizontal")
colorbar.ax.tick_params(bottom=True,direction="in",labelsize=8,pad=3)
colorbar.ax.xaxis.set_ticks_position('top')
colorbar.outline.set_linewidth(.4)
ax.grid(False)
ax.set_ylim(0,12)
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
plt.tight_layout()
# plt.savefig(r'r"\第1章 科研论文配图的绘制与配色基础\图1-2-14 使用seaborn库中Tips数据集绘制的单色系、双色渐变色系和多色系可视化配图示例_b.pdf',bbox_inches='tight')
plt.savefig(r'.\图1-2-14 使用seaborn库中Tips数据集绘制的单色系、双色渐变色系和多色系可视化配图示例_b.png',
bbox_inches='tight',dpi=300)
plt.show()
# c)多色系可视化绘制示例
with plt.style.context(['science']):
from proplot import rc
rc["xtick.minor.visible"] = False
rc["ytick.minor.visible"] = False
rc["xtick.major.pad"] =5
fig,ax = plt.subplots(figsize=(3,3.2),dpi=100,facecolor="w")
scatter = ax.scatter(x="total_bill",y="tip",c="tip",data=tips,ec="k",
cmap="Set1")
for spine in ["top","right"]:
ax.spines[spine].set_visible(False)
ax.tick_params(top=False,right=False)
cax = ax.inset_axes([0.1, .85, 0.6, 0.05], transform=ax.transAxes)
colorbar = fig.colorbar(scatter, ax=scatter, cax=cax,
orientation="horizontal")
colorbar.ax.tick_params(bottom=True,direction="in",labelsize=8,pad=3)
colorbar.ax.xaxis.set_ticks_position('top')
colorbar.outline.set_linewidth(.4)
ax.grid(False)
ax.set_ylim(0,12)
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
plt.savefig(r'.\图1-2-14 使用seaborn库中Tips数据集绘制的单色系、双色渐变色系和多色系可视化配图示例_c.png',
bbox_inches='tight',dpi=300)
plt.show()