SciencePlots用于科学绘图的Matplotlib样式库

文章介绍了如何安装和配置SciencePlots库来增强matplotlib的图形样式,包括需要的Latex环境和NotoSerifSC中文字体。通过示例代码展示了如何解决中文显示问题并创建多条曲线的图形,同时提供了多种风格的主题搭配选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

  • SciencePlots包是matplotlib包的主题扩展库,所以我们必须同时拥有SciencePlots包和matplotlib包。SciencePlots包[官方地址]。(https://github.com/garrettj403/SciencePlots)
  • pip install SciencePlotspip install matplotlib
  • 使用此包还需要Latex支持,Latex下载链接。中文字体需要Noto Serif SC支持,下载链接。安装完字体后,要将matplotlib包的字体缓存更新,以win系统为例,删除C:\Users\lenovo\.matplotlib文件夹,然后重新执行import matplotlib as mpl会更新字体库。
  • 官方文档中使用plt.style.use(['science', 'no-latex', 'cjk-sc-font'])设置中文字体,我安装了'cjk-sc-font'(也就是'Noto Serif SC'字体)但是依然会报错,我使用plt.rcParams['font.sans-serif']=['Noto Serif SC']能达到相同的效果,如果有人知道怎么解决请私信我修改教程。

绘图

#加载包
import numpy as np
import pandas as pd
from plotnine import*
import seaborn as sns
from scipy import stats

import matplotlib as mpl
import matplotlib.pyplot as plt
# 中文显示问题
plt.rcParams['font.sans-serif']=['Noto Serif SC']
plt.rcParams['axes.unicode_minus'] = False

import scienceplots
plt.style.use(['science','ieee','notebook'])

# notebook嵌入图片
%matplotlib inline
# 提高分辨率
%config InlineBackend.figure_format='retina'

# 忽略警告
import warnings
warnings.filterwarnings('ignore')
x = np.arange(1,10,0.5)
y_1 = x * 2 + 4 + x ** 2
y_2 = x * 0.5 + 3 + x ** 1.5
y_3 = x * 3.5 + 2 + x ** 3
plt.figure()
plt.plot(x, y_1)
plt.plot(x, y_2)
plt.plot(x, y_3)
plt.xlabel('时间(s)')
plt.ylabel('电力情况(k)')
plt.show()

在这里插入图片描述

官方文档展示

  • 更多主题搭配可以在这里找到。
  • plt.style.use(['science'])
    请添加图片描述
  • plt.style.use(['science', 'ieee'])
    请添加图片描述
  • plt.style.use(['science', 'notebook'])
    请添加图片描述
  • plt.style.use(['science', 'scatter'])
    请添加图片描述
  • plt.style.use(['dark_background', 'science', 'high-vis'])
    请添加图片描述
  • plt.style.use(['science', 'bright'])
    请添加图片描述
  • plt.style.use(['science', 'vibrant'])
    请添加图片描述
  • plt.style.use(['science', 'muted'])
    请添加图片描述
  • plt.style.use(['science', 'retro'])
    请添加图片描述
  • plt.style.use(['science', 'grid'])
    请添加图片描述
  • plt.style.use(['science', 'high-contrast'])
    请添加图片描述
  • plt.style.use(['science', 'light'])
    请添加图片描述
  • plt.style.use(['science', 'no-latex', 'cjk-tc-font'])
    请添加图片描述
  • plt.style.use(['science', 'no-latex', 'cjk-sc-font'])
    请添加图片描述
  • plt.style.use(['science', 'no-latex', 'cjk-jp-font'])
    请添加图片描述
  • plt.style.use(['science', 'no-latex', 'cjk-kr-font'])
    请添加图片描述
  • plt.style.use(['science', 'russian-font'])
    请添加图片描述
  • plt.style.use(['science', 'turkish-font'])
    请添加图片描述
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羽星_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值