Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def visualize(display_df):
viridis = ['#440154', '#472c7a', '#3b518b', '#2c718e', '#21908d', '#27ad81', '#5cc863', '#aadc32', '#fde725']
import altair as alt
color_scale = alt.Scale(
domain=(display_df.dropna().trending.min(),
0,
display_df.dropna().trending.max()),
range=[viridis[0], viridis[len(viridis) // 2], viridis[-1]]
)
return alt.Chart(display_df).mark_circle().encode(
alt.X('variable'),
alt.Y('term'),
size='frequency',
color=alt.Color('trending:Q', scale=color_scale),
)
longitude2="lon2:Q"
).transform_lookup(
lookup="origin",
from_=lookup_data
).transform_lookup(
lookup="destination",
from_=lookup_data,
as_=["state", "lat2", "lon2"]
).transform_filter(
select_city
)
points = alt.Chart(flights_airport).mark_circle().encode(
latitude="latitude:Q",
longitude="longitude:Q",
size=alt.Size("routes:Q", scale=alt.Scale(range=[0, 1000]), legend=None),
order=alt.Order("routes:Q", sort="descending"),
tooltip=["origin:N", "routes:Q"]
).transform_aggregate(
routes="count()",
groupby=["origin"]
).transform_lookup(
lookup="origin",
from_=lookup_data
).transform_filter(
(alt.datum.state != "PR") & (alt.datum.state != "VI")
).add_selection(
select_city
)
(background + connections + points).configure_view(stroke=None)
.mark_bar()
.transform_window(
sort=[alt.SortField(field=default_rank_col, order="ascending")],
# We don't use an alt.WindowFieldDef here because python gets
# confused when you use "as" as an actual argument name. So we just
# use this syntax.
window=[{"op": "row_number", "as": "qurro_x"}],
)
.encode(
# type="ordinal" needed on the scale here to make bars adjacent;
# see https://stackoverflow.com/a/55544817/10730311.
x=alt.X(
"qurro_x",
title="Feature Rankings",
type="ordinal",
scale=alt.Scale(paddingOuter=1, paddingInner=0, rangeStep=1),
axis=alt.Axis(ticks=False, labelAngle=0),
),
y=alt.Y(default_rank_col, type="quantitative"),
color=alt.Color(
"qurro_classification",
title="Log-Ratio Classification",
scale=alt.Scale(
domain=["None", "Numerator", "Denominator", "Both"],
range=["#e0e0e0", "#f00", "#00f", "#949"],
),
),
tooltip=[
alt.Tooltip(
field="qurro_x",
title="Current Ranking",
type="quantitative",
scale=alt.Scale(domain=[min_y, max_y],
nice=False, padding=5)),
size=alt.condition(nearest, alt.value(80), alt.value(30)),
color=alt.Color('kind:N', legend=alt.Legend(title='Legend'))
).add_selection(
nearest
).properties(
height=size,
width=size
).transform_calculate(
neg_z_imag='-datum.z_imag'
).interactive()
bode = alt.Chart(df).mark_circle().encode(
alt.X('f:Q', axis=alt.Axis(title="f [Hz]"),
scale=alt.Scale(type='log', nice=False)),
size=alt.condition(nearest, alt.value(80), alt.value(30)),
color='kind:N'
).add_selection(
nearest
).properties(
width=size,
height=size/2 - 25
).transform_calculate(
mag="sqrt(pow(datum.z_real,2) + pow(datum.z_imag,2))",
neg_phase="-(180/PI)*atan(datum.z_imag/datum.z_real)"
).interactive()
bode_mag = bode.encode(y=alt.Y('mag:Q',
axis=alt.Axis(title="|Z| [Ω]")))
bode_phs = bode.encode(y=alt.Y('neg_phase:Q',
axis=alt.Axis(title="-ϕ [°]")))
source = data.population.url
slider = alt.binding_range(min=1850, max=2000, step=10)
select_year = alt.selection_single(name='year', fields=['year'], bind=slider)
base = alt.Chart(source).add_selection(
select_year
).transform_filter(
select_year
).transform_calculate(
gender=alt.expr.if_(alt.datum.sex == 1, 'Male', 'Female')
)
color_scale = alt.Scale(domain=['Male', 'Female'],
range=['#1f77b4', '#e377c2'])
left = base.transform_filter(
alt.datum.gender == 'Female'
).encode(
y=alt.Y('age:O', axis=None),
x=alt.X('sum(people):Q',
title='population',
sort=alt.SortOrder('descending')),
color=alt.Color('gender:N', scale=color_scale, legend=None)
).mark_bar().properties(title='Female')
middle = base.encode(
y=alt.Y('age:O', axis=None),
text=alt.Text('age:Q'),
).mark_text().properties(width=20)
find the plot command
>>> e = {'encoding': 'x', 'field': 'petalWidth', 'scale': 'log'}
>>> r = _get_plot_command(e)
>>> assert r.to_dict() == {'field': 'petalWidth', 'scale': {'type': 'log'}}
"""
d = {k: v for k, v in e.items()}
if 'field' not in e:
return
encoding = d.pop('encoding')
column = d.pop('field')
scale = {}
if any([key in d for key in ['scale', 'zero']]):
scale = {'scale': altair.Scale(type=d.pop('scale', None),
zero=d.pop('zero', None))}
d.update(scale)
return getattr(altair, encoding.capitalize())(column, **d)
)
.encode(
# type="ordinal" needed on the scale here to make bars adjacent;
# see https://stackoverflow.com/a/55544817/10730311.
x=alt.X(
"qurro_x",
title="Feature Rankings",
type="ordinal",
scale=alt.Scale(paddingOuter=1, paddingInner=0, rangeStep=1),
axis=alt.Axis(ticks=False, labelAngle=0),
),
y=alt.Y(default_rank_col, type="quantitative"),
color=alt.Color(
"qurro_classification",
title="Log-Ratio Classification",
scale=alt.Scale(
domain=["None", "Numerator", "Denominator", "Both"],
range=["#e0e0e0", "#f00", "#00f", "#949"],
),
),
tooltip=[
alt.Tooltip(
field="qurro_x",
title="Current Ranking",
type="quantitative",
),
alt.Tooltip(
field="qurro_classification",
title="Log-Ratio Classification",
type="nominal",
),
"Feature ID",
annotation = [None] * len(spec.mz)
color = [colors[None]] * len(spec.mz)
calc_mz = [a.calc_mz if a is not None else None for a in annotation]
fragment = [str(a) if a is not None else None for a in annotation]
spec_df = pd.DataFrame({'exp_mz': spec.mz, 'intensity': intensity,
'calc_mz': calc_mz, 'fragment': fragment,
'color': color})
x_axis = altair.X('exp_mz', axis=altair.Axis(title='m/z',
titleFontStyle='italic',
grid=grid),
scale=altair.Scale(nice=True, padding=5, zero=False))
y_axis = altair.Y('intensity',
axis=altair.Axis(title='Intensity', format='%',
grid=grid),
scale=altair.Scale(nice=True, padding=5))
color = altair.Color('color', scale=None, legend=None)
tooltip_not_annotated = [altair.Tooltip('exp_mz', format='.4f',
title='Experimental m/z'),
altair.Tooltip('intensity', format='.0%',
title='Intensity')]
tooltip_annotated = [altair.Tooltip('fragment', title='Fragment'),
altair.Tooltip('exp_mz', format='.4f',
title='Experimental m/z'),
altair.Tooltip('calc_mz', format='.4f',
title='Theoretical m/z'),
altair.Tooltip('intensity', format='.0%',
title='Intensity')]
# Unannotated peaks.
spec_plot = (altair.Chart(spec_df[spec_df['fragment'].isna()])
.mark_rule(size=2).encode(x=x_axis, y=y_axis, color=color,
tooltip=tooltip_not_annotated))
def person_class_bar():
dat_ori = getuserdb()
dat = dat_ori.loc[dat_ori.this_user == True,:].copy()
dat['mnth_yr'] = dat['workshop_start'].dt.to_period('M').astype(str)
dat = dat.melt(
id_vars=['mnth_yr', 'workshop_category'],
value_vars=['workshop_hours', 'class_size'])
chart = alt.Chart(dat).mark_bar().encode(
column='variable',
x=alt.X("sum(value)"),
y=alt.Y('mnth_yr'),
color=alt.Color(
'workshop_category',
scale=alt.Scale(range=['#7dbbd2cc', '#bbc6cbe6', '#6eb0ea', '#d1d8e2', '#1a1d21', '#8f9fb3' ]),legend=None),
tooltip=['workshop_category', 'sum(value)']
).configure_axis(
grid=False
).properties(
width=250
)
return chart.to_json()
x=alt.X(
"x",
type="ordinal",
axis=alt.Axis(
title="Gene",
ticks=False,
labels=False,
orient=alt.AxisOrient("top"),
),
),
y=alt.Y(
"y",
type="ordinal",
axis=alt.Axis(title="Gene", ticks=False, labels=False),
),
color=alt.Color("z:Q", scale=alt.Scale(scheme="viridis", domain=[-1, 1])),
)
.properties(width=250, height=250)
)