afshin-dini commited on
Commit
f66a3de
·
1 Parent(s): 2e3d723

Add hexbin plot

Browse files
src/ai_dashboard/plot_plugin/hexbin_plot.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Hexbin Plot plugin"""
2
+
3
+ from typing import Any, List
4
+ import plotly.express as px
5
+ from dash import html, dcc
6
+ from dash.dependencies import Input, Output
7
+
8
+ from ..base import BasePlotPlugin
9
+
10
+
11
+ class HexbinPlotPlugin(BasePlotPlugin):
12
+ """Hexbin Plot Plugin Class."""
13
+
14
+ name = "Hexbin Plot"
15
+
16
+ def dropdown(self, id_suffix: str, label: str, options: List[str]) -> Any:
17
+ """Create a dropdown control."""
18
+ return html.Div(
19
+ [
20
+ html.Label(label),
21
+ dcc.Dropdown(
22
+ id={"type": "control", "plot": self.name, "axis": id_suffix},
23
+ options=[{"label": c, "value": c} for c in options], # type: ignore
24
+ value=options[0],
25
+ clearable=False,
26
+ persistence=True,
27
+ ),
28
+ ]
29
+ )
30
+
31
+ def controls(self) -> Any:
32
+ """Render the control panel for the hexbin plot."""
33
+ nums = self.numeric_columns()
34
+ return html.Div(
35
+ [
36
+ self.dropdown("x", "X-Axis", nums),
37
+ self.dropdown("y", "Y-Axis", nums),
38
+ ]
39
+ )
40
+
41
+ def render(self, **kwargs: Any) -> Any: # pylint: disable=W0201
42
+ """Render the hexbin plot based on selected axes."""
43
+ fig = px.density_heatmap(
44
+ self.dataframe,
45
+ x=kwargs["x_axis"],
46
+ y=kwargs["y_axis"],
47
+ nbinsx=20,
48
+ nbinsy=20,
49
+ color_continuous_scale="Viridis",
50
+ )
51
+ return dcc.Graph(figure=fig)
52
+
53
+ def register_callbacks(self, app: Any) -> None:
54
+ """Register callbacks for the hexbin plot."""
55
+
56
+ @app.callback( # type: ignore
57
+ Output({"type": "plot-output", "plot": self.name}, "children"),
58
+ Input({"type": "control", "plot": self.name, "axis": "x"}, "value"),
59
+ Input({"type": "control", "plot": self.name, "axis": "y"}, "value"),
60
+ )
61
+ def update(x_axis: str, y_axis: str) -> Any:
62
+ """Update the hexbin plot based on selected axes."""
63
+ return self.render(x_axis=x_axis, y_axis=y_axis)