Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
color = alt.Color('weather:N', scale=scale)
# We create two selections:
# - a brush that is active on the top panel
# - a multi-click that is active on the bottom panel
brush = alt.selection_interval(encodings=['x'])
click = alt.selection_multi(encodings=['color'])
# Top panel is scatter plot of temperature vs time
points = alt.Chart().mark_point().encode(
alt.X('monthdate(date):T', title='Date'),
alt.Y('temp_max:Q',
title='Maximum Daily Temperature (C)',
scale=alt.Scale(domain=[-5, 40])
),
color=alt.condition(brush, color, alt.value('lightgray')),
size=alt.Size('precipitation:Q', scale=alt.Scale(range=[5, 200]))
).properties(
width=550,
height=300
).add_selection(
brush
).transform_filter(
click
)
# Bottom panel is a bar chart of weather type
bars = alt.Chart().mark_bar().encode(
x='count()',
y='weather:N',
color=alt.condition(click, color, alt.value('lightgray')),
).transform_filter(
==============================
This example shows a bar chart with both positive and negative values.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.us_employment()
alt.Chart(source).mark_bar().encode(
x="month:T",
y="nonfarm_change:Q",
color=alt.condition(
alt.datum.nonfarm_change > 0,
alt.value("steelblue"), # The positive color
alt.value("orange") # The negative color
)
).properties(width=600)
color_scale = alt.Scale(domain=['M', 'F'],
range=['#1FC3AA', '#8624F5'])
base = alt.Chart(source).properties(
width=250,
height=250
).add_selection(selector)
points = base.mark_point(filled=True, size=200).encode(
x=alt.X('mean(height):Q',
scale=alt.Scale(domain=[0,84])),
y=alt.Y('mean(weight):Q',
scale=alt.Scale(domain=[0,250])),
color=alt.condition(selector,
'gender:N',
alt.value('lightgray'),
scale=color_scale),
)
hists = base.mark_bar(opacity=0.5, thickness=100).encode(
x=alt.X('age',
bin=alt.Bin(step=5), # step keeps bin size the same
scale=alt.Scale(domain=[0,100])),
y=alt.Y('count()',
stack=None,
scale=alt.Scale(domain=[0,350])),
color=alt.Color('gender:N',
scale=color_scale)
).transform_filter(
selector
)
This example shows a histogram with a global mean overlay.
"""
# category: histograms
import altair as alt
from vega_datasets import data
source = data.movies.url
bar = alt.Chart(source).mark_bar().encode(
alt.X('IMDB_Rating:Q', bin=True, axis=None),
alt.Y('count()')
)
rule = alt.Chart(source).mark_rule(color='red').encode(
x='mean(IMDB_Rating):Q',
size=alt.value(5)
)
bar + rule
chart = source.transform_filter(
alt.FieldOneOfPredicate(field="airport", oneOf=subset)
)
highlight = alt.selection(
type="single", nearest=True, on="mouseover", fields=["airport"]
)
points = (
chart.mark_point()
.encode(
x="day",
y=alt.Y("count", title="# of departing flights"),
color=alt.Color("airport", legend=alt.Legend(title=name)),
tooltip=["day", "airport", "city", "count"],
opacity=alt.value(0.3),
)
.add_selection(highlight)
)
lines = (
chart.mark_line()
.encode(
x="day",
y="count",
color="airport",
size=alt.condition(~highlight, alt.value(1), alt.value(3)),
)
.transform_loess("day", "count", groupby=["airport"], bandwidth=0.2)
)
return lines + points
x="day",
y=alt.Y("count", title="# of departing flights"),
color=alt.Color("airport", legend=alt.Legend(title=name)),
tooltip=["day", "airport", "city", "count"],
opacity=alt.value(0.3),
)
.add_selection(highlight)
)
lines = (
chart.mark_line()
.encode(
x="day",
y="count",
color="airport",
size=alt.condition(~highlight, alt.value(1), alt.value(3)),
)
.transform_loess("day", "count", groupby=["airport"], bandwidth=0.2)
)
return lines + points
Returns
-------
altair.LayerChart
The Altair chart instance with the plotted spectrum.
"""
if spectrum_kws is None:
spectrum_kws = {}
# Top spectrum.
spec_plot = spectrum(spec_top, mirror_intensity=False, **spectrum_kws)
# Mirrored bottom spectrum.
spec_plot += spectrum(spec_bottom, mirror_intensity=True, **spectrum_kws)
spec_plot += (altair.Chart(pd.DataFrame({'sep': [0]}))
.mark_rule(size=3).encode(
y='sep', color=altair.value('lightGray')))
return spec_plot
chart = self._plot(
data=df, width=width, height=height, title=kwds.get("title", None)
)
chart = chart.mark_area().encode(
x=_x(x, df),
y=alt.Y(
value_name,
type=infer_vegalite_type(df[value_name]),
stack=(None, "zero")[stacked],
),
color=alt.Color(field=var_name, type=infer_vegalite_type(df[var_name])),
)
if alpha is not None:
assert 0 <= alpha <= 1
chart = chart.encode(opacity=alt.value(alpha))
if ax is not None:
return ax + chart
return chart
"""
# category: scatter plots
import altair as alt
from vega_datasets import data
cars = data.cars()
brush = alt.selection(type='interval')
tick_axis = alt.Axis(labels=False, domain=False, ticks=False)
tick_axis_notitle = alt.Axis(labels=False, domain=False, ticks=False, title='')
points = alt.Chart(cars).mark_point().encode(
x=alt.X('Miles_per_Gallon', axis=alt.Axis(title='')),
y=alt.Y('Horsepower', axis=alt.Axis(title='')),
color=alt.condition(brush, 'Origin', alt.value('grey'))
).add_selection(
brush
)
x_ticks = alt.Chart(cars).mark_tick().encode(
alt.X('Miles_per_Gallon', axis=tick_axis),
alt.Y('Origin', axis=tick_axis_notitle),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
).add_selection(
brush
)
y_ticks = alt.Chart(cars).mark_tick().encode(
alt.X('Origin', axis=tick_axis_notitle),
alt.Y('Horsepower', axis=tick_axis),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))