引言
作为一个简洁的 Matplotlib 包装器,ProPlot 库 是 Matplotlib 面向对象绘图方法的高级封装,整合了cartopy/Basemap 地图库、xarray 和 pandas,可弥补 Matplotlib 的部分缺陷,让 Matplotlib 爱好者拥有更加顺滑的绘图体验。
1.多子图绘制处理
如图(d)所示,设置 Y 轴共享方式为 True 时的样式,此时,轴标签、刻度标签都实现了共享。
2.颜色条和图例
以个专门用于绘制单个子图或多个连续子图的颜色条和图例的简单框架,该框架将位置参数传递给 ProPlot 的 axes.Axes.colorbar 或 axes.Axes.legend,完成特定子图不同位置颜色条或图例的绘制。想要沿图形边缘绘制颜色条或图例,使用 proplot.figure.Figure.colorbar 和 proplot.figure.Figure.legend 即可。
如下图所示,为 Figure 对象的颜色条和图例的绘制效果图。
代码如下(示例):
import pandas as pd
import numpy as np
import proplot as pplt
import matplotlib.pyplot as plt
fig = pplt.figure(share=False, refwidth=2.3)
# Colorbars
ax = fig.subplot(121)
state = np.random.RandomState(51423)
m = ax.heatmap(state.rand(10, 10), colorbar='t', cmap='grays')
ax.colorbar(m, loc='r')
ax.colorbar(m, loc='ll', label='colorbar label')
ax.format(title='Axes colorbars')
# Legends
ax = fig.subplot(122)
ax.format(title='Axes legends', titlepad='0em')
hs = ax.plot(
(state.rand(10, 5) - 0.5).cumsum(axis=0), linewidth=3,
cycle='ggplot', legend='t',
labels=list('abcde'), legend_kw={'ncols': 5, 'frame': False}
)
ax.legend(hs, loc='r', ncols=1, frame=False)
ax.legend(hs, loc='ll', label='legend label')
fig.format(abc="(a)", abcloc="ul", abcsize=15,
xlabel='xlabel', ylabel='ylabel')
plt.show()
如下图所示,为Axes 对象的颜色条和图例的绘制效果图。
3.美观的颜色和字体
ProPlot 库封装了大量的颜色映射选项,不但提供了来自 Seaborn、cmOcean、SciVisColor 等拓展包和 Scientific colour maps 等项目中的多个颜色映射选项,而且定义了一些默认颜色选项和一个用于生成新颜色条的PerceptualColormap 类。
下图为使用 ProPlot 的不同颜色映射选项绘制的不同颜色映射的效果图。
代码如下(示例):
import pandas as pd
import numpy as np
import proplot as pplt
import matplotlib.pyplot as plt
fig = pplt.figure(share=False, refwidth=2.3)
# Colorbars
ax = fig.subplot(231)
state = np.random.RandomState(51423)
data = 1 + (state.rand(12, 10) - 0.45).cumsum(axis=0)
m = ax.heatmap(state.rand(10, 10), cmap='grays')
ax.colorbar(m, loc='ll', label='grays colorbar')
ax = fig.subplot(232)
m = ax.heatmap(state.rand(10, 10), cmap='viridis')
ax.colorbar(m, loc='ll', label='viridis colorbar')
ax = fig.subplot(233)
m = ax.heatmap(state.rand(10, 10), cmap='mako')
ax.colorbar(m, loc='ll', label='mako colorbar')
ax = fig.subplot(234)
m = ax.heatmap(state.rand(10, 10), cmap='marine')
ax.colorbar(m, loc='ll', label='marine colorbar')
ax = fig.subplot(235)
m = ax.heatmap(state.rand(10, 10), cmap='dense')
ax.colorbar(m, loc='ll', label='dense colorbar')
ax = fig.subplot(236)
m = ax.heatmap(state.rand(10, 10), cmap='batlow')
ax.colorbar(m, loc='ll', label='batlow colorbar')
fig.format(abc="(a)", abcloc="ul", abcsize=15,
xlabel='xlabel', ylabel='ylabel', labelsize=15)
plt.show()
下图 为 ProPlot 中部分字体绘制的可视化结果。
代码如下(示例):
import pandas as pd
import numpy as np
import proplot as pplt
import matplotlib.pyplot as plt
fig = pplt.figure(share=False, refwidth=2.3)
# Colorbars
ax = fig.subplot(231)
state = np.random.RandomState(51423)
data = 1 + (state.rand(12, 10) - 0.45).cumsum(axis=0)
m = ax.heatmap(state.rand(10, 10), cmap='grays')
ax.colorbar(m, loc='ll', label='grays colorbar')
ax = fig.subplot(232)
m = ax.heatmap(state.rand(10, 10), cmap='viridis')
ax.colorbar(m, loc='ll', label='viridis colorbar')
ax = fig.subplot(233)
m = ax.heatmap(state.rand(10, 10), cmap='mako')
ax.colorbar(m, loc='ll', label='mako colorbar')
ax = fig.subplot(234)
m = ax.heatmap(state.rand(10, 10), cmap='marine')
ax.colorbar(m, loc='ll', label='marine colorbar')
ax = fig.subplot(235)
m = ax.heatmap(state.rand(10, 10), cmap='dense')
ax.colorbar(m, loc='ll', label='dense colorbar')
ax = fig.subplot(236)
m = ax.heatmap(state.rand(10, 10), cmap='batlow')
ax.colorbar(m, loc='ll', label='batlow colorbar')
fig.format(abc="(a)", abcloc="ul",abcsize=15,
xlabel='xlabel', ylabel='ylabel',labelsize=15)
plt.show()
以上内容均来自《科研论文绘图》 宁海涛 著