在使用Matplotlib的時候,一開始並不懂得美化,修改style,設置dpi…都是小打小鬧,發現繪製的曲線圖跟論文中的相差很遠。Word、PPT、Visio、Origin、SigmaPlot都能做出比較好看的配圖,但是懶得學,目前還用不到這麼專業的繪圖工具。對於剛上手的小白來說,反而簡單易上手,不那麼“專業”的開源工具包是相對更好的選擇。本文就介紹一個近期更新的SCI/IEEE論文繪圖工具——SciencePlots。剛開源一個月,目前還在維護中,現在主要功能集中在曲線圖和散點圖繪製。
系統環境:
- windows10
- python-3.7.6
- jupyter notebook
作者發表的論文使用該工具配圖:
1. 風格效果
先看療效,能(man)治(zu)病(xu)再(qiu)看;治不了就不用看了,以免浪費時間。
Science的風格效果1:
Science的風格效果2:
Science的風格效果3:
Science的風格效果4:
Science的風格效果5:(有點Yolo主頁的風格…)
Science的風格效果6:
IEEE效果1:
2. 安裝配置
2.1 通用配置
通過簡單的pip安裝
# for latest version
pip install git+https://github.com/garrettj403/SciencePlots.git
# for last release
pip install SciencePlots
2.2 win10 配置
因爲作者是在linux系統下使用,並且已經配置好了其它依賴,所以對安裝的部分介紹較少。
經過一早上的測試,找到了在Win10中使用默認LaTex字體渲染報錯的解決方法,並且pull到了該工具倉庫的issue,作者稍後會添加到readme。主要原因是LaTeX沒有配置好。當然如果不想使用LaTeX渲染,或者認爲Latex花費的時間太長,則可以使用以下no-latex樣式禁用Latex :
plt.style.use(['science','no-latex'])
下面介紹一下使用LaTeX字體渲染的配置流程。
如果沒配置好LaTeX會遇到以下錯誤:
RuntimeError: Failed to process string with tex because latex could not be found
嘗試了很多方法,發現並不起作用,包括pip tex等等。正確的解決方法是:安裝MiKTeX和LaTeX,並將其添加到環境變量。
首先下載TeX:http://www.tug.org/。
點進去,選擇第二個下載
解壓,選擇安裝:
首先安裝 【MiKTeX】:注意選擇爲所有用戶使用;這樣會自動添加到系統環境變量;
然後安裝【LaTex】:esay!
接下來配置環境變量。人類終究是視覺動物,喜歡有視覺衝擊的東西,一張圖總結比文字看起來舒服:
注意有兩個路徑:
D:\APP\MiKTeX\miktex\bin\x64\
D:\APP\TeXstudio\texstudio.exe
其中,第一個是在安裝的時候自動創建的,如果選擇是爲當前用戶安裝,則該路徑在當前用戶的環境變量中,需要手動粘過來。
第二個是需要根據自己系統的安裝路徑確定的。
然後關閉Anaconda的虛擬環境,重新啓動一個窗口重新激活,就可以使用了。查看latex:
可以看到已經配置成功了,接下來就可以使用了。
2.3 Linux 配置
暫未測試,不過肯定需要配置latex。有了win10配置方法,在linux配置應該不難。
3. 食用方法
pip安裝將自動將所有 *.mplstyle
文件移動到適當的目錄中。也可以手動執行此操作。首先,克隆存儲庫,然後將所有 *.mplstyle
文件複製到Matplotlib樣式目錄中。直接pip安裝就可以使用了,源碼方式需要自己配製。
第一種全局使用方式,將以下內容添加到python腳本的頂部:
import matplotlib.pyplot as plt
plt.style.use('science')
第二種全局使用方式,通過以下方式將多種樣式組合在一起:
plt.style.use(['science','ieee'])
注意:在這種情況下,該ieee樣式將覆蓋主要science樣式中的某些參數,以便爲IEEE紙配置圖表(列寬,字體大小等)。
第三種臨時使用方式,使用方法:
with plt.style.context(['science', 'ieee']):
plt.figure()
plt.plot(x, y)
plt.show()
目前該工具支持的風格:
4. 倉庫地址
https://github.com/garrettj403/SciencePlots
5. 測試代碼
""" An example of the 'science' theme. """
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)
with plt.style.context(['science']):
fig, ax = plt.subplots()
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('figures/fig1.pdf')
fig.savefig('figures/fig1.jpg', dpi=300)
with plt.style.context(['science', 'ieee']):
fig, ax = plt.subplots()
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('figures/fig2.pdf')
fig.savefig('figures/fig2.jpg', dpi=300)
with plt.style.context(['science', 'scatter']):
fig, ax = plt.subplots(figsize=(4,4))
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])
fig.savefig('figures/fig3.pdf')
fig.savefig('figures/fig3.jpg', dpi=300)
with plt.style.context(['science', 'high-vis']):
fig, ax = plt.subplots()
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('figures/fig4.pdf')
fig.savefig('figures/fig4.jpg', dpi=300)
with plt.style.context(['dark_background', 'science', 'high-vis']):
fig, ax = plt.subplots()
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('figures/fig5.pdf')
fig.savefig('figures/fig5.jpg', dpi=300)
with plt.style.context(['science', 'notebook']):
fig, ax = plt.subplots()
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('figures/fig10.pdf')
fig.savefig('figures/fig10.jpg', dpi=300)
# Plot different color cycles
with plt.style.context(['science', 'bright']):
fig, ax = plt.subplots()
for p in [5, 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('figures/fig6.pdf')
fig.savefig('figures/fig6.jpg', dpi=300)
with plt.style.context(['science', 'vibrant']):
fig, ax = plt.subplots()
for p in [5, 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('figures/fig7.pdf')
fig.savefig('figures/fig7.jpg', dpi=300)
with plt.style.context(['science', 'muted']):
fig, ax = plt.subplots()
for p in [5, 7, 10, 15, 20, 30, 38, 50, 100, 500]:
ax.plot(x, model(x, p), label=p)
ax.legend(title='Order', fontsize=7)
ax.set(xlabel='Voltage (mV)')
ax.set(ylabel='Current ($\mu$A)')
ax.autoscale(tight=True)
fig.savefig('figures/fig8.pdf')
fig.savefig('figures/fig8.jpg', dpi=300)
with plt.style.context(['science', 'retro']):
fig, ax = plt.subplots()
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('figures/fig9.pdf')
fig.savefig('figures/fig9.jpg', dpi=300)
with plt.style.context(['science', 'grid']):
fig, ax = plt.subplots()
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('figures/fig11.pdf')
fig.savefig('figures/fig11.jpg', dpi=300)