-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscatter-demo.py
More file actions
87 lines (70 loc) · 2.03 KB
/
Copy pathscatter-demo.py
File metadata and controls
87 lines (70 loc) · 2.03 KB
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
# /// script
# requires-python = ">=3.14"
# dependencies = [
# "marimo>=0.19.6",
# "pandas",
# "drawdata",
# "altair",
# ]
# ///
import marimo
__generated_with = "0.19.11"
app = marimo.App(width="medium")
with app.setup:
import marimo as mo
import altair as alt
from drawdata import ScatterWidget
@app.cell(hide_code=True)
def _():
mo.md(r"""
# Drawing a `ScatterChart`
This notebook contains a demo of the `ScatterWidget` inside of the [drawdata](https://github.com/koaning/drawdata) library.
""")
return
@app.cell(hide_code=True)
def _():
widget = mo.ui.anywidget(ScatterWidget(height=400))
widget
return (widget,)
@app.cell(hide_code=True)
def _(widget):
base = alt.Chart(widget.data_as_pandas)
base_bar = base.mark_bar(opacity=0.3, binSpacing=0)
color_domain = widget.data_as_pandas["color"].unique()
xscale = alt.Scale(domain=(0, 800))
yscale = alt.Scale(domain=(0, 500))
colscale = alt.Scale(domain=color_domain, range=color_domain)
points = base.mark_circle().encode(
alt.X("x").scale(xscale),
alt.Y("y").scale(yscale),
color="color",
)
top_hist = (
base_bar
.encode(
alt.X("x:Q")
# when using bins, the axis scale is set through
# the bin extent, so we do not specify the scale here
# (which would be ignored anyway)
.bin(maxbins=30, extent=xscale.domain).stack(None).title(""),
alt.Y("count()").stack(None).title(""),
alt.Color("color:N", scale=colscale),
)
.properties(height=60)
)
right_hist = (
base_bar
.encode(
alt.Y("y:Q")
.bin(maxbins=30, extent=yscale.domain)
.stack(None)
.title(""),
alt.X("count()").stack(None).title(""),
alt.Color("color:N"),
)
.properties(width=60)
)
top_hist & (points | right_hist)
return
if __name__ == "__main__":
app.run()