-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplots.py
102 lines (84 loc) · 3.42 KB
/
plots.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
import pandas as pd
import altair as alt
alt.data_transformers.enable("vegafusion")
def spectra_plot(df, x, y, color, ruler=False):
"""Plot spectra viewer"""
selection = alt.selection_point(fields=[color], bind='legend')
specView = alt.Chart(df).mark_line(interpolate='basis').encode(
x=x,
y=y,
color=color,
opacity=alt.condition(selection, alt.value(1), alt.value(0.1)),
tooltip=[x, y] if ruler else df.columns.tolist()
).add_params(
selection
)
if ruler:
reduced_df = pd.DataFrame(df[x].unique(), columns=[x])
# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection_point(nearest=True, on='mouseover',
fields=[x], empty=False)
# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(reduced_df).mark_point().encode(
x=x,
opacity=alt.value(0),
).add_params(
nearest
)
# Draw points on the line, and highlight based on selection
points = specView.mark_point().encode(
opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)
# # Draw text labels near the points, and highlight based on selection
# text = specView.mark_text(align='left', dx=5, dy=-5).encode(
# text=alt.condition(nearest, y, alt.value(' '))
# )
# Draw a rule at the location of the selection
rule = alt.Chart(reduced_df).mark_rule(color='gray').encode(
x=x,
).transform_filter(
nearest
)
# Put the five layers into a chart and bind the data
plot = alt.layer(
# possible to add text as last layer to show y value per line:
specView, selectors, points, rule # type: ignore
) if ruler else specView
return plot.configure_legend(
# orient='bottom',
labelLimit=0,
).interactive(
# ).properties(
# width=1400, height=600
)
def metadata_viewer(df, analysis='Raman', treatment='bio', region='VLFR'):
"""Display metadata in a heatmap"""
df = df.loc[(df.Region == region)]
col_name = analysis + ' ' + treatment
highlight = alt.selection_point()
base = alt.Chart(df.reset_index()).encode(
x=alt.X('Exposure_days:N', scale=alt.Scale(paddingInner=0.2), axis=alt.Axis(tickSize=0, domain=False)),
y=alt.Y('Polymer:N', scale=alt.Scale(paddingInner=0.2), axis=alt.Axis(tickSize=0, domain=False, title=None)),
)
heatmap = base.mark_square(size=300, opacity=0.6).encode(
color=alt.Color(col_name, type='quantitative', scale=alt.Scale(scheme='yellowgreen')),
opacity=alt.condition(highlight, alt.value(0.8), alt.value(0.2)),
tooltip=['count(Polymer_ID)', 'Supplier', 'Product_ID', 'Specifications'],
)
numbers = base.mark_text(baseline='middle', dy=1, fontSize=14, font='sans'
).encode(
text=col_name,
opacity=alt.condition(highlight, alt.value(0.8), alt.value(0.2)),
)
chart = alt.layer(heatmap, numbers).interactive().facet(
column=alt.Column('Campaign:N', sort=['Summer', 'Autumn', 'Winter', 'Spring', 'Longterm', 'Insitu']),
row='State:N'
).configure_facet(
spacing=20
).configure_view(
stroke=None
).add_params(
highlight
)
return chart