Skip to content

Commit

Permalink
feat: support 2-layered scatter plot
Browse files Browse the repository at this point in the history
krishnanand5 committed Aug 27, 2024
1 parent 529c721 commit 6469e24
Showing 6 changed files with 3,114 additions and 8 deletions.
36 changes: 36 additions & 0 deletions example/scatter/example_scatter_2_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import matplotlib.pyplot as plt
import seaborn as sns

import maidr


penguins = sns.load_dataset("penguins")

# Initialize the plot
fig, ax = plt.subplots(figsize=(10, 6))

# Prepare Scatter plot layer
sns.scatterplot(data=penguins, x="flipper_length_mm", y="body_mass_g", s=100, ax=ax)

# Prepare Line plot layer
sns.regplot(
data=penguins,
x="flipper_length_mm",
y="body_mass_g",
scatter=False,
lowess=True,
line_kws={"color": "black"},
ax=ax
)

ax.set_title("Penguin Flipper Length vs Body Mass (Smoothed)", fontsize=16)
ax.set_xlabel("Flipper Length (mm)", fontsize=14)
ax.set_ylabel("Body Mass (g)", fontsize=14)

# Create a Maidr instance
maidr_plot = maidr.Maidr(fig)

html_string = maidr_plot.render(html=True).get_html_string()

with open("example_scatter_2_layered.html", "w") as f:
f.write(html_string)
2,173 changes: 2,173 additions & 0 deletions example/scatter/example_scatter_2_layered.html

Large diffs are not rendered by default.

790 changes: 790 additions & 0 deletions example_scatter_2_layered.html

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions maidr/core/maidr.py
Original file line number Diff line number Diff line change
@@ -51,9 +51,9 @@ def plots(self) -> list[MaidrPlot]:
"""Return the list of plots extracted from the ``fig``."""
return self._plots

def render(self) -> Tag:
def render(self, html: bool = False) -> Tag:
"""Return the maidr plot inside an iframe."""
return self._create_html_tag()
return self._create_html_tag(html)

def save_html(
self, file: str, *, lib_dir: str | None = "lib", include_version: bool = True
@@ -93,15 +93,15 @@ def destroy(self) -> None:
del self._plots
del self._fig

def _create_html_tag(self) -> Tag:
def _create_html_tag(self, html: bool = False) -> Tag:
"""Create the MAIDR HTML using HTML tags."""
tagged_elements = [element for plot in self._plots for element in plot.elements]
with HighlightContextManager.set_maidr_elements(tagged_elements):
svg = self._get_svg()
maidr = f"\nlet maidr = {json.dumps(self._flatten_maidr(), indent=2)}\n"

# Inject plot's svg and MAIDR structure into html tag.
return Maidr._inject_plot(svg, maidr)
return Maidr._inject_plot(svg, maidr, html)

def _create_html_doc(self) -> HTMLDocument:
"""Create an HTML document from Tag objects."""
@@ -149,7 +149,7 @@ def _unique_id() -> str:
return str(uuid.uuid4())

@staticmethod
def _inject_plot(plot: HTML, maidr: str) -> Tag:
def _inject_plot(plot: HTML, maidr: str, html: bool = False) -> Tag:
"""Embed the plot and associated MAIDR scripts into the HTML structure."""
base_html = tags.html(
tags.head(
@@ -169,7 +169,8 @@ def _inject_plot(plot: HTML, maidr: str) -> Tag:
),
tags.script(maidr),
)

if html:
return base_html
# Embed the rendering into an iFrame for proper working of JS library.
base_html = tags.iframe(
srcdoc=str(base_html.get_html_string()),
109 changes: 107 additions & 2 deletions poetry.lock
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ lxml = ">=5.1.0"
htmltools = ">=0.5"
jupyter = "^1.0.0"
wrapt = "^1.16.0"
statsmodels = "^0.14.2"

[tool.poetry.group.dev.dependencies]
black = "23.3.0"

0 comments on commit 6469e24

Please sign in to comment.