论绘图,R一直压过Python一头,尤其是Python基础绘图库Matplotlib始终打不过ggplot2。
偶然看到Matplotlib风格美化神器——Aquarel,效果十分惊艳,先睹为快:![]()
作为一名理工男我觉得这太酷了,很符合我对未来生活的想象,科技并带着趣味。
说回这个神器,Aquarel是一个用于可视化的轻量级模板引擎,也是Matplotlibs的rcparams包装器,使绘图的样式变得简单。Aquarel模板可以通过编程定义,并以JSON格式进行序列化和共享。
用起来也极简单,举个例子,我们先用Matplotlib随便画个箱线图
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-7, 7, 140)
x = np.hstack([-25, x, 25])
fig, ax = plt.subplots()
ax.boxplot([x, x], notch=True, capwidths=[0.01, 0.2])
plt.show()![]()
加上aquarel的美化呢,他内置了11个风格,大家挑选自己喜欢的即可,比如就选arctic_light
这里有两种常用方法,一种是全局设置主题
from aquarel import load_theme
theme = load_theme("umbra_light")
theme.apply()
fig, ax = plt.subplots()
ax.boxplot([x, x], notch=True, capwidths=[0.01, 0.2])
theme.apply_transforms()
另一种是使用上下文管理器,个人比较推荐
from aquarel import load_theme
with load_theme("umbra_light"):
x = np.linspace(-7, 7, 140)
x = np.hstack([-25, x, 25])
fig, ax = plt.subplots()
ax.boxplot([x, x], notch=True, capwidths=[0.01, 0.2])
plt.show()
效果:
其实就是把正常的绘图代码段插进去就行了
其他风格大家可以自行探索吧,文末附开源代码和官方文档
开源地址:https://github.com/lgienapp/aquarel
官方教程:https://aquarel.readthedocs.io/en/latest/
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.