def test_query_grid_on_button_press(): rmg = RasterModelGrid((5, 5)) imshow_grid(rmg, rmg.nodes, cmap="RdYlBu") # Programmatically create an event near the grid center. event = Event("simulated_event", gcf().canvas) event.xdata = int(rmg.number_of_node_columns * 0.5) event.ydata = int(rmg.number_of_node_rows * 0.5) results = query_grid_on_button_press(event, rmg) x_coord = results["grid location"]["x_coord"] y_coord = results["grid location"]["x_coord"] msg = "Items: Simulated matplotlib event and query results." assert_equal(x_coord, event.xdata, msg) assert_equal(y_coord, event.ydata, msg) msg = "Items: Node ID and grid coordinates of simulated matplotlib event." node = rmg.grid_coords_to_node_id(event.xdata, event.ydata) assert_equal(results["node"]["ID"], node, msg)
def test_query_grid_on_button_press(): rmg = RasterModelGrid((5, 5)) imshow_grid(rmg, rmg.nodes, cmap='RdYlBu') # Programmatically create an event near the grid center. event = Event('simulated_event', gcf().canvas) event.xdata = int(rmg.number_of_node_columns * 0.5) event.ydata = int(rmg.number_of_node_rows * 0.5) results = query_grid_on_button_press(event, rmg) x_coord = results['grid location']['x_coord'] y_coord = results['grid location']['x_coord'] msg = 'Items: Simulated matplotlib event and query results.' assert_equal(x_coord, event.xdata, msg) assert_equal(y_coord, event.ydata, msg) msg = 'Items: Node ID and grid coordinates of simulated matplotlib event.' node = rmg.grid_coords_to_node_id(event.xdata, event.ydata) assert_equal(results['node']['ID'], node, msg)
def test_query_grid_on_button_press(): rmg = RasterModelGrid((5, 5)) imshow_grid(rmg, rmg.nodes, cmap='RdYlBu') # Programmatically create an event near the grid center. event = Event('simulated_event', gcf().canvas) event.xdata = int(rmg.number_of_node_columns * 0.5) event.ydata = int(rmg.number_of_node_rows * 0.5) results = query_grid_on_button_press(event, rmg) x_coord = results['grid location']['x_coord'] y_coord = results['grid location']['x_coord'] msg = 'Items: Simulated matplotlib event and query results.' assert_equal(x_coord, event.xdata, msg) assert_equal(y_coord, event.ydata, msg) msg = 'Items: Node ID and grid coordinates of simulated matplotlib event.' node = rmg.grid_coords_to_node_id(event.xdata, event.ydata) assert_equal(results['node']['ID'], node, msg)
def imshow_grid_at_node(grid, values, **kwds): """imshow_grid_at_node(grid, values, plot_name=None, var_name=None, var_units=None, grid_units=None, symmetric_cbar=False, cmap='pink', limits=(values.min(), values.max()), vmin=values.min(), vmax=values.max(), allow_colorbar=True, norm=[linear], shrink=1., color_for_closed='black', color_for_background=None, show_elements=False, output=None) Prepare a map view of data over all nodes in the grid. Data is plotted as cells shaded with the value at the node at its center. Outer edges of perimeter cells are extrapolated. Closed elements are colored uniformly (default black, overridden with kwd 'color_for_closed'); other open boundary nodes get their actual values. *values* can be a field name, a regular array, or a masked array. If a masked array is provided, masked entries will be treated as if they were Landlab CLOSED_BOUNDARYs. Used together with the color_at_closed=None keyword (i.e., "transparent"), this can allow for construction of overlay layers in a figure (e.g., only defining values in a river network, and overlaying it on another landscape). Use matplotlib functions like xlim, ylim to modify your plot after calling :func:`imshow_grid`, as desired. Node coordinates are printed when a mouse button is pressed on a cell in the plot. This function happily works with both regular and irregular grids. Parameters ---------- grid : ModelGrid Grid containing the field to plot, or describing the geometry of the provided array. values : array_like, masked_array, or str Node values, or a field name as a string from which to draw the data. plot_name : str, optional String to put as the plot title. var_name : str, optional Variable name, to use as a colorbar label. var_units : str, optional Units for the variable being plotted, for the colorbar. grid_units : tuple of str, optional Units for y, and x dimensions. If None, component will look to the gri property `axis_units` for this information. If no units are specified there, no entry is made. symmetric_cbar : bool Make the colormap symetric about 0. cmap : str Name of a colormap limits : tuple of float Minimum and maximum of the colorbar. vmin, vmax: floats Alternatives to limits. allow_colorbar : bool If True, include the colorbar. colorbar_label : str or None The string with which to label the colorbar. norm : matplotlib.colors.Normalize The normalizing object which scales data, typically into the interval [0, 1]. Ignore in most cases. shrink : float Fraction by which to shrink the colorbar. color_for_closed : str or None Color to use for closed nodes (default 'black'). If None, closed (or masked) nodes will be transparent. color_for_background : color str or other color declaration, or None Color to use for closed elements (default None). If None, the background will be transparent, and appear white. show_elements : bool If True, and grid is a Voronoi, the faces will be plotted in black along with just the colour of the cell, defining the cell outlines (defaults False). output : None, string, or bool If None (or False), the image is sent to the imaging buffer to await an explicit call to show() or savefig() from outside this function. If a string, the string should be the path to a save location, and the filename (with file extension). The function will then call plt.savefig([string]) itself. If True, the function will call plt.show() itself once plotting is complete. """ if isinstance(values, str): values_at_node = grid.at_node[values] else: values_at_node = values.reshape((-1,)) if values_at_node.size != grid.number_of_nodes: raise ValueError("number of values does not match number of nodes") values_at_node = np.ma.masked_where( grid.status_at_node == CLOSED_BOUNDARY, values_at_node ) try: shape = grid.shape except AttributeError: shape = (-1,) _imshow_grid_values(grid, values_at_node.reshape(shape), **kwds) if isinstance(values, str): plt.title(values) plt.gcf().canvas.mpl_connect( "button_press_event", lambda event: query_grid_on_button_press(event, grid) )
def imshow_grid_at_node(grid, values, **kwds): """imshow_grid_at_node(grid, values, plot_name=None, var_name=None, var_units=None, grid_units=None, symmetric_cbar=False, cmap='pink', limits=(values.min(), values.max()), vmin=values.min(), vmax=values.max(), allow_colorbar=True, norm=[linear], shrink=1., color_for_closed='black', color_for_background=None, show_elements=False, output=None) Prepare a map view of data over all nodes in the grid. Data is plotted as cells shaded with the value at the node at its center. Outer edges of perimeter cells are extrapolated. Closed elements are colored uniformly (default black, overridden with kwd 'color_for_closed'); other open boundary nodes get their actual values. *values* can be a field name, a regular array, or a masked array. If a masked array is provided, masked entries will be treated as if they were Landlab CLOSED_BOUNDARYs. Used together with the color_at_closed=None keyword (i.e., "transparent"), this can allow for construction of overlay layers in a figure (e.g., only defining values in a river network, and overlaying it on another landscape). Use matplotlib functions like xlim, ylim to modify your plot after calling :func:`imshow_grid`, as desired. Node coordinates are printed when a mouse button is pressed on a cell in the plot. This function happily works with both regular and irregular grids. Parameters ---------- grid : ModelGrid Grid containing the field to plot, or describing the geometry of the provided array. values : array_like, masked_array, or str Node values, or a field name as a string from which to draw the data. plot_name : str, optional String to put as the plot title. var_name : str, optional Variable name, to use as a colorbar label. var_units : str, optional Units for the variable being plotted, for the colorbar. grid_units : tuple of str, optional Units for y, and x dimensions. If None, component will look to the gri property `axis_units` for this information. If no units are specified there, no entry is made. symmetric_cbar : bool Make the colormap symetric about 0. cmap : str Name of a colormap limits : tuple of float Minimum and maximum of the colorbar. vmin, vmax: floats Alternatives to limits. allow_colorbar : bool If True, include the colorbar. colorbar_label : str or None The string with which to label the colorbar. norm : matplotlib.colors.Normalize The normalizing object which scales data, typically into the interval [0, 1]. Ignore in most cases. shrink : float Fraction by which to shrink the colorbar. color_for_closed : str or None Color to use for closed nodes (default 'black'). If None, closed (or masked) nodes will be transparent. color_for_background : color str or other color declaration, or None Color to use for closed elements (default None). If None, the background will be transparent, and appear white. show_elements : bool If True, and grid is a Voronoi, the faces will be plotted in black along with just the colour of the cell, defining the cell outlines (defaults False). output : None, string, or bool If None (or False), the image is sent to the imaging buffer to await an explicit call to show() or savefig() from outside this function. If a string, the string should be the path to a save location, and the filename (with file extension). The function will then call plt.savefig([string]) itself. If True, the function will call plt.show() itself once plotting is complete. """ if isinstance(values, str): values_at_node = grid.at_node[values] else: values_at_node = values.reshape((-1, )) if values_at_node.size != grid.number_of_nodes: raise ValueError('number of values does not match number of nodes') values_at_node = np.ma.masked_where( grid.status_at_node == CLOSED_BOUNDARY, values_at_node) try: shape = grid.shape except AttributeError: shape = (-1, ) _imshow_grid_values(grid, values_at_node.reshape(shape), **kwds) if isinstance(values, str): plt.title(values) plt.gcf().canvas.mpl_connect('button_press_event', lambda event: query_grid_on_button_press(event, grid))