def _plot_map_as_subplots(i_indices, j_indices, extremetype_retperiod_cv_map, title = ""): plt.figure() i_subplot = 1 plt.subplots_adjust(hspace = 0.2, wspace = 0.2) #TODO: plot CV for the current and future climates plt.figtext(0.5, 0.05, title, horizontalalignment='center') max_value = 0.1 for extreme_type, ret_period_to_cv in extremetype_retperiod_cv_map.items(): #max_value = 0.5 if extreme_type == 'low' else 2.5 for ret_period, cv_field in ret_period_to_cv.items(): to_plot = np.ma.masked_all(csfb.xs.shape) for i, j, cv in zip(i_indices, j_indices, cv_field): to_plot[i, j] = cv plt.subplot(2,2, i_subplot) i_subplot += 1 cMap = mpl.cm.get_cmap('jet', 3) cMap.set_over(color = '#FF8C00') basin_boundaries.plot_basin_boundaries_from_shape(csfb.basemap, plotter = plt, linewidth = 1.6) image = csfb.basemap.pcolormesh(csfb.xs, csfb.ys, to_plot, vmin = 0, vmax = 1.5, cmap = cMap) plot_axes = plt.gca() divider = make_axes_locatable(plot_axes) cax = divider.append_axes("right", "8%", pad="3%") # extend choices are "both", "min", "max" and "neither" cb = plt.colorbar(image, extend = 'max', ticks = LinearLocator(numticks = 4), format = "%.2f", drawedges = True, cax = cax) cb.outline.set_visible(False) csfb.basemap.drawcoastlines(linewidth = 0.2) plt.title('Return period: {0} years,\n {1} flow event.'.format(ret_period, extreme_type)) x_min, x_max, y_min, y_max = plot_utils.get_ranges(csfb.xs[i_indices, j_indices], csfb.ys[i_indices, j_indices]) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max)
def plot_data(data = None, imagefile = 'image.png', units = 'm**3/s', minmax = (None, None), indices_file = 'data/streamflows/hydrosheds_euler9/aex_discharge_1970_01_01_00_00.nc', color_map = mpl.cm.get_cmap('RdBu', 20), ticks_locator = LinearLocator(), i_list = None, j_list = None, title = ''): if imagefile is not None: plt.clf() if i_list is None or j_list is None: i_list, j_list = data_select.get_indices_from_file(indices_file) to_plot = np.ma.masked_all(xs.shape) for i, j, value in zip(i_list, j_list, data): to_plot[i, j] = value plt.title(title) m.pcolormesh(xs,ys,to_plot.copy(), cmap = color_map, edgecolors = 'None', antialiased = True, vmin = minmax[0], vmax = minmax[1]) m.drawcoastlines() boundaries.plot_basin_boundaries_from_shape(m, plotter = plt, linewidth = 0.5) draw_meridians_and_parallels(m, 10) # plot_directions(data_mask = to_plot) cb = plt.colorbar(ticks = ticks_locator, format = "%.1f") cb.ax.set_ylabel(units) x1, x2, y1, y2 = plot_utils.get_ranges(xs[i_list, j_list], ys[i_list, j_list]) plt.xlim(x1, x2) plt.ylim(y1, y2) if imagefile is not None: plt.savefig(imagefile, bbox_inches = 'tight')
def plot_signal_to_noise_ratio(): low_periods = [2, 5] high_periods = [10, 30] plot_utils.apply_plot_params(width_pt=None, font_size=9, aspect_ratio=2.5) extreme_types = ['high', 'low'] extreme_type_to_periods = dict(list(zip(extreme_types, [high_periods, low_periods]))) i_indices, j_indices = data_select.get_indices_from_file() i_subplot = 0 #plt.subplots_adjust(wspace = 0.1) cMap = mpl.cm.get_cmap('jet', 3) cMap.set_over(color = '#FF8C00') gs = gridspec.GridSpec(3,2) for extreme_type in extreme_types: for return_period in extreme_type_to_periods[extreme_type]: changes = np.zeros((len(members.current_ids), len(i_indices))) for member_num, the_member in enumerate(members.current_ids): pars_current = csfb.get_pars_for_member_and_type(the_member, level_type = extreme_type) pars_future = csfb.get_pars_for_member_and_type(members.current2future[the_member], level_type = extreme_type) #calculate changes for each pair of members and each position npos = len(pars_current) for pos, pc, pf in zip(range(npos), pars_current, pars_future): if extreme_type == 'high': c = gevfit.get_high_ret_level_stationary(pc, return_period) f = gevfit.get_high_ret_level_stationary(pf, return_period) else: c = gevfit.get_low_ret_level_stationary(pc, return_period) f = gevfit.get_low_ret_level_stationary(pf, return_period) changes[member_num, pos] = f - c pass #calculate mean and stdev of the obtained changes the_mean = np.mean(changes, axis = 0) the_std = np.std(changes, axis = 0) #change if you want signal to noise ratio, currently it is cv (coefficient of variation 1/(signal-to-noise-ratio)) the_values = the_std / np.abs(the_mean) print(the_values.min(), the_values.max()) #the_values = the_mean to_plot = np.ma.masked_all(csfb.xs.shape) max_value = 1.5 for i, j, value, dev in zip(i_indices, j_indices, the_values, the_std): to_plot[i, j] = value #shaded = np.ma.masked_where(to_plot != 0, shaded) #to_plot = np.ma.masked_where(to_plot == 0, to_plot) plot_axes = plt.subplot(gs[i_subplot // 2, i_subplot % 2]) i_subplot += 1 print('just before plotting') basin_boundaries.plot_basin_boundaries_from_shape(csfb.basemap, plotter = plt, linewidth = 1.) image = csfb.basemap.pcolormesh(csfb.xs, csfb.ys, to_plot, vmin = 0, vmax = max_value, cmap = cMap, ax = plot_axes) csfb.basemap.drawcoastlines(linewidth = 0.2) plot_axes.set_title('T = {0}-year'.format(return_period)) x_min, x_max, y_min, y_max = plot_utils.get_ranges(csfb.xs[i_indices, j_indices], csfb.ys[i_indices, j_indices]) plot_axes.set_xlim(x_min, x_max) plot_axes.set_ylim(y_min, y_max) divider = make_axes_locatable(plot_axes) cax = divider.append_axes("right", "8%", pad="3%") # extend choices are "both", "min", "max" and "neither" cb = plt.colorbar(image, extend = 'max', format = "%.1f", drawedges = True, cax = cax) cb.set_ticks(LinearLocator(numticks = 4)) cb.outline.set_visible(False) #plt.show() plt.tight_layout() plt.savefig('cv_for_changes.png')
def plot(data_1d, i_indices, j_indices, xs, ys, title = '', label = "", minmax = (None, None), color_map = mpl.cm.get_cmap('RdBu'), units = '', colorbar_orientation = 'vertical' , basemap = None, colorbar_tick_locator = LinearLocator(numticks = 6), colorbar_label_format = '%.1f', upper_limited = False, not_significant_mask = None, show_colorbar = True, impose_lower_limit = None, imagefile = None, ax = None): fig = None if imagefile is not None: fig = plt.figure() to_plot = np.ma.masked_all(xs.shape) sign_mask_2d = np.ma.masked_all(xs.shape) if not_significant_mask is not None: data_1d_masked = np.ma.masked_where(~not_significant_mask.mask, data_1d) the_zip = list(zip(i_indices, j_indices, data_1d_masked, not_significant_mask)) for i_index, j_index, the_data, significance in the_zip: to_plot[i_index, j_index] = the_data sign_mask_2d[i_index, j_index] = significance #plot not significant mask basemap.pcolormesh(xs, ys, sign_mask_2d.copy(), cmap = mpl.cm.get_cmap('gist_gray', 3), shading = 'flat', vmin = -1, vmax = 1, ax = ax ) else: the_zip = list(zip(i_indices, j_indices, data_1d)) for i_index, j_index, the_data in the_zip: to_plot[i_index, j_index] = the_data image = basemap.pcolormesh(xs, ys, to_plot.copy(), cmap = color_map, vmin = minmax[0], vmax = minmax[1], shading = 'flat', rasterized = False, ax = ax ) plot_basin_boundaries_from_shape(basemap, plotter = plt, linewidth = 1, edge_color = 'k', ax = ax) basemap.drawcoastlines(linewidth = 0.5, ax = ax) #plot_utils.draw_meridians_and_parallels(basemap, step_degrees = 30) if ax is None: ax = plt.gca() x_min, x_max, y_min, y_max = plot_utils.get_ranges(xs[i_indices, j_indices], ys[i_indices, j_indices]) ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) #draw a label #xmin, xmax = plt.xlim() #ymin, ymax = plt.ylim() # dx = xmax - xmin #dy = ymax - ymin #plt.annotate(label, xy = (xmax - 0.1 * dx, ymax - 0.1 * dy), font_properties = FontProperties(size = 25)) if title == "": ax.set_title(label) else: ax.set_title(title) #plot colorbar if show_colorbar: from mpl_toolkits.axes_grid1 import make_axes_locatable divider = make_axes_locatable(ax) cax = divider.append_axes("right", "8%", pad="3%") cb = plt.colorbar(image, #ticks = colorbar_tick_locator, orientation = colorbar_orientation, format = colorbar_label_format, drawedges = True, cax=cax ) cb.set_ticks(colorbar_tick_locator) if impose_lower_limit is None: min_val = min( data_1d.min(), 0) else: min_val = impose_lower_limit lower_limit = (min_val - minmax[0]) / float(minmax[1] - minmax[0]) if lower_limit < 0: lower_limit = 0.0 else: lower_limit = plot_utils.get_closest_tick_value(color_map.N + 1, lower_limit) cb.ax.set_ylim(bottom = lower_limit - 1.0e-4) cb.outline.set_visible(False) cb.ax.set_title(units) if upper_limited: cl = cb.ax.get_yticklabels() labels = [] for text in cl: labels.append(text.get_text()) labels[-1] = '$\\geq$' + labels[-1] cb.ax.set_yticklabels(labels) #save to a file if necessary if fig is not None: fig.savefig(imagefile, bbox_inches = "tight")