【matplotlib】python 论文作图工具SciencePlots的使用

您所在的位置:网站首页 sci图表字体 【matplotlib】python 论文作图工具SciencePlots的使用

【matplotlib】python 论文作图工具SciencePlots的使用

2024-02-20 15:40| 来源: 网络整理| 查看: 265

SciencePlots简介

SciencePlots是一个基于Matplotlib的补充包,里面主要包含了一些以.mplstyle为后缀的图表样式的配置文件。这样,你画图的时候只需要通过调用这些配置文件,就能画出比较好看的数据可视化图表,也避免了你每次画图时都要从头开始手动配置图表的格式。

目前该工具包中包含Science,IEEE等期刊的图表格式,还包括一些对图表中的网格和字体颜色等的配置文件。

项目链接:GitHub - garrettj403/SciencePlots: Matplotlib styles for scientific plotting

安装方式 pip install SciencePlots 示例代码 import numpy as np import matplotlib.pyplot as plt #plt.rcParams['font.sans-serif']=['SimHei'] #显示中文标签 #plt.rcParams['axes.unicode_minus']=False #这两行需要手动设置 def model(x, p): return x ** (2 * p + 1) / (1 + x ** (2 * p)) pparam = dict(xlabel='Voltage (mV)', ylabel='Current ($\mu$A)') x = np.linspace(0.75, 1.25, 201) with plt.style.context(['science', 'no-latex']): fig, ax = plt.subplots() for p in [10, 20, 40, 100]: ax.plot(x, model(x, p), label=p) ax.legend(title='Order') ax.autoscale(tight=True) ax.set(**pparam) # Note: $\mu$ doesn't work with Times font (used by ieee style) ax.set_ylabel(r'Current (\textmu A)') fig.savefig('fig2a.pdf') fig.savefig('fig2a.jpg', dpi=300)

绘图效果还是很不错的 

个人的使用感受以及踩过的坑

这个包的确非常简单易用,但是选择一些格式的时候会报没有字体的错误,例如:

findfont: Font family ['serif'] not found. Falling back to DejaVu Sans. RuntimeWarning: Glyph 30005 missing from current font. Font 'default' does not have a glyph for '\u7535' [U+7535], substituting with a dummy symbol.

第一个是说没有这个font family, 第二个是说中文字符的问题,总之使用起来并不是很顺畅。

要想解决这些问题,需要下载对应的字体,并且修改配置文件 + 重启python环境

关于font family的问题

主要有几步:

1.前往指定目录:/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf,将下载好的SimHei字体移动到该目录下。

2.修改matplotlibrc文件:  /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc,修改内容如下:

font.family : sans-serif # 去掉前面的# font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif # 去掉前面的#,并在冒号后面添加SimHei axes.unicode_minus : False # 去掉前面的#,并将True改为False

 3.重启python环境 这一点非常重要

Font family [‘sans-serif‘] not found.Falling back to DejaVu Sans.解决办法_sinat_40875078的博客-CSDN博客

1238: UserWarning: findfont: Font family [‘sans-serif’] not found. Falling back to DejaVu Sans._但行好事,莫问前程-CSDN博客

Mac python matplotlib Glyph xxxxx missing from current font的解决方案_Python学习与数据挖掘-CSDN博客

关于中文字符的问题

SciencePlots自身提供了中文支持,但是需要安装特定的字体,而且这个字体Windows没有...

FAQ · garrettj403/SciencePlots Wiki · GitHub

如果要使用中文字符,还是按照上面的解决办法,下载中文字体以后加到matplotlib/mpl-data/fonts/ttf 目录下,修改matplotlibrc文件

个人感受

并不算特别好用... 但是它的格式设置都是公开在.mplstyle文件里的,例如nature.mplstyle

自动写代码手动设置这些内容也不是不行.....

# Matplotlib style for Nature journal figures. # In general, they advocate for all fonts to be panel labels to be sans serif # and all font sizes in a figure to be 7 pt and panel labels to be 8 pt bold. # Figure size figure.figsize : 3.3, 2.5 # max width is 3.5 for single column # Font sizes axes.labelsize: 7 xtick.labelsize: 7 ytick.labelsize: 7 legend.fontsize: 7 font.size: 7 # Font Family font.family: sans-serif font.sans-serif: DejaVu Sans, Arial, Helvetica, Lucida Grande, Verdana, Geneva, Lucid, Avant Garde, sans-serif mathtext.fontset : dejavusans # Set line widths axes.linewidth : 0.5 grid.linewidth : 0.5 lines.linewidth : 1. lines.markersize: 3 # Always save as 'tight' # savefig.bbox : tight # savefig.pad_inches : 0.01 # Use virtually all space when we specify figure dimensions # LaTeX packages text.latex.preamble : \usepackage{amsmath} \usepackage{amssymb} \usepackage{sfmath}

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3