def test_colorbar_height(basic_image): """Test that the colorbar ax height matches the image axis height.""" f, ax = plt.subplots(figsize=(5, 5)) im = ax.imshow(basic_image, cmap="RdYlGn") cb = ep.colorbar(im) assert cb.ax.get_position().height == im.axes.get_position().height plt.close(f)
def plot_continuous_raster(self, output_file, cmap, vmax=np.nan, vmin=np.nan, box=True): """Creates a figure of a continuous valued raster :param output_file: path, file path of the figure :param cmap: string, colormap to plot the raster :param vmax: float, optional, value maximum of the scale, this value is used in the normalization of the colormap :param vmin: float, optional, value minimum of the scale, this value is used in the normalization of the colormap :param box: boolean, if False it sets off the frame of the picture :returns: saves the figure of the raster """ raster_np = read_raster(self.path) fig1, ax1 = plt.subplots(figsize=(6, 8), frameon=False) # norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N) if np.isfinite(vmax) and np.isfinite(vmin): im1 = ax1.imshow(raster_np, cmap=cmap, vmax=vmax, vmin=vmin) else: im1 = ax1.imshow(raster_np, cmap=cmap, vmax=raster_np.max(), vmin=raster_np.min()) fig1.tight_layout() plt.setp(ax1) cbar = ep.colorbar(im1, pad=0.3, size='5%') cbar.ax.tick_params(labelsize=15) if not box: ax1.axis('off') fig1.savefig(output_file, dpi=200, bbox_inches='tight')
def plot_algae_bloom(cha_sentinel_class,location): cha_cat_names = ["Pure", "Light Concentration", "Low Concentration", "Moderate Concentration", "High Concentration"] nbr_colors = ["g", "yellowgreen", "peachpuff", "coral", "maroon"] nbr_cmap = ListedColormap(nbr_colors) # Plot the data with a custom legend fig, ax = plt.subplots(figsize=(30, 20)) im = ax.imshow(cha_sentinel_class.reshape(cha_sentinel_class.shape[1:3]), cmap=nbr_cmap) ax.set_title("chlorophyll a", fontsize=16) cbar = ep.colorbar(im) cbar.set_ticks(np.unique(cha_sentinel_class)) cbar.set_ticklabels(cha_cat_names) # Turn off ticks ax.set_axis_off() #plt.show() path = 'static/img/algaeresult/algae_plot_' + location + '.png' plt.savefig(path)
def plot_colormap(dnbr_sentinel_class,location): dnbr_cat_names = ["Enhanced Regrowth", "Unburned", "Low Severity", "Moderate Severity", "High Severity"] nbr_colors = ["g", "yellowgreen", "peachpuff", "coral", "maroon"] nbr_cmap = ListedColormap(nbr_colors) # Plot the data with a custom legend fig, ax = plt.subplots(figsize=(10, 10)) im = ax.imshow(dnbr_sentinel_class.reshape(dnbr_sentinel_class.shape[:2]), cmap=nbr_cmap) ax.set_title("Sentinel dNBR", fontsize=16) cbar = ep.colorbar(im) cbar.set_ticks(np.unique(dnbr_sentinel_class)) cbar.set_ticklabels(dnbr_cat_names) # Turn off ticks ax.set_axis_off() path = 'static/img/fireresult/fire_plot_' + location + '.png' plt.savefig(path)
def plot_continuous_w_window(self, output_file, xy, width, height, bounds, cmap=None, list_colors=None): """ Create a figure of a raster with a zoomed window :param output_file: path, file path of the figure :param xy: tuple (x,y), origin of the zoomed window, the upper left corner :param width: integer, width (number of cells) of the zoomed window :param height: integer, height (number of cells) of the zoomed window :param bounds: list of float, limits for each color of the colormap :param cmap: string, optional, colormap to plot the raster :param list_colors: list of colors (str), optional, as alternative to using a colormap :returns: saves the figure of the raster """ # xy: upper left corner from the lower left corner of the picture raster_np = read_raster(self.path) print('Raster has size: ', raster_np.shape) fig, ax = plt.subplots(1, 2, figsize=(10, 8)) fig.tight_layout() # Creates a colormap based on the given list_colors, if the cmap is not given if cmap is None and list_colors is not None: cmap = matplotlib.colors.ListedColormap(list_colors) elif cmap is not None and list_colors is None: pass else: print('Error: Insuficient number of arguments') norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N) ax[0].imshow(raster_np, cmap=cmap, norm=norm) rectangle = patches.Rectangle(xy, width, height, fill=False) ax[0].add_patch(rectangle) plt.setp(ax, xticks=[], yticks=[]) # Plot Patch box_np = raster_np[xy[1]: xy[1] + height, xy[0]: xy[0] + width] im = ax[1].imshow(box_np, cmap=cmap, norm=norm) # ax[1].axis('off') cbar = ep.colorbar(im, pad=0.3, size='5%') cbar.ax.tick_params(labelsize=20) fig.savefig(output_file, dpi=600, bbox_inches='tight')
def test_colorbar_raises_value_error(): """Test that a non matbplotlib axis object raises an value error.""" with pytest.raises(AttributeError, match="requires a matplotlib"): ep.colorbar(list()) plt.close()
# - # This is using a helper function from earthpy to create the mask so we can plot it # You don't need to do this in your workflow as you can perform the mask in one step # But we have it here for demonstration purposes cl_mask = em._create_mask(landsat_qa, all_masked_values) np.unique(cl_mask) # Below is the plot of the reclassified raster mask created from the `_create_mask` helper function. # + {"caption": "Landsat image in which the masked pixels (cloud) are rendered in light purple.", "tags": ["hide"]} fig, ax = plt.subplots(figsize=(12, 8)) im = ax.imshow(cl_mask, cmap=plt.cm.get_cmap('tab20b', 2)) cbar = ep.colorbar(im) cbar.set_ticks((0.25, .75)) cbar.ax.set_yticklabels(["Clear Pixels", "Cloud / Shadow Pixels"]) ax.set_title("Landsat Cloud Mask | Light Purple Pixels will be Masked") ax.set_axis_off() plt.show() # - # ## What Does the Metadata Tell You? # # You just explored two layers that potentially have information about cloud cover. # However what do the values stored in those rasters mean? You can refer to the # metadata provided by USGS to learn more about how # each layer in your landsat dataset are both stored and calculated.