#!/usr/bin/env python # coding: utf-8 # # 借助 [SciencePlots ](https://github.com/garrettj403/SciencePlots) 进行优美的科研绘图 # # ## 安装 # # In[ ]: get_ipython().system(' pip install SciencePlots') # ## 使用示例 # # 构建数据,导入相关包 # # From version v1.1.0 on, import scienceplots is needed on top of your scripts so Matplotlib can make use of the styles. # # 包 scienceplots 在 v1.1.0 之后,需要先导入才能让 Matplotlib 使用该包提供的样式。 # In[2]: import numpy as np import matplotlib.pyplot as plt import scienceplots from mplfonts import use_font def model(x, p): return x ** (2 * p + 1) / (1 + x ** (2 * p)) x = np.linspace(0.75, 1.25, 201) # 未使用SciencePlots,绘制结果如下: # In[3]: fig, ax = plt.subplots(dpi=150, figsize=(4.5, 3)) 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) plt.show() # 若使用 SciencePlots 中 Science 的提交格式,绘制结果如下: # In[4]: with plt.style.context(["science", "no-latex"]): fig, ax = plt.subplots(dpi=150, figsize=(4.5, 3)) 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) plt.show() # IEEE要求黑白印刷出来的文章也需要能够区分颜色,使用 SciencePlots 中 ieee 的提交格式,绘制结果如下: # In[5]: with plt.style.context(["science", "ieee", "no-latex"]): fig, ax = plt.subplots(dpi=150, figsize=(4.5, 3)) 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) plt.show() # 莫名的 `-` 显示不正常 # In[6]: use_font("Source Han Serif SC") with plt.style.context(["science", "scatter", "no-latex"]): fig, ax = plt.subplots(figsize=(4, 4), dpi=150) ax.plot([-2, 2], [-2, 2], "k--") ax.fill_between( [-2, 2], [-2.2, 1.8], [-1.8, 2.2], color="dodgerblue", alpha=0.2, lw=0 ) for i in range(7): x1 = np.random.normal(0, 0.5, 10) y1 = x1 + np.random.normal(0, 0.2, 10) ax.plot(x1, y1, label=r"$^\#${}".format(i + 1)) ax.legend(title="Sample", loc=2) ax.set_xlabel( r"中文 $\log_{10}\left(\frac{L_\mathrm{IR}}{\mathrm{L}_\odot}\right)$" ) ax.set_ylabel( r"$\log_{10}\left(\frac{L_\mathrm{6.2}}{\mathrm{L}_\odot}\right)$" ) ax.set_xlim([-2, 2]) ax.set_ylim([-2, 2])