This repository has been archived by the owner on Nov 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ex_marginal_chart.py
173 lines (138 loc) · 4.74 KB
/
ex_marginal_chart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""Example Marginal-Chart."""
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from dash import html
from implements import implements
from dash_charts.utils_app import AppBase, AppInterface
from dash_charts.utils_fig import MarginalChart, check_raw_data, min_graph
from dash_charts.utils_helpers import parse_dash_cli_args
class SampleMarginalChart(MarginalChart):
"""Sample implementing a custom MarginalChart."""
def create_traces(self, df_raw):
"""Return traces for plotly chart.
Args:
df_raw: pandas dataframe with columns `name: str`, `x: float`, `y: float` and `label: str`
Returns:
list: Dash chart traces
"""
check_raw_data(df_raw, min_keys=['name', 'x', 'y', 'label'])
return [
go.Scatter(
mode='markers',
# name=df_raw['name'],
text=df_raw['label'],
x=df_raw['x'],
y=df_raw['y'],
),
]
def create_marg_top(self, df_raw):
"""Return traces for the top marginal chart.
Args:
df_raw: same pandas dataframe as self.create_traces()
Returns:
list: trace data points. List may be empty
"""
return [
go.Bar(
marker_color='royalblue',
# name='TODO,
showlegend=False,
x=df_raw['x'],
y=df_raw['y'],
),
]
def create_marg_right(self, df_raw):
"""Return traces for the top marginal chart.
Args:
df_raw: same pandas dataframe as self.create_traces()
Returns:
list: trace data points. List may be empty
"""
key = 'name'
return [
go.Violin(
marker_color='royalblue',
name=str(name),
showlegend=False,
x=df_raw[key][df_raw[key] == name],
y=df_raw['y'][df_raw[key] == name],
)
for name in np.sort(df_raw[key].unique())
]
def create_layout(self):
"""Extend the standard layout.
Returns:
dict: layout for Dash figure
"""
layout = super().create_layout()
layout['legend'] = {} # Reset legend to default position on top right
layout['showlegend'] = False
return layout
@implements(AppInterface)
class SampleMarginalChartDemo(AppBase):
"""Example creating a Marginal chart."""
name = 'Example Marginal Chart'
"""Application name"""
data_raw = None
"""All in-memory data referenced by callbacks and plotted. If modified, will impact all viewers."""
chart_main = None
"""Main chart (Marginal)."""
id_chart = 'marginal'
"""Unique name for the main chart."""
def initialization(self) -> None:
"""Initialize ids with `self.register_uniq_ids([...])` and other one-time actions."""
super().initialization()
self.register_uniq_ids([self.id_chart])
def create_elements(self) -> None:
"""Initialize the charts, tables, and other Dash elements."""
self.chart_main = SampleMarginalChart(
title='Sample User-Implemented Marginal Chart with Iris dataset',
xlabel='Petal Width',
ylabel='Petal Length',
)
def generate_data(self) -> None:
"""Create self.data_raw with sample data."""
# Create dataframe based on px sample dataset
iris = px.data.iris()
self.data_raw = pd.DataFrame(
data={
'name': iris['species'],
'x': iris['petal_width'],
'y': iris['petal_length'],
'label': None,
},
)
def return_layout(self) -> dict:
"""Return Dash application layout.
Returns:
dict: Dash HTML object
"""
return html.Div(
style={
'maxWidth': '1000px',
'marginRight': 'auto',
'marginLeft': 'auto',
}, children=[
html.H4(children=self.name),
html.Div([
min_graph(
id=self._il[self.id_chart],
figure=self.chart_main.create_figure(
df_raw=self.data_raw,
),
),
]),
],
)
def create_callbacks(self) -> None:
"""Create Dash callbacks."""
... # No callbacks necessary for this simple example
instance = SampleMarginalChartDemo
app = instance()
app.create()
if __name__ == '__main__':
app.run(**parse_dash_cli_args())
else:
FLASK_HANDLE = app.get_server()