-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusage.py
145 lines (130 loc) · 3.91 KB
/
usage.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
import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
import dash_alternative_viz as dav
import plotly_express as px
import altair as alt
from bokeh.embed import json_item
import holoviews as hv
import matplotlib.pyplot as plt
import seaborn as sns
hv.extension("bokeh")
gapminder = px.data.gapminder()
app = dash.Dash(__name__)
app.scripts.config.serve_locally = True
app.css.config.serve_locally = True
td_style = {"width": "50%", "margin": "20px"}
app.layout = html.Div(
[
html.Div(
[
dcc.Slider(
id="year",
min=1952,
max=2007,
step=5,
marks={x: str(x) for x in range(1952, 2008, 5)},
)
],
style={
"width": "600px",
"padding-bottom": "30px",
"margin": "0 auto",
"margin-top": "-70px",
},
),
html.Table(
[
html.Tr(
[
html.Td([dcc.Graph(id="px")], style=td_style),
html.Td([dav.Svg(id="seaborn")], style=td_style),
]
),
html.Tr(
[
html.Td([dav.VegaLite(id="vega")], style=td_style),
html.Td([dav.BokehJSON(id="bokeh")], style=td_style),
]
),
],
style={"width": "1000px", "margin": "0 auto"},
),
]
)
@app.callback(Output("px", "figure"), [Input("year", "value")])
def plotly_fig(year):
df = gapminder.query("year == %d" % (year or 1952))
return px.scatter(
df,
x="gdpPercap",
y="lifeExp",
size="pop",
size_max=30,
color="continent",
log_x=True,
height=400,
width=600,
title="Plotly Express",
hover_name="country",
hover_data=df.columns,
).for_each_trace(lambda t: t.update(name=t.name.replace("continent=", "")))
@app.callback(Output("vega", "spec"), [Input("year", "value")])
def altair_fig(year):
df = gapminder.query("year == %d" % (year or 1952))
return (
alt.Chart(df, height=250, width=400)
.mark_circle()
.encode(
alt.X("gdpPercap:Q", scale=alt.Scale(type="log")),
alt.Y("lifeExp:Q", scale=alt.Scale(zero=False)),
size="pop:Q",
color="continent:N",
tooltip=list(df.columns),
)
.interactive()
.properties(title="Altair / Vega-Lite")
.to_dict()
)
@app.callback(Output("bokeh", "json"), [Input("year", "value")])
def bokeh_fig(year):
df = gapminder.query("year == %d" % (year or 1952))
return json_item(
hv.render(
hv.Points(df, kdims=["gdpPercap", "lifeExp"]).opts(
color="continent",
size=hv.dim("pop") ** (0.5) / 800,
logx=True,
height=330,
width=530,
cmap="Category10",
legend_position="bottom_right",
title="HoloViews / Bokeh",
tools=["hover"],
)
)
)
@app.callback(Output("seaborn", "contents"), [Input("year", "value")])
def seaborn_fig(year):
df = gapminder.query("year == %d" % (year or 1952))
fig, ax = plt.subplots()
sns.scatterplot(
data=df,
ax=ax,
x="gdpPercap",
y="lifeExp",
hue="continent",
size="pop",
sizes=(0, 800),
)
ax.set_xscale("log")
ax.set_title("Seaborn / matplotlib")
fig.set_size_inches(5.5, 3.5)
fig.tight_layout()
from io import BytesIO
b_io = BytesIO()
fig.savefig(b_io, format="svg")
return b_io.getvalue().decode("utf-8")
if __name__ == "__main__":
app.run_server(debug=True)