forked from robertodr/tddft_psicon2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaltair_spectrum.py
49 lines (41 loc) · 1.42 KB
/
altair_spectrum.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
from typing import Tuple, Dict
import pandas as pd
import altair as alt
def plot_spectrum(data: Dict,
*,
title: str = "",
x_title: Tuple[str, str] = ("ω", "au"),
y_title: Tuple[str, str] = ("ε", "L⋅mol⁻¹⋅cm⁻¹"),
offset: int = 0):
hover = alt.selection_single(
fields=["x"],
nearest=True,
on="mouseover",
empty="none",
clear="mouseout"
)
s1 = pd.DataFrame(data["convolution"])
lines = alt.Chart(s1).mark_line(size=1.5).encode(
x=alt.X("x", axis=alt.Axis(title=f"{x_title[0]} [{x_title[1]}]", offset=offset)),
y=alt.Y("y", axis=alt.Axis(title=f"{y_title[0]} [{y_title[1]}]")),
)
points = lines.transform_filter(hover).mark_circle()
tooltips = alt.Chart(s1).mark_rule().encode(
x='x:Q',
opacity=alt.condition(hover, alt.value(0.3), alt.value(0)),
tooltip=[alt.Tooltip("x:Q", format=".4f", title=f"{x_title[0]}"), alt.Tooltip("y:Q", format=".1f", title=f"{y_title[0]}")]
).add_selection(
hover
)
s2 = pd.DataFrame(data["sticks"])
sticks = alt.Chart(s2).mark_bar(size=2, opacity=0.2, color="red").encode(
x="poles:Q",
y="residues:Q",
)
# Put the layers into a chart and bind the data
plot = alt.layer(
lines, points, tooltips, sticks,
).properties(
title=title,
)
return plot