def get_plot_data( plot_axis: plt.Axes) -> Tuple[str, str, List[float], str, List[float]]: """ Extracts plot's title, x-axis name and range and y-axis name and range. Parameters ---------- plot_axis : matplotlib.pyplot.Axes A matplotlib axis from which all of the aforementioned information will be extracted. Returns ------- plot_title : string Plot's title. plot_x_label : string Label of the plot's x-axis. plot_x_range : List[Number] Range of the plot's x-axis. plot_y_label : string Label of the plot's y-axis. plot_y_range : List[Number] Range of the plot's y-axis. """ assert isinstance(plot_axis, plt.Axes), 'Must be a matplotlib axis.' plot_title = plot_axis.get_title() plot_x_label = plot_axis.xaxis.get_label_text() plot_x_range = plot_axis.xaxis.get_view_interval() plot_y_label = plot_axis.yaxis.get_label_text() plot_y_range = plot_axis.yaxis.get_view_interval() return plot_title, plot_x_label, plot_x_range, plot_y_label, plot_y_range
def get_bar_data( plot_axis: plt.Axes ) -> Tuple[str, List[str], List[float], List[str], List[float], List[float], List[Tuple[float, float, float, float]]]: """ Extracts plot's title, x-axis name and range and y-axis name and range. Parameters ---------- plot_axis : matplotlib.pyplot.Axes A matplotlib axis from which all of the aforementioned information will be extracted. Returns ------- plot_title : string Plot's title. plot_x_tick_names : List[string] Tick labels of the plot's x-axis. plot_x_range : List[Number] Range of the plot's x-axis. plot_y_tick_names : List[string] Tick labels of the plot's y-axis. plot_y_range : List[Number] Range of the plot's y-axis. plot_bar_width : List[Number] Bar width of every bar in the plot. plot_bar_colours : List[Tuple[float, float, float, float]] Bar colour of every bar in the plot. This is represented as an (r, g, b, alpha) tuple. """ assert isinstance(plot_axis, plt.Axes), 'Must be a matplotlib axis.' plot_title = plot_axis.get_title() plot_x_tick_names = [ x.get_text() for x in plot_axis.xaxis.get_ticklabels() ] plot_x_range = plot_axis.xaxis.get_view_interval() plot_y_tick_names = [ y.get_text() for y in plot_axis.yaxis.get_ticklabels() ] plot_y_range = plot_axis.yaxis.get_view_interval() plot_bar_width = [ybar.get_width() for ybar in plot_axis.patches] plot_bar_colours = [ybar.get_facecolor() for ybar in plot_axis.patches] return (plot_title, plot_x_tick_names, plot_x_range, plot_y_tick_names, plot_y_range, plot_bar_width, plot_bar_colours)
def set_axes_properties(ax: plt.Axes, **kwargs) -> None: """ Ease the configuration of a :class:`matplotlib.axes.Axes` object. Parameters ---------- ax : matplotlib.axes.Axes The axes. Keyword arguments ----------------- fontsize : int Font size to use for the plot titles, and axes ticks and labels. Defaults to 12. title_center : str The center title. Defaults to an empty string. title_left : str The left title. Defaults to an empty string. title_right : str The right title. Defaults to an empty string. x_label : str The x-axis label. Defaults to an empty string. x_labelcolor : str Color of the x-axis label. Defaults to 'black'. x_lim : Sequence[int] Data limits for the x-axis. Defaults to `None`, i.e., the data limits will be left unchanged. invert_xaxis : bool `True` to make to invert the x-axis, `False` otherwise. Defaults to `False`. x_scale : str The x-axis scale. Defaults to 'linear'. x_ticks : sequence[float] Sequence of x-axis ticks location. Defaults to `None`. x_ticklabels : sequence[str] Sequence of x-axis ticks labels. Defaults to `None`. x_ticklabels_color : str Color for the x-axis ticks labels. Defaults to 'black'. x_ticklabels_rotation : float Rotation angle of the x-axis ticks labels. Defaults to 0. xaxis_minor_ticks_visible : bool `True` to show all ticks, either labelled or unlabelled, `False` to show only the labelled ticks. Defaults to `False`. xaxis_visible : bool `False` to make the x-axis invisible. Defaults to `True`. y_label : str The y-axis label. Defaults to an empty string. y_labelcolor : str Color of the y-axis label. Defaults to 'black'. y_lim : Sequence[int] Data limits for the y-axis. Defaults to `None`, i.e., the data limits will be left unchanged. invert_yaxis : bool `True` to make to invert the y-axis, `False` otherwise. Defaults to `False`. y_scale : str The y-axis scale. Defaults to 'linear'. y_ticks : sequence[float] Sequence of y-axis ticks location. Defaults to `None`. y_ticklabels : sequence[str] Sequence of y-axis ticks labels. Defaults to `None`. y_ticklabels_color : str Color for the y-axis ticks labels. Defaults to 'black'. y_ticklabels_rotation : float Rotation angle of the y-axis ticks labels. Defaults to 0. yaxis_minor_ticks_visible : bool `True` to show all ticks, either labelled or unlabelled, `False` to show only the labelled ticks. Defaults to :obj:`False`. yaxis_visible : bool :obj:`False` to make the y-axis invisible. Defaults to :obj:`True`. z_label : str The z-axis label. Defaults to an empty string. z_labelcolor : str Color of the z-axis label. Defaults to 'black'. z_lim : Sequence[int] Data limits for the z-axis. Defaults to :obj:`None`, i.e., the data limits will be left unchanged. invert_zaxis : bool :obj:`True` to make to invert the z-axis, :obj:`False` otherwise. Defaults to :obj:`False`. z_scale : str The z-axis scale. Defaults to 'linear'. z_ticks : sequence[float] Sequence of z-axis ticks location. Defaults to :obj:`None`. z_ticklabels : sequence[str] Sequence of z-axis ticks labels. Defaults to :obj:`None`. z_ticklabels_color : str Rotation angle of the z-axis ticks labels. Defaults to 0. z_ticklabels_rotation : float Color for the z-axis ticks labels. Defaults to 'black'. zaxis_minor_ticks_visible : bool :obj:`True` to show all ticks, either labelled or unlabelled, :obj:`False` to show only the labelled ticks. Defaults to :obj:`False`. zaxis_visible : bool :obj:`False` to make the z-axis invisible. Defaults to :obj:`True`. legend_on : bool :obj:`True` to show the legend, :obj:`False` otherwise. Defaults to :obj:`False`. legend_loc : str String specifying the location where the legend should be placed. Defaults to 'best'; please see :func:`matplotlib.pyplot.legend` for all the available options. legend_bbox_to_anchor : Sequence[float] 4-items tuple defining the box used to place the legend. This is used in conjuction with `legend_loc` to allow arbitrary placement of the legend. legend_framealpha : float Legend transparency. It should be between 0 and 1; defaults to 0.5. legend_ncol : int Number of columns into which the legend labels should be arranged. Defaults to 1. text : str Text to be added to the figure as anchored text. Defaults to :obj:`None`, meaning that no text box is shown. text_loc : str String specifying the location where the text box should be placed. Defaults to 'upper right'; please see :class:`matplotlib.offsetbox.AnchoredText` for all the available options. grid_on : bool :obj:`True` to show the plot grid, :obj:`False` otherwise. Defaults to :obj:`False`. grid_properties : dict Keyword arguments specifying various settings of the plot grid. """ fontsize = kwargs.get("fontsize", 12) # title title_center = kwargs.get("title_center", "") title_left = kwargs.get("title_left", "") title_right = kwargs.get("title_right", "") # x-axis x_label = kwargs.get("x_label", "") x_labelcolor = kwargs.get("x_labelcolor", "black") x_lim = kwargs.get("x_lim", None) invert_xaxis = kwargs.get("invert_xaxis", False) x_scale = kwargs.get("x_scale", "linear") x_ticks = kwargs.get("x_ticks", None) x_ticklabels = kwargs.get("x_ticklabels", None) x_ticklabels_color = kwargs.get("x_ticklabels_color", "black") x_ticklabels_rotation = kwargs.get("x_ticklabels_rotation", 0) x_tickformat = kwargs.get("x_tickformat", None) xaxis_minor_ticks_visible = kwargs.get("xaxis_minor_ticks_visible", False) xaxis_visible = kwargs.get("xaxis_visible", True) # y-axis y_label = kwargs.get("y_label", "") y_labelcolor = kwargs.get("y_labelcolor", "black") y_lim = kwargs.get("y_lim", None) invert_yaxis = kwargs.get("invert_yaxis", False) y_scale = kwargs.get("y_scale", "linear") y_ticks = kwargs.get("y_ticks", None) y_ticklabels = kwargs.get("y_ticklabels", None) y_ticklabels_color = kwargs.get("y_ticklabels_color", "black") y_ticklabels_rotation = kwargs.get("y_ticklabels_rotation", 0) y_tickformat = kwargs.get("y_tickformat", None) yaxis_minor_ticks_visible = kwargs.get("yaxis_minor_ticks_visible", False) yaxis_visible = kwargs.get("yaxis_visible", True) # legend legend_on = kwargs.get("legend_on", False) legend_loc = kwargs.get("legend_loc", "best") legend_bbox_to_anchor = kwargs.get("legend_bbox_to_anchor", None) legend_framealpha = kwargs.get("legend_framealpha", 0.5) legend_ncol = kwargs.get("legend_ncol", 1) legend_fontsize = kwargs.get("legend_fontsize", fontsize) # textbox text = kwargs.get("text", None) text_loc = kwargs.get("text_loc", "") # grid grid_on = kwargs.get("grid_on", False) grid_properties = kwargs.get("grid_properties", None) rcParams["font.size"] = fontsize # rcParams['text.usetex'] = True # plot titles if ax.get_title(loc="center") == "": ax.set_title(title_center, loc="center", fontsize=rcParams["font.size"] - 1) if ax.get_title(loc="left") == "": ax.set_title(title_left, loc="left", fontsize=rcParams["font.size"] - 1) if ax.get_title(loc="right") == "": ax.set_title(title_right, loc="right", fontsize=rcParams["font.size"] - 1) # axes labels if ax.get_xlabel() == "": ax.set(xlabel=x_label) if ax.get_ylabel() == "": ax.set(ylabel=y_label) # axes labelcolors if ax.get_xlabel() != "" and x_labelcolor != "": ax.xaxis.label.set_color(x_labelcolor) if ax.get_ylabel() != "" and y_labelcolor != "": ax.yaxis.label.set_color(y_labelcolor) # axes limits if x_lim is not None: ax.set_xlim(x_lim) if y_lim is not None: ax.set_ylim(y_lim) # invert the axes if invert_xaxis: ax.invert_xaxis() if invert_yaxis: ax.invert_yaxis() # axes scale if x_scale is not None: ax.set_xscale(x_scale) if y_scale is not None: ax.set_yscale(y_scale) # axes ticks if x_ticks is not None: ax.get_xaxis().set_ticks(x_ticks) if y_ticks is not None: ax.get_yaxis().set_ticks(y_ticks) # axes tick labels if x_ticklabels is not None: ax.get_xaxis().set_ticklabels(x_ticklabels) if y_ticklabels is not None: ax.get_yaxis().set_ticklabels(y_ticklabels) # axes tick labels color if x_ticklabels_color != "": ax.tick_params(axis="x", colors=x_ticklabels_color) if y_ticklabels_color != "": ax.tick_params(axis="y", colors=y_ticklabels_color) # axes tick format if x_tickformat is not None: ax.xaxis.set_major_formatter(FormatStrFormatter(x_tickformat)) if y_tickformat is not None: ax.yaxis.set_major_formatter(FormatStrFormatter(y_tickformat)) # axes tick labels rotation plt.xticks(rotation=x_ticklabels_rotation) plt.yticks(rotation=y_ticklabels_rotation) # unlabelled axes ticks if not xaxis_minor_ticks_visible: ax.get_xaxis().set_tick_params(which="minor", size=0) ax.get_xaxis().set_tick_params(which="minor", width=0) if not yaxis_minor_ticks_visible: ax.get_yaxis().set_tick_params(which="minor", size=0) ax.get_yaxis().set_tick_params(which="minor", width=0) # axes visibility if not xaxis_visible: ax.get_xaxis().set_visible(False) if not yaxis_visible: ax.get_yaxis().set_visible(False) # legend if legend_on: if legend_bbox_to_anchor is None: ax.legend( loc=legend_loc, framealpha=legend_framealpha, ncol=legend_ncol, fontsize=legend_fontsize, ) else: ax.legend( loc=legend_loc, framealpha=legend_framealpha, ncol=legend_ncol, fontsize=legend_fontsize, bbox_to_anchor=legend_bbox_to_anchor, ) # text box if text is not None: ax.add_artist(AnchoredText(text, loc=text_locations[text_loc])) # plot grid if grid_on: gps = grid_properties if grid_properties is not None else {} ax.grid(True, **gps)