Proplot对matplotlib进行了高度的封装,是一个高级绘图工具,其功能相当强大!而且融和了cartopy、basemap、xarray和pandas。看到这里这应该就是我一直想要的绘图工具了!
好了,啥也别说了!请让我趴地上把下面的内容介绍完!
如果你满足以下条件,那么Proplot是非常适合你的:
?经常绘图,而且包含很多复杂的子图?经常需要对图进行标注和美化?几乎每天都要创建新的图形
Proplot列出了matplotlib的很多不友好的方面,并通过封装来解决这些问题,提供更友好的交互方式。
?更少的代码,更多的图
引入format方法去除了繁琐的图形设置问题,使用更少的代码,高度自定义图形。
示例
Proplot
import proplot as plotf, axs = plot.subplots(ncols=2)axs.format(linewidth=1, color=’gray’)axs.format(xticks=20, xtickminor=True, xlabel=’x axis’, ylabel=’y axis’)
matplotlib
import matplotlib.ticker as mtickerfrom matplotlib import rcParamsrcParams[‘axes.linewidth’] = 1rcParams[‘axes.color’] = ‘gray’fig, axs = plt.subplots(ncols=2)for ax in axs:ax.xaxis.set_major_locator(mticker.MultipleLocator(10))ax.tick_params(width=1, color=’gray’, labelcolor=’gray’)ax.tick_params(axis=’x’, which=’minor’, bottom=True)ax.set_xlabel(‘x axis’, color=’gray’)ax.set_ylabel(‘y axis’, color=’gray’)plt.style.use(‘default’) # restore
?类构造函数
通过类构造函数对类名较长,书写不友好的类进行了封装注册,并提供关键词参数进行自定义。
?自动化维度和图形间距
添加新的设置选项控制图形的维度和间距,以更好的解决多子图所带来的图形间距问题。比自带的tightlayout更友好。
?去除冗余信息 matplotlib的子图share参数可以让子图共享轴,但是对于轴的标签、legend和colorbar等信息却无法进行处理,Proplot引入了新的Figure、colorbar和legend`方法处理这种情况,使多子图绘图更简洁。
?设置外部colorbar和legend matplotlib中为多个子图设置colorbar和legend时是非常麻烦的,尤其是需要自定义位置时。Proplot引入了新的框架处理此类问题。
import proplot as plotimport numpy as npplot.rc.cycle = ‘538’labels = [‘a’, ‘bb’, ‘ccc’, ‘dddd’, ‘eeeee’]f, axs = plot.subplots(ncols=2, span=False, share=1, axwidth=2)hs1, hs2 = [], []# On-the-fly legendsstate = np.random.RandomState(51423)for i, label in enumerate(labels):data = (state.rand(20) – 0.45).cumsum(axis=0)h1 = axs[0].plot(data, lw=4, label=label, legend=’ul’,legend_kw={‘order’: ‘F’, ‘title’: ‘column major’})hs1.extend(h1)h2 = axs[1].plot(data, lw=4, label=label, legend=’r’, cycle=’Set3′,legend_kw={‘ncols’: 1, ‘frame’: False, ‘title’: ‘no frame’})hs2.extend(h2)# Outer legendsax = axs[0]ax.legend(hs1, loc=’b’, ncols=3, title=’row major’, order=’C’,facecolor=’gray2′)ax = axs[1]ax.legend(hs2, loc=’b’, ncols=3, center=True, title=’centered rows’)axs.format(xlabel=’xlabel’, ylabel=’ylabel’, suptitle=’Legend formatting demo’)
?新的及改进后的绘图方法
matplotlib默认的设置画图是真的难看,pandas、xarray和seaborn都进行改进,Proplot则将这些改进又进行了进一步封装优化。
import proplot as plotimport numpy as np# Pcolor plot with and without distinct levelsf, axs = plot.subplots(ncols=2, axwidth=2)state = np.random.RandomState(51423)data = (state.normal(0, 1, size=(33, 33))).cumsum(axis=0).cumsum(axis=1)axs.format(suptitle=’Pcolor plot with levels’)for ax, n, mode, side in zip(axs, (200, 10), (‘Ambiguous’, ‘Discernible’), ‘lr’):ax.pcolor(data, cmap=’spectral’, N=n, symmetric=True, colorbar=side)ax.format(title=f'{mode} level boundaries’, yformatter=’null’)
?colormap和属性循环
matplotlib中的 ListedColormap[1] 和 LinearSegmentedColormap[2] 着实难用。Proplot提供了更加友好的方法,而且提供了新的方法来创建colormap。
?更智能的colormap归一化
Proplot提供了更方便的函数来处理colormap的归一化以及延伸的问题。
import proplot as plotimport numpy as npf, axs = plot.subplots([[0, 0, 1, 1, 0, 0], [2, 3, 3, 4, 4, 5]],wratios=(1.5, 0.5, 1, 1, 0.5, 1.5), axwidth=1.7, ref=1, right=’2em’)axs.format(suptitle=’BinNorm color-range standardization’)levels = plot.arange(0, 360, 45)state = np.random.RandomState(51423)data = (20*(state.rand(20, 20) – 0.4).cumsum(axis=0).cumsum(axis=1)) % 360# Cyclic colorbar with distinct end colorsax = axs[0]ax.pcolormesh(data, levels=levels, cmap=’phase’, extend=’neither’,colorbar=’b’, colorbar_kw={‘locator’: 90})ax.format(title=’cyclic colormapnwith distinct end colors’)# Colorbars with different extend valuesfor ax, extend in zip(axs[1:], (‘min’, ‘max’, ‘neither’, ‘both’)):ax.pcolormesh(data[:, :10], levels=levels, cmap=’oxy’,extend=extend, colorbar=’b’, colorbar_kw={‘locator’: 90})ax.format(title=f’extend={extend!r}’)
文章知识点与官方知识档案匹配,可进一步学习相关知识Python入门技能树首页概览208512 人正在系统学习中 相关资源:SAI绘画软件v1.3.1.0汉化绿色免费版_SAI-其它代码类资源-CSDN文库
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!