Python自学笔记——Matplotlib风羽自定义

上传人:碎****木 文档编号:220861781 上传时间:2021-12-09 格式:DOCX 页数:11 大小:29.21KB
返回 下载 相关 举报
Python自学笔记——Matplotlib风羽自定义_第1页
第1页 / 共11页
Python自学笔记——Matplotlib风羽自定义_第2页
第2页 / 共11页
Python自学笔记——Matplotlib风羽自定义_第3页
第3页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Python自学笔记——Matplotlib风羽自定义》由会员分享,可在线阅读,更多相关《Python自学笔记——Matplotlib风羽自定义(11页珍藏版)》请在金锄头文库上搜索。

1、Python 自学笔记Matplotlib 风羽自定义对于气象专业的学校生来说,风场是预报重要的参考数据,我们所知的风羽有四种:短线代表风速2m/s,长线代表风速 4m/s,空心三角代表风速 20m/s,实心三角代表风速50m/s。而 matplotlib 的风羽只有短线、长线、三角三种,而这里的三角不分空心实心,但是可通过转变风羽颜色为白色使三角变为空心外形,虽然这三种可以自定义各自代表的风速,但是仍与我们的使用习惯不符,即使把三角设成20m/s,原本一个实心三角就能表示的 50m/s 的风在 matplotlib 中需要两个三角外加两条长线一条短线。为了迎合预报员的需求,我在争辩了 mat

2、plotlib 的风场函数 barbs()的源代码 quiver.py 文件后,对 quiver.py 做了适当的调整,使得 matplotlib 也有了空心三角和实心三角之分。一、函数 barbs 的使用barb(X, Y, U, V, *kw) X:风场数据X 坐标Y:风场数据Y 坐标U:风的水平方向重量V:风的垂直方向重量”Demonstration of wind barb plots ”import matplotlib.pyplot as plt import numpy as npx = np.linspace(-5, 5, 5) X, Y = np.meshgrid(x, x)

3、U, V = 12*X, 12*Ydata = (-1.5, .5, -6, -6),(1, -1, -46, 46),(-3, -1, 11, -11),(1, 1.5, 80, 80),(0.5, 0.25, 25, 15),(-1.5, -0.5, -5, 40)data = np.array(data, dtype=(”x”, np.float32), (”y”, np.float32), (”u”, np.float32), (”v”, np.float32) # Default parameters, uniform gridax = plt.subplot(2, 2, 1) ax

4、.barbs(X, Y, U, V)# Arbitrary set of vectors, make them longer and change the pivot point #(point around which they”re rotated) to be the middleax = plt.subplot(2, 2, 2)ax.barbs(data”x”, data”y”, data”u”, data”v”, length=8, pivot=”middle”)# Showing colormapping with uniform grid. Fill the circle for

5、 an empty barb, # don”t round the values, and change some of the size parametersax = plt.subplot(2, 2, 3)ax.barbs(X, Y, U, V, np.sqrt(U*U + V*V), fill_empty=True, rounding=False,sizes=dict(emptybarb=0.25, spacing=0.2, height=0.3) # Change colors as well as the increments for parts of the barbsax = p

6、lt.subplot(2, 2, 4)ax.barbs(data”x”, data”y”, data”u”, data”v”, flagcolor=”r”,barbcolor=”b”, ”g”, barb_increments=dict(half=10, full=20, flag=100),flip_barb=True) plt.show()二、源代码解读1. class Barbs()class Barbs(mcollections.PolyCollection):docstring.interpddef init (self, ax, *args, *kw): ”.”def _find_

7、tails(self, mag, rounding=True, half=5, full=10, flag=50): ”.”def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,pivot, sizes, fill_empty, flip): ”.”def set_UVC(self, U, V, C=None): ”.”def set_offsets(self, xy): ”.”通过读源代码可知类 Barbs 有五个方法分别为 init 、_find_tails、_make_barbs、set_UVC

8、、set_offsets。2. init docstring.interpddef init (self, ax, *args, *kw): “The constructor takes one required argument, an Axes instance, followed by the args and kwargs described by the following pylab interface documentation:%(barbs_doc)s “self._pivot = kw.pop(”pivot”, ”tip”) self._length = kw.pop(”l

9、ength”, 7) barbcolor = kw.pop(”barbcolor”, None) flagcolor = kw.pop(”flagcolor”, None) self.sizes = kw.pop(”sizes”, dict() self.fill_empty = kw.pop(”fill_empty”, False)self.barb_increments = kw.pop(”barb_increments”, dict() self.rounding = kw.pop(”rounding”, True)self.flip = kw.pop(”flip_barb”, Fals

10、e)transform = kw.pop(”transform”, ax.transData)# Flagcolor and and barbcolor provide convenience parameters for # setting the facecolor and edgecolor, respectively, of the barb# polygon. We also work here to make the flag the same color as the # rest of the barb by defaultif None in (barbcolor, flag

11、color): kw”edgecolors” = ”face”if flagcolor:kw”facecolors” = flagcolor elif barbcolor:kw”facecolors” = barbcolorelse:# Set to facecolor passed in or default to black kw.setdefault(”facecolors”, ”k”)else:kw”edgecolors” = barbcolor kw”facecolors” = flagcolor# Parse out the data arrays from the various

12、 configurations supported x, y, u, v, c = _parse_args(*args)self.x = x self.y = yxy = np.hstack(x:, np.newaxis, y:, np.newaxis)# Make a collectionbarb_size = self._length * 2 / 4 # Empirically determined mcollections.PolyCollection. init (self, , (barb_size,),offsets=xy, transOffset=transform, *kw)s

13、elf.set_transform(transforms.IdentityTransform()self.set_UVC(u, v, c) init ()方法为初始化方法,此方法中flagcolor、barbcolor 为设置风羽颜色的关键字,中间的说明文字提示颜色设置是针对全部的风羽的,所以通过颜色设置达不到风羽中既有空心白色三角又有实心黑色三角。初始化方法中在对一些参数进展了初始化赋值后执行了set_UVC()方法,所以我们顺着这个 set_UVC()方法往下连续读。3.set_UVC()def set_UVC(self, U, V, C=None):self.u = ma.masked_

14、invalid(U, copy=False).ravel() self.v = ma.masked_invalid(V, copy=False).ravel() if C is not None:c = ma.masked_invalid(C, copy=False).ravel()x, y, u, v, c = delete_masked_points(self.x.ravel(),self.y.ravel(),self.u, self.v, c)else:x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(),sel

15、f.u, self.v)magnitude = np.hypot(u, v)flags, emptyflags,barbs, halves, empty = self._find_tails(magnitude,self.rounding,*self.barb_increments)# Get the vertices for each of the barbsplot_barbs = self._make_barbs(u, v, flags, emptyflags,barbs, halves, empty,self._length, self._pivot, self.sizes,self.fill_empty, self.flip)self.set_verts(plot_barbs)# Set the co

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号