spt¶
约 296 个字 124 行代码 预计阅读时间 3 分钟
spt: A Scientific matplotlib plot rcParams configuration template python package.
- Repo: https://github.com/Bit-Part-Young/spt
- Full Documentation: README.md / Web
Installation¶
- via pip:
- via git:
pip install git+https://github.com/Bit-Part-Young/spt.git
# or
pip install git+https://gitee.com/yangsl306/spt.git
- via source code:
git clone https://github.com/Bit-Part-Young/spt.git
# or
git clone https://gitee.com/yangsl306/spt.git
cd spt
# virtual environment
python -m venv venv
source venv/bin/activate
pip install .
# or
pip install -r requirements.txt
# editable mode
pip install -e .
Usage¶
Full example codes can be found in examples folder.
fonts¶
- Times New Roman: Copy
TimesNewRoman*.ttf
fonts to~/.fonts
or~/.local/share/fonts
or matplotlib font path in specific conda env, then remove matplotlib cache, relogin.
cp fonts/TimesNewRoman*.ttf ~/.fonts
# or
cp fonts/TimesNewRoman*.ttf ~/.local/share/fonts
# copy to matplotlib font path in specific conda env
cp fonts/TimesNewRoman*.ttf <conda_env>/lib/pythonXXX/site-packages/matplotlib/mpl-data/fonts/ttf/
# remove matplotlib cache
rm -rf ~/.cache/matplotlib
# check fonts
fc-list lang:en | grep -i "Times New Roman"
- Chinese: Copy
SimHei.ttf
font to~/.fonts
or~/.local/share/fonts
or matplotlib font path in specific conda env, backup original matplotlibrc file, copy modified matplotlibrc file tompl-data
path, then remove matplotlib cache, relogin.
cp fonts/SimHei.ttf ~/.fonts
# or
cp fonts/SimHei.ttf ~/.local/share/fonts
# copy to matplotlib font path in specific conda env
cp fonts/SimHei.ttf <conda_env>/lib/pythonXXX/site-packages/matplotlib/mpl-data/fonts/ttf/
# backup matplotlibrc file
cd <conda_env>/lib/pythonXXX/site-packages/matplotlib/mpl-data/
cp matplotlibrc matplotlibrc_origin
cd -
# copy modified matplotlibrc file
cp fonts/matplotlibrc <conda_env>/lib/pythonXXX/site-packages/matplotlib/mpl-data/
# remove matplotlib cache
rm -rf ~/.cache/matplotlib
# check fonts
fc-list lang:zh | grep -i "SimHei"
matplotlibrc
modification:
# origin
font.family: sans-serif
font.sans-serif: DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
axes.unicode_minus: True # use Unicode for the minus symbol rather than hyphen. See
# modification
font.family: sans-serif
font.sans-serif: DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif, SimHei, Times New Roman, Times
axes.unicode_minus: False # use Unicode for the minus symbol rather than hyphen. See
set_plot_params()¶
- code snippets (complete script: plot.py):
import matplotlib.pyplot as plt
from spt.plot_params import set_plot_params
...
set_plot_params()
fig, ax = plt.subplots()
...
Figure:
set_roman_plot_params()¶
- code snippets (complete script: plot_roman.py):
import matplotlib.pyplot as plt
from spt.plot_params import set_roman_plot_params
...
set_roman_plot_params()
fig, ax = plt.subplots()
...
Figure:
- 3d plot code snippets (complete script: plot_3d.py):
import matplotlib.pyplot as plt
from spt.plot_params import set_roman_plot_params
...
set_roman_plot_params(
axes_labelpad=15,
legend_handletextpad=0.0,
legend_fontsize=22,
savefig_bbox="standard",
)
fig, ax = plt.subplots(subplot_kw={"projection": "3d"}, figsize=(10, 8))
...
Figure:
Chinese characters plot¶
- code snippets (complete script: plot_zh.py)
import matplotlib.pyplot as plt
from spt.plot_params import set_roman_plot_params
...
set_roman_plot_params()
fig, ax = plt.subplots()
ax.plot(x, y, label="正弦函数")
ax.set(xlabel="x", ylabel="y")
# legend 字体设置为 SimHei
ax.legend(prop={"family": "SimHei"})
...
Figure
Scientific Figure Examples¶
- Example 1 (complete script: dot_line_plot.py):
Figure:
- Example 2 (complete script: phase_stability_Nb3Si_plot.py):
Figure:
- Example 3 (complete script: b_fit_cal.py):
Figure:
- Example 4 (complete script: xrd_plot.py):
Figure:
- Example 5 (complete script: ternary_plot.py):
Figure:
- Example 5 (complete script: ternary_convex_hull.py):
Figure:
To do¶
- 完善
setup.py
安装脚本 - 3D 图绘制 z 轴标签显示不全(
"savefig.bbox"
参数设为"tight"
时,会出现这种情况,需设为"standard"
,多余空白需自己后处理掉;jupyter notebook 中 z 轴标签仍显示不全) - 安装 spt package 后,使用
fig, ax = plt.subplots()
命令,VSCode 的 Pylance 插件无法自动识别ax
对象的属性和方法(matplotlib 3.8 版本的问题,需将 matplotlib 版本降到 3.8 以下)