详解Python matplotlib深度美化(第二期)
本次分享通过style sheets美化图表。
本期内容「绘图数据+代码+代码详细解释」已上传:
什么是style sheets?
matplotlib的style sheets可以简单的理解为是一组预定义的绘图元素属性配置文件(存储matplotlibrc文件中元素属性自定义值),用于快速调整图表的外观。matplotlib内置多种style sheets,mac系统存储在xxx/site-packages/matplotlib/mpl-data/stylelib路径下,以.mplstyle结尾。
点开一个ggplot的绘图风格ggplot.mplstyle看看 ,
patch.linewidth: 0.5
patch.facecolor: 348ABD # blue
patch.edgecolor: EEEEEE
patch.antialiased: Truefont.size: 10.0
axes.facecolor: E5E5E5
axes.edgecolor: white
axes.linewidth: 1
axes.grid: True
axes.titlesize: x-large
axes.labelsize: large
axes.labelcolor: 555555
axes.axisbelow: True # grid/ticks are below elements (e.g., lines, text)
axes.prop_cycle: cycler('color', ['E24A33', '348ABD', '988ED5', '777777', 'FBC15E', '8EBA42', 'FFB5B8'])
# E24A33 : red
# 348ABD : blue
# 988ED5 : purple
# 777777 : gray
# FBC15E : yellow
# 8EBA42 : green
# FFB5B8 : pink
xtick.color: 555555
xtick.direction: out
ytick.color: 555555
ytick.direction: out
grid.color: white
grid.linestyle: - # solid line
figure.facecolor: white
figure.edgecolor: 0.50
style sheets使用
通过plt.style.use可轻松使用style sheets,例如使用'Solarize_Light2'这种style sheets,
plt.style.use('Solarize_Light2')
不同的style sheets也可以混合使用,举个例子,
# 7.6.3_02
# -*- encoding: utf-8 -*-
'''
未经过允许禁止转载!
@Author : 公众号: pythonic生物人
@Desc : 7.6.3 如何使用style sheets
'''import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
x = np.linspace(-2 * np.pi, 2 * np.pi, 500)
y_cos = np.cos(x)
y_sin = np.sin(x)
mpl.rc_file_defaults()
fig = plt.figure(dpi=150, figsize=(8, 3))
fig.patch.set_edgecolor('grey')
fig.patch.set_linewidth(1)
# 第一个子图,使用ggplot风格
plt.style.use('grayscale') #使用ggplot风格
ax1 = plt.subplot(131)
ax1.plot(x, y_cos, label="cos(x)")
ax1.plot(x, y_sin, label="sin(x)")
ax1.set_title("grayscale style")
ax1.legend()
# 第二个子图,使用bmh风格
mpl.rc_file_defaults()
plt.style.use('bmh') #使用bmh风格
ax2 = plt.subplot(132)
ax2.plot(x, y_cos, label="cos(x)")
ax2.plot(x, y_sin, label="sin(x)")
ax2.set_title("bmh style")
ax2.legend()
# 第3个子图,使用混合风格
mpl.rc_file_defaults()
plt.style.use(['grayscale', 'bmh']) #使用grayscale混合bmh风格
ax2 = plt.subplot(133)
ax2.plot(x, y_cos, label="cos(x)")
ax2.plot(x, y_sin, label="sin(x)")
ax2.set_title("grayscale + bmh style")
ax2.legend()
plt.tight_layout()
plt.show()
有哪些style sheets?
使用plt.style.available可输出matplotlib所有内置style sheets名称,
# 7.6.2_01
# -*- encoding: utf-8 -*-
'''
未经过允许禁止转载!
@Author : 公众号: pythonic生物人
@Desc : 7.6.2 有哪些内置style sheets
'''import matplotlib.pyplot as plt
print(plt.style.available)
'Solarize_Light2', '_classic_test_patch','bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-v0_8', 'seaborn-v0_8-bright', 'seaborn-v0_8-colorblind', 'seaborn-v0_8-dark', 'seaborn-v0_8-dark-palette', 'seaborn-v0_8-darkgrid', 'seaborn-v0_8-deep', 'seaborn-v0_8-muted', 'seaborn-v0_8-notebook', 'seaborn-v0_8-paper', 'seaborn-v0_8-pastel', 'seaborn-v0_8-poster', 'seaborn-v0_8-talk', 'seaborn-v0_8-ticks', 'seaborn-v0_8-white', 'seaborn-v0_8-whitegrid', 'tableau-colorblind10'
共计26种,展示一下各类style sheets效果,
本期内容「绘图数据+代码+代码详细解释」已上传:
👇加入学习(备注:399),