Skip to content

feat: support boxplot using maidr-ts #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion example/box/matplotlib/example_mpl_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import maidr

maidr.set_engine("ts")

# Generating random data for three different groups
data_group1 = np.random.normal(100, 10, 200)
data_group2 = np.random.normal(90, 20, 200)
Expand Down Expand Up @@ -40,7 +42,7 @@ def boxplot(is_vert: bool):
# plt.show()
maidr.show(vert)

# Create the vertical boxplot
# Create the horizontal boxplot
horz = boxplot(is_vert=False)
# plt.show()
maidr.show(horz)
2 changes: 2 additions & 0 deletions example/box/seaborn/example_sns_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import maidr

maidr.set_engine("ts")

# Load the Iris dataset
iris = sns.load_dataset("iris")

Expand Down
4 changes: 2 additions & 2 deletions maidr/core/enum/maidr_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class MaidrKey(str, Enum):
TITLE = "title"

# Box plot keys.
LOWER_OUTLIER = "lower_outlier"
LOWER_OUTLIER = "lowerOutliers"
MIN = "min"
MAX = "max"
Q1 = "q1"
Q2 = "q2"
Q3 = "q3"
UPPER_OUTLIER = "upper_outlier"
UPPER_OUTLIER = "upperOutliers"

# Grouped bar and heatmap plot keys.
FILL = "fill"
Expand Down
34 changes: 14 additions & 20 deletions maidr/core/plot/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,6 @@ def render(self) -> dict:
box_orientation = {MaidrKey.ORIENTATION: self._orientation}
return DictMergerMixin.merge_dict(base_schema, box_orientation)

def _extract_axes_data(self) -> dict:
base_ax_schema = super()._extract_axes_data()
if self._orientation == "vert":
box_ax_schema = {
MaidrKey.X: {
MaidrKey.LEVEL: self.extract_level(self.ax, MaidrKey.X),
},
}
else:
box_ax_schema = {
MaidrKey.Y: {
MaidrKey.LEVEL: self.extract_level(self.ax, MaidrKey.Y),
}
}
return self.merge_dict(base_ax_schema, box_ax_schema)

def _extract_plot_data(self) -> list:
data = self._extract_bxp_maidr(self._bxp_stats)

Expand All @@ -171,17 +155,27 @@ def _extract_bxp_maidr(self, bxp_stats: dict) -> list[dict] | None:
caps = self._bxp_extractor.extract_caps(bxp_stats["caps"])
medians = self._bxp_extractor.extract_medians(bxp_stats["medians"])
outliers = self._bxp_extractor.extract_outliers(bxp_stats["fliers"], caps)

for whisker, cap, median, outlier in zip(whiskers, caps, medians, outliers):
levels = (
self.extract_level(self.ax, MaidrKey.X)
if self._orientation == "vert"
else self.extract_level(self.ax, MaidrKey.Y)
)
if levels is None:
levels = []

for whisker, cap, median, outlier, level in zip(
whiskers, caps, medians, outliers, levels
):
bxp_maidr.append(
{
MaidrKey.LOWER_OUTLIER.value: outlier["lower_outlier"],
MaidrKey.LOWER_OUTLIER.value: outlier[MaidrKey.LOWER_OUTLIER.value],
MaidrKey.MIN.value: cap["min"],
MaidrKey.Q1.value: whisker["q1"],
MaidrKey.Q2.value: median,
MaidrKey.Q3.value: whisker["q3"],
MaidrKey.MAX.value: cap["max"],
MaidrKey.UPPER_OUTLIER.value: outlier["upper_outlier"],
MaidrKey.UPPER_OUTLIER.value: outlier[MaidrKey.UPPER_OUTLIER.value],
MaidrKey.FILL.value: level,
}
)

Expand Down
48 changes: 25 additions & 23 deletions tests/core/plot/test_boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,49 @@

@pytest.mark.parametrize("lib", [Library.MATPLOTLIB, Library.SEABORN])
def test_box_plot_data(plot_fixture, lib):
x_level = ["1", "2", "3"] if lib == Library.MATPLOTLIB else ["0", "1", "2"]
# x_level = ["1", "2", "3"] if lib == Library.MATPLOTLIB else ["0", "1", "2"]
expected_maidr_data = {
MaidrKey.TYPE: PlotType.BOX,
MaidrKey.ORIENTATION: "vert",
MaidrKey.TITLE: f"Test {lib.value} box title",
MaidrKey.AXES: {
MaidrKey.X: {
MaidrKey.LABEL: f"Test {lib.value} box x label",
MaidrKey.LEVEL: x_level,
},
MaidrKey.Y: {
MaidrKey.LABEL: f"Test {lib.value} box y label",
},
},
MaidrKey.DATA: [
{
MaidrKey.LOWER_OUTLIER: [],
MaidrKey.MIN: 1.0,
MaidrKey.Q1: 1.5,
MaidrKey.Q2: 2.0,
MaidrKey.Q3: 2.5,
MaidrKey.MAX: 3.0,
MaidrKey.UPPER_OUTLIER: [],
MaidrKey.FILL.value: "0",
MaidrKey.LOWER_OUTLIER.value: [],
MaidrKey.MIN.value: 1.0,
MaidrKey.Q1.value: 1.5,
MaidrKey.Q2.value: 2.0,
MaidrKey.Q3.value: 2.5,
MaidrKey.MAX.value: 3.0,
MaidrKey.UPPER_OUTLIER.value: [],
},
{
MaidrKey.LOWER_OUTLIER: [],
MaidrKey.MIN: 4.0,
MaidrKey.Q1: 4.5,
MaidrKey.Q2: 5.0,
MaidrKey.Q3: 5.5,
MaidrKey.MAX: 6.0,
MaidrKey.UPPER_OUTLIER: [],
MaidrKey.FILL.value: "1",
MaidrKey.LOWER_OUTLIER.value: [],
MaidrKey.MIN.value: 4.0,
MaidrKey.Q1.value: 4.5,
MaidrKey.Q2.value: 5.0,
MaidrKey.Q3.value: 5.5,
MaidrKey.MAX.value: 6.0,
MaidrKey.UPPER_OUTLIER.value: [],
},
{
MaidrKey.LOWER_OUTLIER: [],
MaidrKey.MIN: 7.0,
MaidrKey.Q1: 7.5,
MaidrKey.Q2: 8.0,
MaidrKey.Q3: 8.5,
MaidrKey.MAX: 9.0,
MaidrKey.UPPER_OUTLIER: [],
MaidrKey.FILL.value: "2",
MaidrKey.LOWER_OUTLIER.value: [],
MaidrKey.MIN.value: 7.0,
MaidrKey.Q1.value: 7.5,
MaidrKey.Q2.value: 8.0,
MaidrKey.Q3.value: 8.5,
MaidrKey.MAX.value: 9.0,
MaidrKey.UPPER_OUTLIER.value: [],
},
],
}
Expand Down
Loading