1、Demonstration of wind barb plotsimport matplotlib.pyplot as pltimport numpy as npx = np.linspace(-5, 5, 5)X, Y = np.meshgrid(x, x)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.
2、float32), (yuv, np.float32)# Default parameters, uniform gridax = plt.subplot(2, 2, 1)ax.barbs(X, Y, U, V)# Arbitrary set of vectors, make them longer and change the pivot point#(point around which theyre rotated) to be the middleax = plt.subplot(2, 2, 2)ax.barbs(data, data, length=8, pivot=middle)#
3、 Showing colormapping with uniform grid. Fill the circle for an empty barb,# dont 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 colo
4、rs as well as the increments for parts of the barbsax = plt.subplot(2, 2, 4), 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.interpd def _init_(self, ax, *args, *kw): . de
5、f _find_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、set_of
6、fsets。2._init_ 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(pivottip self._length = kw.pop(length, 7) barbcolor = kw.pop(barbcolor, None) flagcolor = kw.pop(
7、flagcolor self.sizes = kw.pop(sizes, dict() self.fill_empty = kw.pop(fill_empty, False) self.barb_increments = kw.pop(barb_increments self.rounding = kw.pop(rounding, True) self.flip = kw.pop(flip_barb transform = kw.pop(transform, ax.transData) # Flagcolor and and barbcolor provide convenience para
8、meters 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 default if None in (barbcolor, flagcolor): kwedgecolors = face if flagcolor:facecolors = flagcolor elif barbcolor: = barbcolor else: #
9、 Set to facecolor passed in or default to black kw.setdefault(k # Parse out the data arrays from the various configurations supported x, y, u, v, c = _parse_args(*args) self.x = x self.y = y xy = np.hstack(x:, np.newaxis, y:, np.newaxis) # Make a collection barb_size = self._length * 2 / 4 # Empiric
10、ally determined mcollections.PolyCollection._init_(self, , (barb_size,), offsets=xy, transOffset=transform, *kw) self.set_transform(transforms.IdentityTransform() self.set_UVC(u, v, c)_init_()方法为初始化方法,此方法中flagcolor、barbcolor为设置风羽颜色的关键字,中间的说明文字提示颜色设置是针对所有的风羽的,所以通过颜色设置达不到风羽中既有空心白色三角又有实心黑色三角。初始化方法中在对一些
11、参数进行了初始化赋值后执行了set_UVC()方法,所以我们顺着这个set_UVC()方法往下继续读。3.set_UVC() self.u = ma.masked_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.
12、u, self.v, c) x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(), self.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 barbs plot_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 color array self.set_array(c) # Update the offsets
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1