Skip to content

Commit 1cbf501

Browse files
committed
Altair viz: Convert from DF -> alt.Data
1 parent 14af7c8 commit 1cbf501

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

mesa/experimental/components/altair.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import contextlib
22
from typing import Optional
33

4-
import pandas as pd
54
import solara
65

76
with contextlib.suppress(ImportError):
@@ -36,25 +35,24 @@ def portray(g):
3635
return all_agent_data
3736

3837
all_agent_data = portray(space)
39-
df = pd.DataFrame(all_agent_data)
38+
encoding_dict = {
39+
# no x-axis label
40+
"x": alt.X("x", axis=None, type="ordinal"),
41+
# no y-axis label
42+
"y": alt.Y("y", axis=None, type="ordinal"),
43+
}
44+
has_color = "color" in all_agent_data[0]
45+
if has_color:
46+
encoding_dict["color"] = alt.Color("color", type="nominal")
47+
has_size = "size" in all_agent_data[0]
48+
if has_size:
49+
encoding_dict["size"] = alt.Size("size", type="quantitative")
50+
4051
chart = (
41-
alt.Chart(df)
52+
alt.Chart(alt.Data(values=all_agent_data), encoding=alt.Encoding(**encoding_dict))
4253
.mark_point(filled=True)
43-
.encode(
44-
# no x-axis label
45-
x=alt.X("x", axis=None),
46-
# no y-axis label
47-
y=alt.Y("y", axis=None),
48-
)
54+
.properties(width=space.width * 15, height=space.height * 15)
4955
# .configure_view(strokeOpacity=0) # hide grid/chart lines
5056
)
5157

52-
has_color = hasattr(all_agent_data[0], "color")
53-
if has_color:
54-
chart = chart.encode(color=alt.Color("color"))
55-
56-
has_size = hasattr(all_agent_data[0], "size")
57-
if has_size:
58-
chart = chart.encode(size=alt.Size("size"))
59-
6058
return chart

0 commit comments

Comments
 (0)