示例#1
0
    def box(self, dir, var, **kwargs):
        """
        Plot a windrose in proportional bar mode. For each var bins and for each
        sector, a colored bar will be draw on the axes.

        Mandatory:
        * dir : 1D array - directions the wind blows from, North centred
        * var : 1D array - values of the variable to compute. Typically the wind
        speeds
        Optional:
        * nsector: integer - number of sectors used to compute the windrose
        table. If not set, nsectors=16, then each sector will be 360/16=22.5°,
        and the resulting computed table will be aligned with the cardinals
        points.
        * bins : 1D array or integer- number of bins, or a sequence of
        bins variable. If not set, bins=6 between min(var) and max(var).
        * blowto : bool. If True, the windrose will be pi rotated,
        to show where the wind blow to (usefull for pollutant rose).
        * colors : string or tuple - one string color ('k' or 'black'), in this
        case all bins will be plotted in this color; a tuple of matplotlib
        color args (string, float, rgb, etc), different levels will be plotted
        in different colors in the order specified.
        * cmap : a cm Colormap instance from matplotlib.cm.
          - if cmap == None and colors == None, a default Colormap is used.
        edgecolor : string - The string color each edge bar will be plotted.
        Default : no edgecolor

        """

        bins, nbins, nsector, colors, angles, kwargs = self._init_plot(
            dir, var, **kwargs)
        null = popd(kwargs, 'facecolor', None)
        edgecolor = popd(kwargs, 'edgecolor', None)
        if edgecolor is not None:
            if not isinstance(edgecolor, str):
                raise ValueError('edgecolor must be a string color')
        opening = N.linspace(0.0, N.pi / 16, nbins)

        for j in range(nsector):
            offset = 0
            for i in range(nbins):
                if i > 0:
                    offset += self._info['table'][i - 1, j]
                val = self._info['table'][i, j]
                zorder = nbins - i
                patch = Rectangle((angles[j] - opening[i] / 2, offset),
                                  opening[i],
                                  val,
                                  facecolor=colors[i],
                                  edgecolor=edgecolor,
                                  zorder=zorder,
                                  **kwargs)
                self.add_patch(patch)
                if j == 0:
                    self.patches_list.append(patch)
        self._update()
示例#2
0
    def contourf(self, dir, var, **kwargs):
        """
        Plot a windrose in filled mode. For each var bins, a line will be
        draw on the axes, a segment between each sector (center to center).
        Each line can be formated (color, width, ...) like with standard plot
        pylab command.

        Mandatory:
        * dir : 1D array - directions the wind blows from, North centred
        * var : 1D array - values of the variable to compute. Typically the wind
        speeds
        Optional:
        * nsector: integer - number of sectors used to compute the windrose
        table. If not set, nsectors=16, then each sector will be 360/16=22.5°,
        and the resulting computed table will be aligned with the cardinals
        points.
        * bins : 1D array or integer- number of bins, or a sequence of
        bins variable. If not set, bins=6, then
            bins=linspace(min(var), max(var), 6)
        * blowto : bool. If True, the windrose will be pi rotated,
        to show where the wind blow to (usefull for pollutant rose).
        * colors : string or tuple - one string color ('k' or 'black'), in this
        case all bins will be plotted in this color; a tuple of matplotlib
        color args (string, float, rgb, etc), different levels will be plotted
        in different colors in the order specified.
        * cmap : a cm Colormap instance from matplotlib.cm.
          - if cmap == None and colors == None, a default Colormap is used.

        others kwargs : see help(pylab.plot)

        """

        bins, nbins, nsector, colors, angles, kwargs = self._init_plot(
            dir, var, **kwargs)
        null = popd(kwargs, 'facecolor', None)
        null = popd(kwargs, 'edgecolor', None)
        offset = 0
        for i in range(nbins):
            val = self._info['table'][i, :] + offset
            offset += self._info['table'][i, :]
            zorder = nbins - i
            patch = self.fill(angles,
                              val,
                              facecolor=colors[i],
                              edgecolor=colors[i],
                              zorder=zorder,
                              **kwargs)
            self.patches_list.extend(patch)
示例#3
0
    def set_radii_angle(self, **kwargs):
        """
        Set the radii labels angle
        """

        null = popd(kwargs, 'labels', None)
        angle = popd(kwargs, 'angle', None)
        if angle is None:
            angle = self.radii_angle
        self.radii_angle = angle
        radii = N.linspace(0.1, self.get_rmax(), 6)
        radii_labels = ["%.1f" % r for r in radii]
        radii_labels[0] = ""  #Removing label 0
        null = self.set_rgrids(radii=radii,
                               labels=radii_labels,
                               angle=self.radii_angle,
                               **kwargs)
示例#4
0
    def _init_plot(self, dir, var, **kwargs):
        """
        Internal method used by all plotting commands
        """
        #self.cla()
        null = popd(kwargs, 'zorder', None)

        #Init of the bins array if not set
        bins = popd(kwargs, 'bins', None)
        if bins is None:
            bins = N.linspace(N.min(var), N.max(var), 6)
        if isinstance(bins, int):
            bins = N.linspace(N.min(var), N.max(var), bins)
        nbins = len(bins)

        #Init of the number of sectors
        nsector = popd(kwargs, 'nsector', None)
        if nsector is None:
            nsector = 16

        #Sets the colors table based on the colormap or the "colors" argument
        colors = popd(kwargs, 'colors', None)
        cmap = popd(kwargs, 'cmap', None)
        if colors is not None:
            if isinstance(colors, str):
                colors = [colors] * nbins
            if isinstance(colors, (tuple, list)):
                if len(colors) != nbins:
                    raise ValueError("colors and bins must have same length")
        else:
            if cmap is None:
                cmap = cm.jet
            colors = self._colors(cmap, nbins)

        #Building the list of angles
        angles = N.arange(0, -2 * N.pi, -2 * N.pi / nsector) + N.pi / 2

        normed = popd(kwargs, 'normed', False)
        blowto = popd(kwargs, 'blowto', False)

        #Set the global information dictionnary
        self._info['dir'], self._info['bins'], self._info['table'] = histogram(
            dir, var, bins, nsector, normed, blowto)

        return bins, nbins, nsector, colors, angles, kwargs
示例#5
0
    def legend(self, loc='lower left', **kwargs):
        """
        Sets the legend location and her properties.
        The location codes are

          'best'         : 0,
          'upper right'  : 1,
          'upper left'   : 2,
          'lower left'   : 3,
          'lower right'  : 4,
          'right'        : 5,
          'center left'  : 6,
          'center right' : 7,
          'lower center' : 8,
          'upper center' : 9,
          'center'       : 10,

        If none of these are suitable, loc can be a 2-tuple giving x,y
        in axes coords, ie,

          loc = (0, 1) is left top
          loc = (0.5, 0.5) is center, center

        and so on.  The following kwargs are supported:

        isaxes=True           # whether this is an axes legend
        prop = FontProperties(size='smaller')  # the font property
        pad = 0.2             # the fractional whitespace inside the legend border
        shadow                # if True, draw a shadow behind legend
        labelsep = 0.005     # the vertical space between the legend entries
        handlelen = 0.05     # the length of the legend lines
        handletextsep = 0.02 # the space between the legend line and legend text
        axespad = 0.02       # the border between the axes and legend edge
        """
        def get_handles():
            handles = list()
            for p in self.patches_list:
                if isinstance(p, matplotlib.patches.Polygon) or \
                isinstance(p, matplotlib.patches.Rectangle):
                    color = p.get_facecolor()
                elif isinstance(p, matplotlib.lines.Line2D):
                    color = p.get_color()
                else:
                    raise AttributeError("Can't handle patches")
                handles.append(
                    Rectangle((0, 0),
                              0.2,
                              0.2,
                              facecolor=color,
                              edgecolor='black'))
            return handles

        def get_labels():
            labels = N.copy(self._info['bins'])
            labels = ["[%.1f : %0.1f[" %(labels[i], labels[i+1]) \
                      for i in range(len(labels)-1)]
            return labels

        null = popd(kwargs, 'labels', None)
        null = popd(kwargs, 'handles', None)
        handles = get_handles()
        labels = get_labels()
        self.legend_ = matplotlib.legend.Legend(self, handles, labels, loc,
                                                **kwargs)
        return self.legend_