From 003be7cc1c4fbaa7d24df61ab85b1273cfe8f663 Mon Sep 17 00:00:00 2001 From: Daksh Pokar Date: Tue, 11 Feb 2025 10:18:13 -0600 Subject: [PATCH] fix: stacked bar plot with new api (#132) --- .../matplotlib/example_mpl_stacked.html | 547 +----------------- .../stacked/matplotlib/example_mpl_stacked.py | 60 +- maidr/core/plot/maidr_plot_factory.py | 6 +- maidr/patch/barplot.py | 9 +- 4 files changed, 33 insertions(+), 589 deletions(-) diff --git a/example/stacked/matplotlib/example_mpl_stacked.html b/example/stacked/matplotlib/example_mpl_stacked.html index a88f5b5..a7f48e0 100644 --- a/example/stacked/matplotlib/example_mpl_stacked.html +++ b/example/stacked/matplotlib/example_mpl_stacked.html @@ -1,548 +1,9 @@ + - - MAIDR - - + -
- - - - - - 2024-03-07T15:32:33.713265 - image/svg+xml - - - Matplotlib v3.7.5, https://matplotlib.org/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- + - + \ No newline at end of file diff --git a/example/stacked/matplotlib/example_mpl_stacked.py b/example/stacked/matplotlib/example_mpl_stacked.py index 62cdaae..e31b7da 100644 --- a/example/stacked/matplotlib/example_mpl_stacked.py +++ b/example/stacked/matplotlib/example_mpl_stacked.py @@ -1,49 +1,27 @@ -import os - -import maidr import matplotlib.pyplot as plt import numpy as np +import maidr -def get_filepath(filename: str) -> str: - current_file_path = os.path.abspath(__file__) - directory = os.path.dirname(current_file_path) - return os.path.join(directory, filename) - - -def plot(): - species = ( - "Adelie\n $\\mu=$3700.66g", - "Chinstrap\n $\\mu=$3733.09g", - "Gentoo\n $\\mu=5076.02g$", - ) - weight_counts = { - "Below": np.array([70, 31, 58]), - "Above": np.array([82, 37, 66]), - } - width = 0.5 - - fig, ax = plt.subplots() - bottom = np.zeros(3) - - for boolean, weight_count in weight_counts.items(): - ax.bar(species, weight_count, width, label=boolean, bottom=bottom) - bottom += weight_count - - ax.set_title("Number of penguins with above average body mass") - ax.legend(loc="upper right") - ax.set_xlabel("Species") - ax.set_ylabel("Number of Penguins") - - return ax +species = ( + "Adelie\n $\\mu=$3700.66g", + "Chinstrap\n $\\mu=$3733.09g", + "Gentoo\n $\\mu=5076.02g$", +) +weight_counts = { + "Below": np.array([70, 31, 58]), + "Above": np.array([82, 37, 66]), +} +width = 0.5 +fig, ax = plt.subplots() +bottom = np.zeros(3) -def main(): - stacked = plot() - stacked_maidr = maidr.stacked(stacked) - stacked_maidr.save_html(get_filepath("example_mpl_stacked.html")) - stacked_maidr.show() +for boolean, weight_count in weight_counts.items(): + p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom) + bottom += weight_count +ax.set_title("Number of penguins with above average body mass") +ax.legend(loc="upper right") -if __name__ == "__main__": - main() +maidr.show(p) diff --git a/maidr/core/plot/maidr_plot_factory.py b/maidr/core/plot/maidr_plot_factory.py index 6f8a02e..8f2df6d 100644 --- a/maidr/core/plot/maidr_plot_factory.py +++ b/maidr/core/plot/maidr_plot_factory.py @@ -3,14 +3,14 @@ from matplotlib.axes import Axes from maidr.core.enum import PlotType -from maidr.core.plot.maidr_plot import MaidrPlot from maidr.core.plot.barplot import BarPlot from maidr.core.plot.boxplot import BoxPlot +from maidr.core.plot.grouped_barplot import GroupedBarPlot from maidr.core.plot.heatmap import HeatPlot from maidr.core.plot.histogram import HistPlot from maidr.core.plot.lineplot import LinePlot +from maidr.core.plot.maidr_plot import MaidrPlot from maidr.core.plot.scatterplot import ScatterPlot -from maidr.core.plot.grouped_barplot import GroupedBarPlot class MaidrPlotFactory: @@ -43,6 +43,6 @@ def create(ax: Axes, plot_type: PlotType, **kwargs) -> MaidrPlot: elif PlotType.SCATTER == plot_type: return ScatterPlot(ax) elif PlotType.DODGED == plot_type or PlotType.STACKED == plot_type: - return GroupedBarPlot(ax, plot_type) + return GroupedBarPlot(ax, plot_type, **kwargs) else: raise TypeError(f"Unsupported plot type: {plot_type}.") diff --git a/maidr/patch/barplot.py b/maidr/patch/barplot.py index 63a15dc..b006baf 100644 --- a/maidr/patch/barplot.py +++ b/maidr/patch/barplot.py @@ -1,7 +1,6 @@ from __future__ import annotations import wrapt - from matplotlib.axes import Axes from matplotlib.container import BarContainer @@ -10,7 +9,13 @@ def bar(wrapped, instance, args, kwargs) -> Axes | BarContainer: - return common(PlotType.BAR, wrapped, instance, args, kwargs) + plot_type = PlotType.BAR + if "bottom" in kwargs: + bottom = kwargs.get("bottom") + if bottom is not None: + plot_type = PlotType.STACKED + + return common(plot_type, wrapped, instance, args, kwargs) # Patch matplotlib functions.