Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert x in data.columns
if y is None:
y_values = list(data.columns[1:])
else:
y = _valid_column(y)
assert y in data.columns
y_values = [y]
return (
alt.Chart(data, mark=self._get_mark_def(mark, kwargs))
.transform_fold(y_values, as_=["column", "value"])
.encode(
x=x,
y=alt.Y("value:Q", title=None, stack=stacked),
color=alt.Color("column:N", title=None),
tooltip=[x] + y_values,
)
if "flight_id" in self.data.columns:
feature_list.append("flight_id")
if "callsign" in self.data.columns:
feature_list.append("callsign")
if "icao24" in self.data.columns:
feature_list.append("icao24")
if isinstance(y, alt.Y):
alt_y = y
y = y.shorthand
if isinstance(y, str):
feature_list.append(y)
data = self.data[feature_list].query(f"{y} == {y}")
default_encode = dict(
x="timestamp:T",
y=alt_y if alt_y is not None else alt.Y(y, title=y),
color=alt.Color(
"flight_id"
if "flight_id" in data.columns
else (
"callsign" if "callsign" in data.columns else "icao24"
)
),
)
else:
feature_list += y
data = (
self.data[feature_list]
.melt("timestamp", y)
.query("value == value")
)
default_encode = dict(x="timestamp:T", y="value", color="variable")
def scatterplot(
data=None, x=0, y=1, color=None, opacity=1, tooltip=None, height=600, width=800
):
"""Generate a scatterplot."""
if color is not None:
if isinstance(data[color].iloc[0], Number):
color = alt.Color(
color, scale=hue_scale_light if opacity == 1 else hue_scale_dark
)
opt_args = choose_kwargs(locals(), ["color", "tooltip"])
return alt.Chart(
data,
height=height,
width=width,
mark=alt.MarkDef(type="point" if opacity == 1 else "circle", opacity=opacity),
).encode(x=x, y=y, **opt_args)
models = [ Instance.resolve_default_model(None) ]
output = []
for model in models:
#Instance.set_default_model(model=model)
data = self.serialize(instance_hash, instance_hash_rewritten, filtered_instances, model)
for flip, count in data["counts"].items():
output.append({
"flip": flip,
"count": count,
"model": model
})
df = pd.DataFrame(output)
chart = alt.Chart(df).mark_bar().encode(
y=alt.Y('model:N'),
x=alt.X('count:Q', stack="zero"),
color=alt.Color('flip:N', scale=alt.Scale(
range=["#1f77b4", "#ff7f0e", "#c7c7c7"],
domain=["flip_to_correct", "flip_to_incorrect", "unflip"])),
tooltip=['model:N', 'count:Q', 'correctness:N']
).properties(width=100)#.configure_facet(spacing=5)#
return chart
else:
marker_attrs["fill"] = node_color
##### alpha
if isinstance(alpha, str):
encoded_attrs["opacity"] = alpha
elif isinstance(alpha, int) or isinstance(alpha, float):
marker_attrs["opacity"] = alpha
elif alpha is not None:
raise Exception("alpha must be a string or None.")
##### cmap
if isinstance(cmap, str):
encoded_attrs["fill"] = alt.Color(
node_color,
scale=alt.Scale(scheme=cmap))
elif cmap is not None:
raise Exception("cmap must be a string (colormap name) or None.")
if tooltip is not None:
encoded_attrs['tooltip'] = tooltip
marker_attrs['strokeWidth'] = linewidths
# ---------- Construct visualization ------------
node_chart = node_chart.mark_point(
**marker_attrs
).encode(
x=alt.X('x', axis=alt.Axis(grid=False, labels=False, ticks=False)),
range=["#c30d24", "#f3a583", "#cccccc", "#94c6da", "#1770ab"]
)
y_axis = alt.Axis(title='Question',
offset=5,
ticks=False,
minExtent=60,
domain=False)
source = alt.pd.DataFrame(data)
alt.Chart(source).mark_bar().encode(
x='percentage_start:Q',
x2='percentage_end:Q',
y=alt.Y('question:N', axis=y_axis),
color=alt.Color(
'type:N',
legend=alt.Legend( title='Response'),
scale=color_scale,
)
Repeated Choropleth Map
=======================
Three choropleths representing disjoint data from the same table.
"""
# category: maps
import altair as alt
from vega_datasets import data
states = alt.topo_feature(data.us_10m.url, 'states')
pop_eng_hur = data.population_engineers_hurricanes.url
variable_list = ['population', 'engineers', 'hurricanes']
alt.Chart(states).mark_geoshape().encode(
alt.Color(alt.repeat('row'), type='quantitative')
).transform_lookup(
lookup='id',
from_=alt.LookupData(pop_eng_hur, 'id', variable_list)
).properties(
width=500,
height=300
).project(
type='albersUsa'
).repeat(
row=variable_list
).resolve_scale(
color='independent'
)
This example shows the 2010 daily high temperature (F) in Seattle, WA.
"""
# category: case studies
import altair as alt
from vega_datasets import data
# Since the data is more than 5,000 rows we'll import it from a URL
source = data.seattle_temps.url
alt.Chart(
source,
title="2010 Daily High Temperature (F) in Seattle, WA"
).mark_rect().encode(
x='date(date):O',
y='month(date):O',
color=alt.Color('max(temp):Q', scale=alt.Scale(scheme="inferno")),
tooltip=[
alt.Tooltip('monthdate(date):T', title='Date'),
alt.Tooltip('max(temp):Q', title='Max Temp')
]
).properties(width=550)