def __init__(self, fcst, velocity_variable, speed_units='knots', ax=None, fig=None, **kwdargs): self.variable = velocity_variable self.speed_units = speed_units # set the default colormap if needed kwdargs['cmap'] = kwdargs.get('cmap', velocity_cmap) # convert the bins to knots bins = xray.Variable('bins', velocity_variable.speed_bins.copy(), {'units': velocity_variable.units}) _, self.bins, _ = units.convert_units(bins, speed_units) default_norm = plt.cm.colors.BoundaryNorm(self.bins, self.bins.size) kwdargs['norm'] = kwdargs.get('norm', default_norm) self.variable = velocity_variable self.ax, self.fig = utils.axis_figure(ax, fig) # add the color bar self.cax = self.fig.add_axes([0.92, 0.05, 0.03, 0.9]) cbar = mpl.colorbar.ColorbarBase(self.cax, cmap=kwdargs['cmap'], norm=kwdargs['norm']) cbar.set_label("Knots") fcst = self.normalize(fcst) # use the longitude grid to define the radius of the circles sorted_lats = np.sort(fcst['latitude'].values) resol = np.median(angles.angle_diff(sorted_lats[1:], sorted_lats[:-1])) # create the map self.m = utils.get_basemap(fcst, ax=self.ax, lon_pad=0.75 * resol, lat_pad = 0.75 * resol) def create_circle(one_loc): # determine the circle center x, y = self.m(one_loc['longitude'].values, one_loc['latitude'].values) speeds = one_lonlat[self.variable.speed_name].values dirs = one_lonlat[self.variable.direction_name].values orientation = self.variable.direction_orientation return DirectionCircle(x, y, speeds=np.atleast_1d(speeds), directions=np.atleast_1d(dirs), orientation=orientation, radius=0.4 * resol, ax=self.ax, **kwdargs) self.circles = [[create_circle(one_lonlat) for lo, one_lonlat in one_lat.groupby('longitude')] for la, one_lat in fcst.groupby('latitude')] self.set_title(fcst)
def template_temporal_plot(fcst, ax=None, fig=None): """ Adds a human readable time axis to a figure. """ ax, fig = utils.axis_figure(ax, fig) # set the x-axis to show time str_times = [x.strftime('%m-%d %Hh') for x in pd.to_datetime(fcst['time'].values)] ax.xaxis.set_ticks(np.arange(len(str_times)) + 0.5) ax.xaxis.set_ticklabels(str_times, rotation=90) ax.set_xlabel("Forecast Time (UTC)") return ax
def binned_line_plot(variable, bin_divs, ax=None, **kwdargs): """ Creates a plot showing the binned probability of the data in variable. """ ax, _ = utils.axis_figure(axis=ax) assert variable.dims == ('time', 'realization') n_times, n_real = variable.shape bins = bin_matrix(variable.values, bin_divs) xs = np.arange(n_times).repeat(n_real).reshape(bins.shape) lines = plt.plot(xs + 0.5, bins - 0.5, **kwdargs) return lines
def __init__(self, x, y, speeds, directions, radius, cmap=None, norm=None, ax=None, fig=None, arrow_alpha=0.7, circle_alpha=0.6, orientation='from'): self.axis, self.fig = utils.axis_figure(ax, fig) self.x = x self.y = y self.center = np.array([self.x, self.y]).flatten() self.radius = radius self.cm = cmap or plt.cm.get_cmap('Blues') self.norm = norm or plt.Normalize() self.arrow_alpha = arrow_alpha self.circle_alpha = circle_alpha assert orientation in ['from', 'to'] self.orientation = orientation self.polys = self._build_polys(speeds, directions) self.circle = self._build_circle(speeds) self.axis.add_patch(self.circle) [self.axis.add_patch(poly) for poly in self.polys]
def __init__(self, fcsts, velocity_variable, speed_units='knot', max_speed=None, ax=None, fig=None): self.variable = velocity_variable self.speed_units = speed_units self.ax, self.fig = utils.axis_figure(ax, fig) self.ax.set_ylabel("Speed (%s)" % speed_units) # convert the bins to knots bins = xray.Variable('bins', velocity_variable.speed_bins.copy(), {'units': velocity_variable.units}) _, self.bins, _ = units.convert_units(bins, speed_units) # convert the data to knots and radians fcsts = self.normalize(fcsts) # determine the upper bound on speed so we can place # the direction circles. all_speeds =fcsts[self.variable.speed_name].values max_speed = max_speed or np.max(all_speeds) max_bin = np.sum(self.bins <= max_speed) + 2 self.max_bin = np.minimum(max_bin, self.bins.size) self.plot(fcsts)
def binned_probability_plot(variable, bin_divs, ax=None, **kwdargs): """ Creates a plot showing the binned probability of the data in variable. """ ax, _ = utils.axis_figure(axis=ax) if variable.dims == ('time', ): variable = (xray.concat([variable], 'realization') .transpose('time', 'realization')) assert variable.dims == ('time', 'realization') n_times, n_real = variable.shape # compute the binned probabilities probs = bin_probs(variable, bin_divs) # default to a blue colormap placed in the background kwdargs['cmap'] = kwdargs.get('cmap', plt.cm.get_cmap('Blues')) kwdargs['zorder'] = kwdargs.get('zorder', -100) # plot the probabilities y, x = np.meshgrid(np.arange(bin_divs.size), np.arange(variable['time'].size + 1)) pm = ax.pcolormesh(x, y, probs.values, norm=plt.Normalize(vmin=0., vmax=1.), **kwdargs) return pm