marmot.plottingmodules.plotutils.plot_library.PlotLibrary#
- class PlotLibrary(nrows: int = 1, ncols: int = 1, figsize: Tuple[int, int] = (6, 4), sharey: bool = False, squeeze: bool = True, ravel_axs: bool = False, **kwargs)[source]#
Bases:
marmot.plottingmodules.plotutils.plot_library.SetupSubplot
A library of commonly used plotting methods.
Inherits the SetupSubplot class and takes all the same arguments as it.
Defines the dimensions (nrows, ncols) of a figure and its subplots. Builds on top of matplotlib.pyplot.subplots and preserves all functionality of that function.
All arguments are optional and by default calling SetupSubplot() or PlotLibrary() which inherits SetupSubplot, will create a 1x1 figure with a figsize defined in the config.yml file. The following are some common values to pass when creating various plots.
1x1 figure: SetupSubplot()
1xN figure: SetupSubplot(nrows=1, ncols=N, sharey=True)
MxN figure: SetupSubplot(nrows=M, ncols=N, sharey=True, squeeze=False, ravel_axs=True)
Plotting defaults are also set in this class, which are defined in the config.yml file.
- Parameters
nrows (int, optional) – Number of rows of the subplot grid. Defaults to 1.
ncols (int, optional) – Number of columns of the subplot grid. Defaults to 1.
figsize (Tuple[int, int], optional) – The x,y dimension of each subplot. Defaults set in config.yml
sharey (bool, optional) – share the y-axis across all subplots. Defaults to False.
squeeze (bool, optional) – If True, extra dimensions are squeezed out from the returned axs objects if 1x1 figure or 1xN figure. Defaults to True.
ravel_axs (bool, optional) – If True the returned axs object is a 1D numpy object array of Axes objects. This can be used to convert MxN figure axs objects to 1D. Defaults to False.
**kwargs – These parameters will be passed to matplotlib.pyplot.subplots function.
Methods
barplot
(df[, color, stacked, sub_pos, ...])Creates a bar plot.
clustered_stacked_barplot
(df_list, labels, ...)Creates a clustered stacked barplot.
histogram
(df, color_dict[, label, sub_pos])Creates a histogram plot
lineplot
(data[, column, color, linestyle, ...])Creates a line plot.
stackplot
(df[, color_dict, sub_pos, ...])Creates a stacked area plot.
- stackplot(df: pandas.core.frame.DataFrame, color_dict: Optional[dict] = None, sub_pos: Union[int, Tuple[int, int]] = 0, ytick_major_fmt: str = 'standard', **kwargs)[source]#
Creates a stacked area plot.
Wrapper around matplotlib.stackplot.
- Parameters
df (pd.DataFrame) – DataFrame of data to plot.
color_dict (dict) – Colour dictionary, keys should be in data columns. Defaults to None.
ytick_major_fmt (str, optional) – Sets the ytick major format. Value gets passed to the set_yaxis_major_tick_format method Defaults to ‘standard’
sub_pos (Union[int, Tuple[int, int]], optional) – Position of subplot, can be either a integer or a tuple of 2 integers depending on how SetupSubplot was instantiated. Defaults to 0
**kwargs – These parameters will be passed to matplotlib.axes.Axes.stackplot function.
- barplot(df: pandas.core.frame.DataFrame, color: Optional[Union[dict, list]] = None, stacked: bool = False, sub_pos: Union[int, Tuple[int, int]] = 0, custom_tick_labels: Optional[list] = None, ytick_major_fmt: str = 'standard', legend=False, edgecolor='black', linewidth='0.1', **kwargs)[source]#
Creates a bar plot.
Wrapper around pandas.plot.bar
- Parameters
df (pd.DataFrame) – DataFrame of data to plot.
color (dict) – dictionary of colors, dict keys should be found in df columns.
stacked (bool, optional) – Whether to stack bar values. Defaults to False.
sub_pos (Union[int, Tuple[int, int]], optional) – Position of subplot, can be either a integer or a tuple of 2 integers depending on how SetupSubplot was instantiated. Defaults to 0
custom_tick_labels (list, optional) – List of custom tick labels to use. Defaults to None
ytick_major_fmt (str, optional) – Sets the ytick major format. Value gets passed to the set_yaxis_major_tick_format method Defaults to ‘standard’
**kwargs – These parameters will be passed to matplotlib.axes.Axes.plot function.
- lineplot(data: pandas.core.series.Series, column=None, color: Optional[Union[dict, str]] = None, linestyle: str = 'solid', sub_pos: Union[int, Tuple[int, int]] = 0, alpha: int = 1, ytick_major_fmt: str = 'standard', **kwargs)[source]#
Creates a line plot.
Wrapper around matplotlib.plot
- Parameters
data (pd.Series, pd.DataFrame) – Series/ df of data to plot.
column (str) – If passing df as data column is required.
color (dict, str, optional) – Color dict or str, if dict color is chosen based on df column Defaults to None.
linestyle (str, optional) – Style of line to plot. Defaults to ‘solid’.
sub_pos (Union[int, Tuple[int, int]], optional) – Position of subplot, can be either a integer or a tuple of 2 integers depending on how SetupSubplot was instantiated. Defaults to 0
alpha (int, optional) – Line opacity. Defaults to 1.
ytick_major_fmt (str, optional) – Sets the ytick major format. Value gets passed to the set_yaxis_major_tick_format method Defaults to ‘standard’
**kwargs – These parameters will be passed to matplotlib.axes.Axes.plot function.
- histogram(df: pandas.core.frame.DataFrame, color_dict: dict, label=None, sub_pos: Union[int, Tuple[int, int]] = 0, **kwargs)[source]#
Creates a histogram plot
Wrapper around matplotlib.hist
- Parameters
df (pd.DataFrame) – DataFrame of data to plot.
color_dict (dict) – Colour dictionary
label (list, optional) – List of labels for legend. Defaults to None.
sub_pos (Union[int, Tuple[int, int]], optional) – Position of subplot, can be either a integer or a tuple of 2 integers depending on how SetupSubplot was instantiated. Defaults to 0
**kwargs – These parameters will be passed to matplotlib.axes.Axes.hist function.
- clustered_stacked_barplot(df_list: List[pandas.core.frame.DataFrame], labels: list, color_dict: dict, title: str = '', H: str = '//', sub_pos: Union[int, Tuple[int, int]] = 0, ytick_major_fmt='standard', **kwargs)[source]#
Creates a clustered stacked barplot.
- Parameters
df_list (List[pd.DataFrame, pd.DataFrame]) – List of Pandas DataFrames The columns within each dataframe will be stacked with different colors. The corresponding columns between each dataframe will be set next to each other and given different hatches.
labels (list) – A list of strings, usually the scenario names
color_dict (dict) – Color dictionary, keys should be the same as labels
title (str, optional) – Optional plot title. Defaults to “”.
H (str, optional) – Sets the hatch pattern to differentiate dataframe bars. Defaults to “//”.
sub_pos (Union[int, Tuple[int, int]], optional) – Position of subplot, can be either a integer or a tuple of 2 integers depending on how SetupSubplot was instantiated. Defaults to 0
ytick_major_fmt (str, optional) – Sets the ytick major format. Value gets passed to the set_yaxis_major_tick_format method Defaults to ‘standard’
**kwargs – These parameters will be passed to matplotlib.axes.Axes.plot function.