def pcolormesh(workspaces, fig=None): """ Create a figure containing pcolor subplots :param workspaces: A list of workspace handles :param fig: An optional figure to contain the new plots. Its current contents will be cleared :returns: The figure containing the plots """ # check inputs _validate_pcolormesh_inputs(workspaces) workspaces = [ws for ws in workspaces if isinstance(ws, MatrixWorkspace)] # create a subplot of the appropriate number of dimensions # extend in number of columns if the number of plottables is not a square number workspaces_len = len(workspaces) fig, axes, nrows, ncols = create_subplots(workspaces_len, fig=fig) row_idx, col_idx = 0, 0 for subplot_idx in range(nrows * ncols): ax = axes[row_idx][col_idx] if subplot_idx < workspaces_len: ws = workspaces[subplot_idx] pcm = pcolormesh_on_axis(ax, ws) if pcm: # Colour bar limits are wrong if workspace is ragged. Set them manually. colorbar_min = np.nanmin(pcm.get_array()) colorbar_max = np.nanmax(pcm.get_array()) pcm.set_clim(colorbar_min, colorbar_max) if col_idx < ncols - 1: col_idx += 1 else: row_idx += 1 col_idx = 0 else: # nothing here ax.axis('off') # Adjust locations to ensure the plots don't overlap fig.subplots_adjust(wspace=SUBPLOT_WSPACE, hspace=SUBPLOT_HSPACE) fig.colorbar(pcm, ax=axes.ravel().tolist(), pad=0.06) fig.canvas.set_window_title(figure_title(workspaces, fig.number)) #assert a minimum size, otherwise we can lose axis labels size = fig.get_size_inches() if (size[0] <= COLORPLOT_MIN_WIDTH) or (size[1] <= COLORPLOT_MIN_HEIGHT): fig.set_size_inches(COLORPLOT_MIN_WIDTH, COLORPLOT_MIN_HEIGHT, forward=True) fig.canvas.draw() fig.show() return fig
def test_figure_title_with_workspace_list(self): self.assertEqual( "fake-10", figure_title((FakeWorkspace("fake"), FakeWorkspace("nextfake")), 10))
def test_figure_title_with_single_workspace(self): self.assertEqual("fake-5", figure_title(FakeWorkspace("fake"), 5))
def test_figure_title_with_list_of_strings(self): self.assertEqual("first-10", figure_title(["first", "second"], 10))
def test_figure_title_with_single_string(self): self.assertEqual("test-1", figure_title("test", 1))
def test_figure_title_with_empty_list_raises_assertion(self): with self.assertRaises(AssertionError): figure_title([], 5)
def pcolormesh(workspaces, fig=None, color_norm=None, normalize_by_bin_width=None): """ Create a figure containing pcolor subplots :param workspaces: A list of workspace handles :param fig: An optional figure to contain the new plots. Its current contents will be cleared :param normalize_by_bin_width: Optional and only to be used in the event that the function is being called as part of a plot restore :returns: The figure containing the plots """ # check inputs _validate_pcolormesh_inputs(workspaces) workspaces = [ws for ws in workspaces if isinstance(ws, MatrixWorkspace)] # create a subplot of the appropriate number of dimensions # extend in number of columns if the number of plottables is not a square number workspaces_len = len(workspaces) fig, axes, nrows, ncols = create_subplots(workspaces_len, fig=fig) plots = [] row_idx, col_idx = 0, 0 for subplot_idx in range(nrows * ncols): ax = axes[row_idx][col_idx] if subplot_idx < workspaces_len: ws = workspaces[subplot_idx] pcm = pcolormesh_on_axis(ax, ws, color_norm, normalize_by_bin_width) plots.append(pcm) if col_idx < ncols - 1: col_idx += 1 else: row_idx += 1 col_idx = 0 if ConfigService.getString("plots.ShowMinorTicks").lower() == "on": ax.minorticks_on() ax.show_minor_gridlines = ConfigService.getString( "plots.ShowMinorGridlines").lower() == "on" else: # nothing here ax.axis('off') # If there are multiple plots limits are the min and max of all the plots colorbar_min = min(pt.norm.vmin for pt in plots) colorbar_max = max(pt.norm.vmax for pt in plots) for pt in plots: pt.set_clim(colorbar_min, colorbar_max) # Adjust locations to ensure the plots don't overlap fig.subplots_adjust(wspace=SUBPLOT_WSPACE, hspace=SUBPLOT_HSPACE) axes = axes.ravel() colorbar = fig.colorbar(pcm, ax=axes.tolist(), pad=0.06) add_colorbar_label(colorbar, axes) if fig.canvas.manager is not None: fig.canvas.manager.set_window_title( figure_title(workspaces, fig.number)) # assert a minimum size, otherwise we can lose axis labels size = fig.get_size_inches() if (size[0] <= COLORPLOT_MIN_WIDTH) or (size[1] <= COLORPLOT_MIN_HEIGHT): fig.set_size_inches(COLORPLOT_MIN_WIDTH, COLORPLOT_MIN_HEIGHT, forward=True) fig.canvas.draw() fig.show() return fig