first commit

This commit is contained in:
Ayxan
2022-05-23 00:16:32 +04:00
commit d660f2a4ca
24786 changed files with 4428337 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
# flake8: noqa
__version__ = "4.2.0"
from .vegalite import *
from . import examples
def load_ipython_extension(ipython):
from ._magics import vega, vegalite
ipython.register_magic_function(vega, "cell")
ipython.register_magic_function(vegalite, "cell")

View File

@@ -0,0 +1,177 @@
"""
Magic functions for rendering vega/vega-lite specifications
"""
__all__ = ["vega", "vegalite"]
import json
import warnings
import IPython
from IPython.core import magic_arguments
import pandas as pd
from toolz import curried
from altair.vegalite import v3 as vegalite_v3
from altair.vegalite import v4 as vegalite_v4
from altair.vega import v5 as vega_v5
try:
import yaml
YAML_AVAILABLE = True
except ImportError:
YAML_AVAILABLE = False
RENDERERS = {
"vega": {"5": vega_v5.Vega},
"vega-lite": {"3": vegalite_v3.VegaLite, "4": vegalite_v4.VegaLite},
}
TRANSFORMERS = {
"vega": {
# Vega doesn't yet have specific data transformers; use vegalite
"5": vegalite_v4.data_transformers,
},
"vega-lite": {
"3": vegalite_v3.data_transformers,
"4": vegalite_v4.data_transformers,
},
}
def _prepare_data(data, data_transformers):
"""Convert input data to data for use within schema"""
if data is None or isinstance(data, dict):
return data
elif isinstance(data, pd.DataFrame):
return curried.pipe(data, data_transformers.get())
elif isinstance(data, str):
return {"url": data}
else:
warnings.warn("data of type {} not recognized".format(type(data)))
return data
def _get_variable(name):
"""Get a variable from the notebook namespace."""
ip = IPython.get_ipython()
if ip is None:
raise ValueError(
"Magic command must be run within an IPython "
"environemnt, in which get_ipython() is defined."
)
if name not in ip.user_ns:
raise NameError(
"argument '{}' does not match the "
"name of any defined variable".format(name)
)
return ip.user_ns[name]
@magic_arguments.magic_arguments()
@magic_arguments.argument(
"data",
nargs="*",
help="local variable name of a pandas DataFrame to be used as the dataset",
)
@magic_arguments.argument("-v", "--version", dest="version", default="5")
@magic_arguments.argument("-j", "--json", dest="json", action="store_true")
def vega(line, cell):
"""Cell magic for displaying Vega visualizations in CoLab.
%%vega [name1:variable1 name2:variable2 ...] [--json] [--version='5']
Visualize the contents of the cell using Vega, optionally specifying
one or more pandas DataFrame objects to be used as the datasets.
If --json is passed, then input is parsed as json rather than yaml.
"""
args = magic_arguments.parse_argstring(vega, line)
version = args.version
assert version in RENDERERS["vega"]
Vega = RENDERERS["vega"][version]
data_transformers = TRANSFORMERS["vega"][version]
def namevar(s):
s = s.split(":")
if len(s) == 1:
return s[0], s[0]
elif len(s) == 2:
return s[0], s[1]
else:
raise ValueError("invalid identifier: '{}'".format(s))
try:
data = list(map(namevar, args.data))
except ValueError:
raise ValueError("Could not parse arguments: '{}'".format(line))
if args.json:
spec = json.loads(cell)
elif not YAML_AVAILABLE:
try:
spec = json.loads(cell)
except json.JSONDecodeError:
raise ValueError(
"%%vega: spec is not valid JSON. "
"Install pyyaml to parse spec as yaml"
)
else:
spec = yaml.load(cell, Loader=yaml.FullLoader)
if data:
spec["data"] = []
for name, val in data:
val = _get_variable(val)
prepped = _prepare_data(val, data_transformers)
prepped["name"] = name
spec["data"].append(prepped)
return Vega(spec)
@magic_arguments.magic_arguments()
@magic_arguments.argument(
"data",
nargs="?",
help="local variablename of a pandas DataFrame to be used as the dataset",
)
@magic_arguments.argument("-v", "--version", dest="version", default="4")
@magic_arguments.argument("-j", "--json", dest="json", action="store_true")
def vegalite(line, cell):
"""Cell magic for displaying vega-lite visualizations in CoLab.
%%vegalite [dataframe] [--json] [--version=3]
Visualize the contents of the cell using Vega-Lite, optionally
specifying a pandas DataFrame object to be used as the dataset.
if --json is passed, then input is parsed as json rather than yaml.
"""
args = magic_arguments.parse_argstring(vegalite, line)
version = args.version
assert version in RENDERERS["vega-lite"]
VegaLite = RENDERERS["vega-lite"][version]
data_transformers = TRANSFORMERS["vega-lite"][version]
if args.json:
spec = json.loads(cell)
elif not YAML_AVAILABLE:
try:
spec = json.loads(cell)
except json.JSONDecodeError:
raise ValueError(
"%%vegalite: spec is not valid JSON. "
"Install pyyaml to parse spec as yaml"
)
else:
spec = yaml.load(cell, Loader=yaml.FullLoader)
if args.data is not None:
data = _get_variable(args.data)
spec["data"] = _prepare_data(data, data_transformers)
return VegaLite(spec)

View File

@@ -0,0 +1,22 @@
"""Datasets for use with Altair (deprecated)
The functions here are merely thin wrappers for data access routines in the
vega_datasets package.
"""
from altair.utils.deprecation import deprecated
@deprecated("load_dataset is deprecated. " "Use the vega_datasets package instead.")
def load_dataset(name):
"""Load a dataset by name as a pandas.DataFrame."""
import vega_datasets
return vega_datasets.data(name)
@deprecated("load_dataset is deprecated. " "Use the vega_datasets package instead.")
def list_datasets():
"""List the available datasets."""
import vega_datasets
return vega_datasets.data.list_datasets()

View File

@@ -0,0 +1,17 @@
import os
def iter_examples():
"""Iterate over the examples in this directory.
Each item is a dict with the following keys:
- "name" : the unique name of the example
- "filename" : the full file path to the example
"""
example_dir = os.path.abspath(os.path.dirname(__file__))
for filename in os.listdir(example_dir):
name, ext = os.path.splitext(filename)
if name.startswith('_') or ext != '.py':
continue
yield {'name': name,
'filename': os.path.join(example_dir, filename)}

View File

@@ -0,0 +1,70 @@
"""
Connections Among U.S. Airports Interactive
-------------------------------------------
This example shows all the connections between major U.S. airports. Lookup transformations
are used to find the coordinates of each airport and connecting airports. Connections
are displayed on mouseover via a single selection.
"""
# category: case studies
import altair as alt
from vega_datasets import data
# Since these data are each more than 5,000 rows we'll import from the URLs
airports = data.airports.url
flights_airport = data.flights_airport.url
states = alt.topo_feature(data.us_10m.url, feature="states")
# Create mouseover selection
select_city = alt.selection_single(
on="mouseover", nearest=True, fields=["origin"], empty="none"
)
# Define which attributes to lookup from airports.csv
lookup_data = alt.LookupData(
airports, key="iata", fields=["state", "latitude", "longitude"]
)
background = alt.Chart(states).mark_geoshape(
fill="lightgray",
stroke="white"
).properties(
width=750,
height=500
).project("albersUsa")
connections = alt.Chart(flights_airport).mark_rule(opacity=0.35).encode(
latitude="latitude:Q",
longitude="longitude:Q",
latitude2="lat2:Q",
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)

View File

@@ -0,0 +1,33 @@
"""
Locations of US Airports
========================
This is a layered geographic visualization that shows the positions of US
airports on a background of US states.
"""
# category: case studies
import altair as alt
from vega_datasets import data
airports = data.airports()
states = alt.topo_feature(data.us_10m.url, feature='states')
# US states background
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
).properties(
width=500,
height=300
).project('albersUsa')
# airport positions on background
points = alt.Chart(airports).mark_circle(
size=10,
color='steelblue'
).encode(
longitude='longitude:Q',
latitude='latitude:Q',
tooltip=['name', 'city', 'state']
)
background + points

View File

@@ -0,0 +1,39 @@
"""
Locations of US Airports
========================
This is a layered geographic visualization that shows the positions of US
airports on a background of US states.
"""
# category: maps
import altair as alt
from vega_datasets import data
airports = data.airports.url
states = alt.topo_feature(data.us_10m.url, feature='states')
# US states background
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
).properties(
width=500,
height=300
).project('albersUsa')
# airport positions on background
points = alt.Chart(airports).transform_aggregate(
latitude='mean(latitude)',
longitude='mean(longitude)',
count='count()',
groupby=['state']
).mark_circle().encode(
longitude='longitude:Q',
latitude='latitude:Q',
size=alt.Size('count:Q', title='Number of Airports'),
color=alt.value('steelblue'),
tooltip=['state:N','count:Q']
).properties(
title='Number of airports in US'
)
background + points

View File

@@ -0,0 +1,20 @@
"""
Anscombe's Quartet
------------------
This example shows how to use the column channel to make a trellis plot. Anscombe's Quartet is a famous dataset constructed by Francis Anscombe. Common summary statistics are identical for each subset of the data, despite the subsets having vastly different characteristics.
"""
# category: case studies
import altair as alt
from vega_datasets import data
source = data.anscombe()
alt.Chart(source).mark_circle().encode(
alt.X('X', scale=alt.Scale(zero=False)),
alt.Y('Y', scale=alt.Scale(zero=False)),
alt.Facet('Series', columns=2),
).properties(
width=180,
height=180,
)

View File

@@ -0,0 +1,30 @@
"""
Area Chart with Gradient
------------------------
This example shows how to make an area chart with a gradient fill.
For more information about gradient options see the Vega-Lite `Gradient documentation <https://vega.github.io/vega-lite/docs/types.html#gradient>`_.
"""
# category: area charts
import altair as alt
from vega_datasets import data
source = data.stocks()
alt.Chart(source).transform_filter(
'datum.symbol==="GOOG"'
).mark_area(
line={'color':'darkgreen'},
color=alt.Gradient(
gradient='linear',
stops=[alt.GradientStop(color='white', offset=0),
alt.GradientStop(color='darkgreen', offset=1)],
x1=1,
x2=1,
y1=1,
y2=0
)
).encode(
alt.X('date:T'),
alt.Y('price:Q')
)

View File

@@ -0,0 +1,22 @@
"""
Bar Chart with Line on Dual Axis
--------------------------------
This example shows how to combine two plots and keep their axes.
For a more polished version of this chart, see :ref:`gallery_wheat_wages`.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.wheat()
base = alt.Chart(source).encode(x='year:O')
bar = base.mark_bar().encode(y='wheat:Q')
line = base.mark_line(color='red').encode(
y='wages:Q'
)
(bar + line).properties(width=600)

View File

@@ -0,0 +1,15 @@
"""
Horizontal Bar Chart
--------------------
This example is a bar chart drawn horizontally by putting the quantitative value on the x axis.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.wheat()
alt.Chart(source).mark_bar().encode(
x='wheat:Q',
y="year:O"
).properties(height=700)

View File

@@ -0,0 +1,15 @@
"""
Sorted Bar Chart
================
This example shows a bar chart sorted by a calculated value.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
x='sum(yield):Q',
y=alt.Y('site:N', sort='-x')
)

View File

@@ -0,0 +1,50 @@
"""
Compact Trellis Grid of Bar Charts
==================================
This example shows a simple grid of bar charts to compare performance data..
"""
# category: bar charts
import altair as alt
import pandas as pd
source = pd.DataFrame(
[
{"a": "a1", "b": "b1", "c": "x", "p": "0.14"},
{"a": "a1", "b": "b1", "c": "y", "p": "0.60"},
{"a": "a1", "b": "b1", "c": "z", "p": "0.03"},
{"a": "a1", "b": "b2", "c": "x", "p": "0.80"},
{"a": "a1", "b": "b2", "c": "y", "p": "0.38"},
{"a": "a1", "b": "b2", "c": "z", "p": "0.55"},
{"a": "a1", "b": "b3", "c": "x", "p": "0.11"},
{"a": "a1", "b": "b3", "c": "y", "p": "0.58"},
{"a": "a1", "b": "b3", "c": "z", "p": "0.79"},
{"a": "a2", "b": "b1", "c": "x", "p": "0.83"},
{"a": "a2", "b": "b1", "c": "y", "p": "0.87"},
{"a": "a2", "b": "b1", "c": "z", "p": "0.67"},
{"a": "a2", "b": "b2", "c": "x", "p": "0.97"},
{"a": "a2", "b": "b2", "c": "y", "p": "0.84"},
{"a": "a2", "b": "b2", "c": "z", "p": "0.90"},
{"a": "a2", "b": "b3", "c": "x", "p": "0.74"},
{"a": "a2", "b": "b3", "c": "y", "p": "0.64"},
{"a": "a2", "b": "b3", "c": "z", "p": "0.19"},
{"a": "a3", "b": "b1", "c": "x", "p": "0.57"},
{"a": "a3", "b": "b1", "c": "y", "p": "0.35"},
{"a": "a3", "b": "b1", "c": "z", "p": "0.49"},
{"a": "a3", "b": "b2", "c": "x", "p": "0.91"},
{"a": "a3", "b": "b2", "c": "y", "p": "0.38"},
{"a": "a3", "b": "b2", "c": "z", "p": "0.91"},
{"a": "a3", "b": "b3", "c": "x", "p": "0.99"},
{"a": "a3", "b": "b3", "c": "y", "p": "0.80"},
{"a": "a3", "b": "b3", "c": "z", "p": "0.37"},
]
)
alt.Chart(source, width=60, height=alt.Step(8)).mark_bar().encode(
y=alt.Y("c:N", axis=None),
x=alt.X("p:Q", title=None, axis=alt.Axis(format="%")),
color=alt.Color(
"c:N", title="settings", legend=alt.Legend(orient="bottom", titleOrient="left")
),
row=alt.Row("a:N", title="Factor A", header=alt.Header(labelAngle=0)),
column=alt.Column("b:N", title="Factor B"),
)

View File

@@ -0,0 +1,21 @@
"""
Bar Chart with Highlighted Bar
------------------------------
This example shows a basic bar chart with a single bar highlighted.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.wheat()
alt.Chart(source).mark_bar().encode(
x='year:O',
y="wheat:Q",
# The highlight will be set on the result of a conditional statement
color=alt.condition(
alt.datum.year == 1810, # If the year is 1810 this test returns True,
alt.value('orange'), # which sets the bar orange.
alt.value('steelblue') # And if it's not true it sets the bar steelblue.
)
).properties(width=600)

View File

@@ -0,0 +1,30 @@
"""
Bar Chart with Highlighted Segment
----------------------------------
This example shows a bar chart that highlights values beyond a threshold.
"""
import altair as alt
import pandas as pd
from vega_datasets import data
source = data.wheat()
threshold = pd.DataFrame([{"threshold": 90}])
bars = alt.Chart(source).mark_bar().encode(
x="year:O",
y="wheat:Q",
)
highlight = alt.Chart(source).mark_bar(color="#e45755").encode(
x='year:O',
y='baseline:Q',
y2='wheat:Q'
).transform_filter(
alt.datum.wheat > 90
).transform_calculate("baseline", "90")
rule = alt.Chart(threshold).mark_rule().encode(
y='threshold:Q'
)
(bars + highlight + rule).properties(width=600)

View File

@@ -0,0 +1,25 @@
"""
Bar Chart with Labels
=====================
This example shows a basic horizontal bar chart with labels created with Altair.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.wheat()
bars = alt.Chart(source).mark_bar().encode(
x='wheat:Q',
y="year:O"
)
text = bars.mark_text(
align='left',
baseline='middle',
dx=3 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='wheat:Q'
)
(bars + text).properties(height=900)

View File

@@ -0,0 +1,21 @@
"""
Bar Chart with Line at Mean
---------------------------
This example shows the mean value overlayed on a bar chart.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.wheat()
bar = alt.Chart(source).mark_bar().encode(
x='year:O',
y='wheat:Q'
)
rule = alt.Chart(source).mark_rule(color='red').encode(
y='mean(wheat):Q'
)
(bar + rule).properties(width=600)

View File

@@ -0,0 +1,20 @@
"""
Bar Chart with Negative Values
==============================
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)

View File

@@ -0,0 +1,19 @@
"""
Bar Chart with rounded edges
----------------------------
This example shows how to create a bar chart with rounded edges.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
alt.Chart(source).mark_bar(
cornerRadiusTopLeft=3,
cornerRadiusTopRight=3
).encode(
x='month(date):O',
y='count():Q',
color='weather:N'
)

View File

@@ -0,0 +1,27 @@
"""
Bar Chart with Rolling Mean
---------------------------
A bar chart overlayed with a rolling mean. In this example the average of values over the previous decade is displayed as a line.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.wheat()
bar = alt.Chart(source).mark_bar().encode(
x='year:O',
y='wheat:Q'
)
line = alt.Chart(source).mark_line(color='red').transform_window(
# The field to average
rolling_mean='mean(wheat)',
# The number of values before and after the current value to include.
frame=[-9, 0]
).encode(
x='year:O',
y='rolling_mean:Q'
)
(bar + line).properties(width=600)

View File

@@ -0,0 +1,33 @@
"""
Becker's Barley Trellis Plot
----------------------------
The example demonstrates the trellis charts created by Richard Becker, William Cleveland and others in the 1990s. Using the visualization technique below they identified an anomoly in a widely used agriculatural dataset, which they termed `"The Morris Mistake." <http://ml.stat.purdue.edu/stat695t/writings/Trellis.User.pdf>`_. It became their favored way of showcasing the power of this pioneering plot.
"""
# category: case studies
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source, title="The Morris Mistake").mark_point().encode(
alt.X(
'yield:Q',
title="Barley Yield (bushels/acre)",
scale=alt.Scale(zero=False),
axis=alt.Axis(grid=False)
),
alt.Y(
'variety:N',
title="",
sort='-x',
axis=alt.Axis(grid=True)
),
color=alt.Color('year:N', legend=alt.Legend(title="Year")),
row=alt.Row(
'site:N',
title="",
sort=alt.EncodingSortField(field='yield', op='sum', order='descending'),
)
).properties(
height=alt.Step(20)
).configure_view(stroke="transparent")

View File

@@ -0,0 +1,22 @@
"""
Becker's Barley Trellis Plot (wrapped facet)
--------------------------------------------
The example demonstrates the trellis charts created by Richard Becker, William Cleveland and others in the 1990s.
This is the Altair replicate of `the VegaLite version <https://vega.github.io/vega-lite/docs/facet.html#facet-full>`_
demonstrating the usage of `columns` argument to create wrapped facet.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.barley.url
alt.Chart(source).mark_point().encode(
alt.X('median(yield):Q', scale=alt.Scale(zero=False)),
y='variety:O',
color='year:N',
facet=alt.Facet('site:O', columns=2),
).properties(
width=200,
height=100,
)

View File

@@ -0,0 +1,16 @@
"""
Binned Heatmap
--------------
This example shows how to make a heatmap from binned quantitative data.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.movies.url
alt.Chart(source).mark_rect().encode(
alt.X('IMDB_Rating:Q', bin=alt.Bin(maxbins=60)),
alt.Y('Rotten_Tomatoes_Rating:Q', bin=alt.Bin(maxbins=40)),
alt.Color('count():Q', scale=alt.Scale(scheme='greenblue'))
)

View File

@@ -0,0 +1,16 @@
"""
Binned Scatterplot
------------------
This example shows how to make a binned scatterplot.
"""
# category: scatter plots
import altair as alt
from vega_datasets import data
source = data.movies.url
alt.Chart(source).mark_circle().encode(
alt.X('IMDB_Rating:Q', bin=True),
alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),
size='count()'
)

View File

@@ -0,0 +1,18 @@
"""
Boxplot with Min/Max Whiskers
------------------------------
This example shows how to make a boxplot using US Population data from 2000.
Note that the default value of the `extent` property is 1.5,
which represents the convention of extending the whiskers
to the furthest points within 1.5 * IQR from the first and third quartile.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.population.url
alt.Chart(source).mark_boxplot(extent='min-max').encode(
x='age:O',
y='people:Q'
)

View File

@@ -0,0 +1,16 @@
"""
Bubble Plot
-----------------
This example shows how to make a bubble plot.
"""
# category: scatter plots
import altair as alt
from vega_datasets import data
source = data.cars()
alt.Chart(source).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
size='Acceleration'
)

View File

@@ -0,0 +1,29 @@
"""
Bump Chart
----------
This example shows a bump chart. The data is first grouped into six-month
intervals using pandas. The ranks are computed by Altair using a
window transform.
"""
# category: line charts
import altair as alt
from vega_datasets import data
import pandas as pd
stocks = data.stocks()
source = stocks.groupby([pd.Grouper(key="date", freq="6M"),"symbol"]).mean().reset_index()
alt.Chart(source).mark_line(point = True).encode(
x = alt.X("date:O", timeUnit="yearmonth", title="date"),
y="rank:O",
color=alt.Color("symbol:N")
).transform_window(
rank="rank()",
sort=[alt.SortField("price", order="descending")],
groupby=["date"]
).properties(
title="Bump Chart for Stock Prices",
width=600,
height=150,
)

View File

@@ -0,0 +1,44 @@
"""
Candlestick Chart
=================
A candlestick chart inspired from `Protovis <http://mbostock.github.io/protovis/ex/candlestick.html>`_.
This example shows the performance of the Chicago Board Options Exchange `Volatility Index <https://en.wikipedia.org/wiki/VIX>`_ (VIX)
in the summer of 2009. The thick bar represents the opening and closing prices,
while the thin bar shows intraday high and low prices; if the index closed higher on a given day, the bars are colored green rather than red.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.ohlc()
open_close_color = alt.condition("datum.open <= datum.close",
alt.value("#06982d"),
alt.value("#ae1325"))
base = alt.Chart(source).encode(
alt.X('date:T',
axis=alt.Axis(
format='%m/%d',
labelAngle=-45,
title='Date in 2009'
)
),
color=open_close_color
)
rule = base.mark_rule().encode(
alt.Y(
'low:Q',
title='Price',
scale=alt.Scale(zero=False),
),
alt.Y2('high:Q')
)
bar = base.mark_bar().encode(
alt.Y('open:Q'),
alt.Y2('close:Q')
)
rule + bar

View File

@@ -0,0 +1,23 @@
"""
Choropleth Map
==============
A choropleth map of unemployment rate per county in the US
"""
# category: maps
import altair as alt
from vega_datasets import data
counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url
alt.Chart(counties).mark_geoshape().encode(
color='rate:Q'
).transform_lookup(
lookup='id',
from_=alt.LookupData(source, 'id', ['rate'])
).project(
type='albersUsa'
).properties(
width=500,
height=300
)

View File

@@ -0,0 +1,28 @@
"""
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')
source = 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(source, 'id', variable_list)
).properties(
width=500,
height=300
).project(
type='albersUsa'
).repeat(
row=variable_list
).resolve_scale(
color='independent'
)

View File

@@ -0,0 +1,65 @@
"""
Atmospheric CO2 Concentration
-----------------------------
This example is a fully developed line chart that uses a window transformation.
It was inspired by `Gregor Aisch's work at datawrapper
<https://www.datawrapper.de/_/OHgEm/>`_.
"""
# category: case studies
import altair as alt
from vega_datasets import data
source = data.co2_concentration.url
base = alt.Chart(
source,
title="Carbon Dioxide in the Atmosphere"
).transform_calculate(
year="year(datum.Date)"
).transform_calculate(
decade="floor(datum.year / 10)"
).transform_calculate(
scaled_date="(datum.year % 10) + (month(datum.Date)/12)"
).transform_window(
first_date='first_value(scaled_date)',
last_date='last_value(scaled_date)',
sort=[{"field": "scaled_date", "order": "ascending"}],
groupby=['decade'],
frame=[None, None]
).transform_calculate(
end="datum.first_date === datum.scaled_date ? 'first' : datum.last_date === datum.scaled_date ? 'last' : null"
).encode(
x=alt.X(
"scaled_date:Q",
axis=alt.Axis(title="Year into Decade", tickCount=11)
),
y=alt.Y(
"CO2:Q",
title="CO2 concentration in ppm",
scale=alt.Scale(zero=False)
)
)
line = base.mark_line().encode(
color=alt.Color(
"decade:O",
scale=alt.Scale(scheme="magma"),
legend=None
)
)
text = base.encode(text="year:N")
start_year = text.transform_filter(
alt.datum.end == 'first'
).mark_text(baseline="top")
end_year = text.transform_filter(
alt.datum.end == 'last'
).mark_text(baseline="bottom")
(line + start_year + end_year).configure_text(
align="left",
dx=1,
dy=3
).properties(width=600, height=375)

View File

@@ -0,0 +1,32 @@
"""
Comet Chart Example
----------------------------
Inspired by `Zan Armstrong's comet chart <https://www.zanarmstrong.com/infovisresearch>`_
this plot uses ``mark_trail`` to visualize change of grouped data over time.
A more elaborate example and explanation of creating comet charts in Altair
is shown in `this blogpost <https://medium.com/de-dataverbinders/comet-charts-in-python-visualizing-statistical-mix-effects-and-simpsons-paradox-with-altair-6cd51fb58b7c>`_.
"""
# category: other charts
import altair as alt
import vega_datasets
(
alt.Chart(vega_datasets.data.barley.url)
.transform_pivot("year", value="yield", groupby=["variety", "site"])
.transform_fold(["1931", "1932"], as_=["year", "yield"])
.transform_calculate(calculate="datum['1932'] - datum['1931']", as_="delta")
.mark_trail()
.encode(
x=alt.X('year:O', title=None),
y=alt.Y('variety:N', title='Variety'),
size=alt.Size('yield:Q', scale=alt.Scale(range=[0, 12]), legend=alt.Legend(values=[20, 60], title='Barley Yield (bushels/acre)')),
color=alt.Color('delta:Q', scale=alt.Scale(domainMid=0), legend=alt.Legend(title='Yield Delta (%)')),
tooltip=alt.Tooltip(['year:O', 'yield:Q']),
column=alt.Column('site:N', title='Site')
)
.configure_view(stroke=None)
.configure_legend(orient='bottom', direction='horizontal')
.properties(title='Barley Yield comparison between 1932 and 1931')
)

View File

@@ -0,0 +1,18 @@
"""
Connected Scatterplot (Lines with Custom Paths)
-----------------------------------------------
This example show how the order encoding can be used to draw a custom path. The dataset tracks miles driven per capita along with gas prices annually from 1956 to 2010.
It is based on Hannah Fairfield's article 'Driving Shifts Into Reverse'. See https://archive.nytimes.com/www.nytimes.com/imagepages/2010/05/02/business/02metrics.html for the original.
"""
# category: scatter plots
import altair as alt
from vega_datasets import data
source = data.driving()
alt.Chart(source).mark_line(point=True).encode(
alt.X('miles', scale=alt.Scale(zero=False)),
alt.Y('gas', scale=alt.Scale(zero=False)),
order='year'
)

View File

@@ -0,0 +1,21 @@
"""
Cumulative Count Chart
----------------------
This example shows an area chart with cumulative count.
Adapted from https://vega.github.io/vega-lite/examples/area_cumulative_freq.html
"""
# category: area charts
import altair as alt
from vega_datasets import data
source = data.movies.url
alt.Chart(source).transform_window(
cumulative_count="count()",
sort=[{"field": "IMDB_Rating"}],
).mark_area().encode(
x="IMDB_Rating:Q",
y="cumulative_count:Q"
)

View File

@@ -0,0 +1,17 @@
"""
Cumulative Wikipedia Donations
==============================
This chart shows cumulative donations to Wikipedia over the past 10 years. Inspired by this `Reddit post <https://www.reddit.com/r/dataisbeautiful/comments/7guwd0/cumulative_wikimedia_donations_over_the_past_10/>`_ but using lines instead of areas.
"""
# category: case studies
import altair as alt
source = "https://frdata.wikimedia.org/donationdata-vs-day.csv"
alt.Chart(source).mark_line().encode(
alt.X('monthdate(date):T', title='Month', axis=alt.Axis(format='%B')),
alt.Y('max(ytdsum):Q', title='Cumulative Donations', stack=None),
alt.Color('year(date):O', legend=alt.Legend(title='Year')),
alt.Order('year(data):O')
)

View File

@@ -0,0 +1,28 @@
"""
Faceted Density Estimates
-------------------------
Density estimates of measurements for each iris flower feature
"""
# category: area charts
import altair as alt
from vega_datasets import data
source = data.iris()
alt.Chart(source).transform_fold(
['petalWidth',
'petalLength',
'sepalWidth',
'sepalLength'],
as_ = ['Measurement_type', 'value']
).transform_density(
density='value',
bandwidth=0.3,
groupby=['Measurement_type'],
extent= [0, 8]
).mark_area().encode(
alt.X('value:Q'),
alt.Y('density:Q'),
alt.Row('Measurement_type:N')
).properties(width=300, height=50)

View File

@@ -0,0 +1,35 @@
"""
Stacked Density Estimates
-------------------------
To plot a stacked graph of estimates, use a shared ``extent`` and a fixed
number of subdivision ``steps`` to ensure that the points for each area align
well. Density estimates of measurements for each iris flower feature are plot
in a stacked method. In addition, setting ``counts`` to true multiplies the
densities by the number of data points in each group, preserving proportional
differences.
"""
# category: area charts
import altair as alt
from vega_datasets import data
source = data.iris()
alt.Chart(source).transform_fold(
['petalWidth',
'petalLength',
'sepalWidth',
'sepalLength'],
as_ = ['Measurement_type', 'value']
).transform_density(
density='value',
bandwidth=0.3,
groupby=['Measurement_type'],
extent= [0, 8],
counts = True,
steps=200
).mark_area().encode(
alt.X('value:Q'),
alt.Y('density:Q', stack='zero'),
alt.Color('Measurement_type:N')
).properties(width=400, height=100)

View File

@@ -0,0 +1,367 @@
"""
Diverging Stacked Bar Chart
---------------------------
This example shows a diverging stacked bar chart for sentiments towards a set of eight questions, displayed as percentages with neutral responses straddling the 0% mark.
"""
# category: bar charts
import altair as alt
source = alt.pd.DataFrame([
{
"question": "Question 1",
"type": "Strongly disagree",
"value": 24,
"percentage": 0.7,
"percentage_start": -19.1,
"percentage_end": -18.4
},
{
"question": "Question 1",
"type": "Disagree",
"value": 294,
"percentage": 9.1,
"percentage_start": -18.4,
"percentage_end": -9.2
},
{
"question": "Question 1",
"type": "Neither agree nor disagree",
"value": 594,
"percentage": 18.5,
"percentage_start": -9.2,
"percentage_end": 9.2
},
{
"question": "Question 1",
"type": "Agree",
"value": 1927,
"percentage": 59.9,
"percentage_start": 9.2,
"percentage_end": 69.2
},
{
"question": "Question 1",
"type": "Strongly agree",
"value": 376,
"percentage": 11.7,
"percentage_start": 69.2,
"percentage_end": 80.9
},
{
"question": "Question 2",
"type": "Strongly disagree",
"value": 2,
"percentage": 18.2,
"percentage_start": -36.4,
"percentage_end": -18.2
},
{
"question": "Question 2",
"type": "Disagree",
"value": 2,
"percentage": 18.2,
"percentage_start": -18.2,
"percentage_end": 0
},
{
"question": "Question 2",
"type": "Neither agree nor disagree",
"value": 0,
"percentage": 0,
"percentage_start": 0,
"percentage_end": 0
},
{
"question": "Question 2",
"type": "Agree",
"value": 7,
"percentage": 63.6,
"percentage_start": 0,
"percentage_end": 63.6
},
{
"question": "Question 2",
"type": "Strongly agree",
"value": 11,
"percentage": 0,
"percentage_start": 63.6,
"percentage_end": 63.6
},
{
"question": "Question 3",
"type": "Strongly disagree",
"value": 2,
"percentage": 20,
"percentage_start": -30,
"percentage_end": -10
},
{
"question": "Question 3",
"type": "Disagree",
"value": 0,
"percentage": 0,
"percentage_start": -10,
"percentage_end": -10
},
{
"question": "Question 3",
"type": "Neither agree nor disagree",
"value": 2,
"percentage": 20,
"percentage_start": -10,
"percentage_end": 10
},
{
"question": "Question 3",
"type": "Agree",
"value": 4,
"percentage": 40,
"percentage_start": 10,
"percentage_end": 50
},
{
"question": "Question 3",
"type": "Strongly agree",
"value": 2,
"percentage": 20,
"percentage_start": 50,
"percentage_end": 70
},
{
"question": "Question 4",
"type": "Strongly disagree",
"value": 0,
"percentage": 0,
"percentage_start": -15.6,
"percentage_end": -15.6
},
{
"question": "Question 4",
"type": "Disagree",
"value": 2,
"percentage": 12.5,
"percentage_start": -15.6,
"percentage_end": -3.1
},
{
"question": "Question 4",
"type": "Neither agree nor disagree",
"value": 1,
"percentage": 6.3,
"percentage_start": -3.1,
"percentage_end": 3.1
},
{
"question": "Question 4",
"type": "Agree",
"value": 7,
"percentage": 43.8,
"percentage_start": 3.1,
"percentage_end": 46.9
},
{
"question": "Question 4",
"type": "Strongly agree",
"value": 6,
"percentage": 37.5,
"percentage_start": 46.9,
"percentage_end": 84.4
},
{
"question": "Question 5",
"type": "Strongly disagree",
"value": 0,
"percentage": 0,
"percentage_start": -10.4,
"percentage_end": -10.4
},
{
"question": "Question 5",
"type": "Disagree",
"value": 1,
"percentage": 4.2,
"percentage_start": -10.4,
"percentage_end": -6.3
},
{
"question": "Question 5",
"type": "Neither agree nor disagree",
"value": 3,
"percentage": 12.5,
"percentage_start": -6.3,
"percentage_end": 6.3
},
{
"question": "Question 5",
"type": "Agree",
"value": 16,
"percentage": 66.7,
"percentage_start": 6.3,
"percentage_end": 72.9
},
{
"question": "Question 5",
"type": "Strongly agree",
"value": 4,
"percentage": 16.7,
"percentage_start": 72.9,
"percentage_end": 89.6
},
{
"question": "Question 6",
"type": "Strongly disagree",
"value": 1,
"percentage": 6.3,
"percentage_start": -18.8,
"percentage_end": -12.5
},
{
"question": "Question 6",
"type": "Disagree",
"value": 1,
"percentage": 6.3,
"percentage_start": -12.5,
"percentage_end": -6.3
},
{
"question": "Question 6",
"type": "Neither agree nor disagree",
"value": 2,
"percentage": 12.5,
"percentage_start": -6.3,
"percentage_end": 6.3
},
{
"question": "Question 6",
"type": "Agree",
"value": 9,
"percentage": 56.3,
"percentage_start": 6.3,
"percentage_end": 62.5
},
{
"question": "Question 6",
"type": "Strongly agree",
"value": 3,
"percentage": 18.8,
"percentage_start": 62.5,
"percentage_end": 81.3
},
{
"question": "Question 7",
"type": "Strongly disagree",
"value": 0,
"percentage": 0,
"percentage_start": -10,
"percentage_end": -10
},
{
"question": "Question 7",
"type": "Disagree",
"value": 0,
"percentage": 0,
"percentage_start": -10,
"percentage_end": -10
},
{
"question": "Question 7",
"type": "Neither agree nor disagree",
"value": 1,
"percentage": 20,
"percentage_start": -10,
"percentage_end": 10
},
{
"question": "Question 7",
"type": "Agree",
"value": 4,
"percentage": 80,
"percentage_start": 10,
"percentage_end": 90
},
{
"question": "Question 7",
"type": "Strongly agree",
"value": 0,
"percentage": 0,
"percentage_start": 90,
"percentage_end": 90
},
{
"question": "Question 8",
"type": "Strongly disagree",
"value": 0,
"percentage": 0,
"percentage_start": 0,
"percentage_end": 0
},
{
"question": "Question 8",
"type": "Disagree",
"value": 0,
"percentage": 0,
"percentage_start": 0,
"percentage_end": 0
},
{
"question": "Question 8",
"type": "Neither agree nor disagree",
"value": 0,
"percentage": 0,
"percentage_start": 0,
"percentage_end": 0
},
{
"question": "Question 8",
"type": "Agree",
"value": 0,
"percentage": 0,
"percentage_start": 0,
"percentage_end": 0
},
{
"question": "Question 8",
"type": "Strongly agree",
"value": 2,
"percentage": 100,
"percentage_start": 0,
"percentage_end": 100
}
])
color_scale = alt.Scale(
domain=[
"Strongly disagree",
"Disagree",
"Neither agree nor disagree",
"Agree",
"Strongly agree"
],
range=["#c30d24", "#f3a583", "#cccccc", "#94c6da", "#1770ab"]
)
y_axis = alt.Axis(
title='Question',
offset=5,
ticks=False,
minExtent=60,
domain=False
)
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,
)
)

View File

@@ -0,0 +1,18 @@
"""
Donut Chart
-----------
This example shows how to make a Donut Chart using ``mark_arc``.
This is adapted from a corresponding Vega-Lite Example:
`Donut Chart <https://vega.github.io/vega-lite/examples/arc_donut.html>`_.
"""
# category: circular plots
import pandas as pd
import altair as alt
source = pd.DataFrame({"category": [1, 2, 3, 4, 5, 6], "value": [4, 6, 10, 3, 7, 8]})
alt.Chart(source).mark_arc(innerRadius=50).encode(
theta=alt.Theta(field="value", type="quantitative"),
color=alt.Color(field="category", type="nominal"),
)

View File

@@ -0,0 +1,40 @@
"""
Dot Dash Plot
=============
How to make the dot-dash plot presented in Edward Tufte's `Visual Display of Quantitative Information <https://www.edwardtufte.com/tufte/books_vdqi>`_. Based
on a JavaScript implementation by `g3o2 <https://bl.ocks.org/g3o2/bd4362574137061c243a2994ba648fb8>`_.
"""
# category: scatter plots
import altair as alt
from vega_datasets import data
source = data.cars()
# Configure the options common to all layers
brush = alt.selection(type='interval')
base = alt.Chart(source).add_selection(brush)
# Configure the points
points = base.mark_point().encode(
x=alt.X('Miles_per_Gallon', title=''),
y=alt.Y('Horsepower', title=''),
color=alt.condition(brush, 'Origin', alt.value('grey'))
)
# Configure the ticks
tick_axis = alt.Axis(labels=False, domain=False, ticks=False)
x_ticks = base.mark_tick().encode(
alt.X('Miles_per_Gallon', axis=tick_axis),
alt.Y('Origin', title='', axis=tick_axis),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
)
y_ticks = base.mark_tick().encode(
alt.X('Origin', title='', axis=tick_axis),
alt.Y('Horsepower', axis=tick_axis),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
)
# Build the chart
y_ticks | (points & x_ticks)

View File

@@ -0,0 +1,24 @@
"""
Error Bars showing Confidence Interval
======================================
This example shows how to show error bars using confidence intervals.
The confidence intervals are computed internally in vega by a non-parametric
`bootstrap of the mean <https://github.com/vega/vega-statistics/blob/master/src/bootstrapCI.js>`_.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.barley()
error_bars = alt.Chart(source).mark_errorbar(extent='ci').encode(
x=alt.X('yield:Q', scale=alt.Scale(zero=False)),
y=alt.Y('variety:N')
)
points = alt.Chart(source).mark_point(filled=True, color='black').encode(
x=alt.X('yield:Q', aggregate='mean'),
y=alt.Y('variety:N'),
)
error_bars + points

View File

@@ -0,0 +1,23 @@
"""
Error Bar with Standard Deviation
---------------------------------
This example shows how to show error bars with standard deviation using crop yields data of different
in the years of 1930s.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.barley()
error_bars = alt.Chart(source).mark_errorbar(extent='stdev').encode(
x=alt.X('yield:Q', scale=alt.Scale(zero=False)),
y=alt.Y('variety:N')
)
points = alt.Chart(source).mark_point(filled=True, color='black').encode(
x=alt.X('yield:Q', aggregate='mean'),
y=alt.Y('variety:N'),
)
error_bars + points

View File

@@ -0,0 +1,82 @@
"""
Falkensee
-----------------------
This example is a reproduction of the Falkensee plot found in the vega-lite examples.
"""
# category: case studies
import altair as alt
source = [
{"year": "1875", "population": 1309},
{"year": "1890", "population": 1558},
{"year": "1910", "population": 4512},
{"year": "1925", "population": 8180},
{"year": "1933", "population": 15915},
{"year": "1939", "population": 24824},
{"year": "1946", "population": 28275},
{"year": "1950", "population": 29189},
{"year": "1964", "population": 29881},
{"year": "1971", "population": 26007},
{"year": "1981", "population": 24029},
{"year": "1985", "population": 23340},
{"year": "1989", "population": 22307},
{"year": "1990", "population": 22087},
{"year": "1991", "population": 22139},
{"year": "1992", "population": 22105},
{"year": "1993", "population": 22242},
{"year": "1994", "population": 22801},
{"year": "1995", "population": 24273},
{"year": "1996", "population": 25640},
{"year": "1997", "population": 27393},
{"year": "1998", "population": 29505},
{"year": "1999", "population": 32124},
{"year": "2000", "population": 33791},
{"year": "2001", "population": 35297},
{"year": "2002", "population": 36179},
{"year": "2003", "population": 36829},
{"year": "2004", "population": 37493},
{"year": "2005", "population": 38376},
{"year": "2006", "population": 39008},
{"year": "2007", "population": 39366},
{"year": "2008", "population": 39821},
{"year": "2009", "population": 40179},
{"year": "2010", "population": 40511},
{"year": "2011", "population": 40465},
{"year": "2012", "population": 40905},
{"year": "2013", "population": 41258},
{"year": "2014", "population": 41777}
]
source2 = [{
"start": "1933",
"end": "1945",
"event": "Nazi Rule"
},
{
"start": "1948",
"end": "1989",
"event": "GDR (East Germany)"
}]
source = alt.pd.DataFrame(source)
source2 = alt.pd.DataFrame(source2)
line = alt.Chart(source).mark_line(color='#333').encode(
alt.X('year:T', axis=alt.Axis(format='%Y')),
y='population'
).properties(
width=500,
height=300
)
point = line.mark_point(color='#333')
rect = alt.Chart(source2).mark_rect().encode(
x='start:T',
x2='end:T',
color='event:N'
)
rect + line + point

View File

@@ -0,0 +1,19 @@
"""
Filled Step Chart
-----------------
This example shows Google's stock price over time as a step chart with its area filled in and its line emphasized.
"""
# category: line charts
import altair as alt
from vega_datasets import data
source = data.stocks()
alt.Chart(source).mark_area(
color="lightblue",
interpolate='step-after',
line=True
).encode(
x='date',
y='price'
).transform_filter(alt.datum.symbol == 'GOOG')

View File

@@ -0,0 +1,20 @@
"""
Gantt Chart
-----------------
This example shows how to make a simple Gantt chart.
"""
# category: other charts
import altair as alt
import pandas as pd
source = pd.DataFrame([
{"task": "A", "start": 1, "end": 3},
{"task": "B", "start": 3, "end": 8},
{"task": "C", "start": 8, "end": 10}
])
alt.Chart(source).mark_bar().encode(
x='start',
x2='end',
y='task'
)

View File

@@ -0,0 +1,18 @@
"""
Gapminder Bubble Plot
=====================
This example shows how to make a bubble plot showing the correlation between
health and income for 187 countries in the world (modified from an example
in Lisa Charlotte Rost's blog post `'One Chart, Twelve Charting Libraries' <http://lisacharlotterost.github.io/2016/05/17/one-chart-code/>`_.
"""
# category: case studies
import altair as alt
from vega_datasets import data
source = data.gapminder_health_income.url
alt.Chart(source).mark_circle().encode(
alt.X('income:Q', scale=alt.Scale(type='log')),
alt.Y('health:Q', scale=alt.Scale(zero=False)),
size='population:Q'
)

View File

@@ -0,0 +1,17 @@
"""
Grouped Bar Chart
-----------------
This example shows a grouped bar chart.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
x='year:O',
y='sum(yield):Q',
color='year:N',
column='site:N'
)

View File

@@ -0,0 +1,17 @@
"""
Horizontal Grouped Bar Chart
----------------------------
This example shows a horizontal grouped bar chart.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
x='sum(yield):Q',
y='year:O',
color='year:N',
row='site:N'
)

View File

@@ -0,0 +1,25 @@
"""
Grouped Bar Chart with Error Bars
---------------------------------
This example shows a grouped bar chart with error bars.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.barley()
bars = alt.Chart().mark_bar().encode(
x='year:O',
y=alt.Y('mean(yield):Q', title='Mean Yield'),
color='year:N',
)
error_bars = alt.Chart().mark_errorbar(extent='ci').encode(
x='year:O',
y='yield:Q'
)
alt.layer(bars, error_bars, data=source).facet(
column='site:N'
)

View File

@@ -0,0 +1,43 @@
"""
Hexbin Chart
-----------------
This example shows a hexbin chart.
"""
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
# Size of the hexbins
size = 15
# Count of distinct x features
xFeaturesCount = 12
# Count of distinct y features
yFeaturesCount = 7
# Name of the x field
xField = 'date'
# Name of the y field
yField = 'date'
# the shape of a hexagon
hexagon = "M0,-2.3094010768L2,-1.1547005384 2,1.1547005384 0,2.3094010768 -2,1.1547005384 -2,-1.1547005384Z"
alt.Chart(source).mark_point(size=size**2, shape=hexagon).encode(
x=alt.X('xFeaturePos:Q', axis=alt.Axis(title='Month',
grid=False, tickOpacity=0, domainOpacity=0)),
y=alt.Y('day(' + yField + '):O', axis=alt.Axis(title='Weekday',
labelPadding=20, tickOpacity=0, domainOpacity=0)),
stroke=alt.value('black'),
strokeWidth=alt.value(0.2),
fill=alt.Color('mean(temp_max):Q', scale=alt.Scale(scheme='darkblue')),
tooltip=['month(' + xField + '):O', 'day(' + yField + '):O', 'mean(temp_max):Q']
).transform_calculate(
# This field is required for the hexagonal X-Offset
xFeaturePos='(day(datum.' + yField + ') % 2) / 2 + month(datum.' + xField + ')'
).properties(
# Exact scaling factors to make the hexbins fit
width=size * xFeaturesCount * 2,
height=size * yFeaturesCount * 1.7320508076, # 1.7320508076 is approx. sin(60°)*2
).configure_view(
strokeWidth=0
)

View File

@@ -0,0 +1,35 @@
"""
Histogram with Responsive Bins
------------------------------
This shows an example of a histogram with bins that are responsive to a
selection domain. Click and drag on the bottom panel to see the bins
change on the top panel.
"""
# category: histograms
import altair as alt
from vega_datasets import data
source = data.flights_5k.url
brush = alt.selection_interval(encodings=['x'])
base = alt.Chart(source).transform_calculate(
time="hours(datum.date) + minutes(datum.date) / 60"
).mark_bar().encode(
y='count():Q'
).properties(
width=600,
height=100
)
alt.vconcat(
base.encode(
alt.X('time:Q',
bin=alt.Bin(maxbins=30, extent=brush),
scale=alt.Scale(domain=brush)
)
),
base.encode(
alt.X('time:Q', bin=alt.Bin(maxbins=30)),
).add_selection(brush)
)

View File

@@ -0,0 +1,24 @@
"""
Histogram with a Global Mean Overlay
------------------------------------
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
base = alt.Chart(source)
bar = base.mark_bar().encode(
x=alt.X('IMDB_Rating:Q', bin=True, axis=None),
y='count()'
)
rule = base.mark_rule(color='red').encode(
x='mean(IMDB_Rating):Q',
size=alt.value(5)
)
bar + rule

View File

@@ -0,0 +1,41 @@
"""
Horizon Graph
-------------
This example shows how to make a Horizon Graph with 2 layers. (See https://idl.cs.washington.edu/papers/horizon/ for more details on Horizon Graphs.)
"""
# category: area charts
import altair as alt
import pandas as pd
source = pd.DataFrame([
{"x": 1, "y": 28}, {"x": 2, "y": 55},
{"x": 3, "y": 43}, {"x": 4, "y": 91},
{"x": 5, "y": 81}, {"x": 6, "y": 53},
{"x": 7, "y": 19}, {"x": 8, "y": 87},
{"x": 9, "y": 52}, {"x": 10, "y": 48},
{"x": 11, "y": 24}, {"x": 12, "y": 49},
{"x": 13, "y": 87}, {"x": 14, "y": 66},
{"x": 15, "y": 17}, {"x": 16, "y": 27},
{"x": 17, "y": 68}, {"x": 18, "y": 16},
{"x": 19, "y": 49}, {"x": 20, "y": 15}
])
area1 = alt.Chart(source).mark_area(
clip=True,
interpolate='monotone'
).encode(
alt.X('x', scale=alt.Scale(zero=False, nice=False)),
alt.Y('y', scale=alt.Scale(domain=[0, 50]), title='y'),
opacity=alt.value(0.6)
).properties(
width=500,
height=75
)
area2 = area1.encode(
alt.Y('ny:Q', scale=alt.Scale(domain=[0, 50]))
).transform_calculate(
"ny", alt.datum.y - 50
)
area1 + area2

View File

@@ -0,0 +1,16 @@
"""
Horizontal Stacked Bar Chart
============================
This is an example of a horizontal stacked bar chart using data which contains crop yields over different regions and different years in the 1930s.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
x='sum(yield)',
y='variety',
color='site'
)

View File

@@ -0,0 +1,21 @@
"""
Image tooltip
-------------
This example shows how to render images in tooltips.
Either URLs or local file paths can be used to reference
the images.
"""
# category: other charts
import altair as alt
import pandas as pd
source = pd.DataFrame.from_records(
[{'a': 1, 'b': 1, 'image': 'https://altair-viz.github.io/_static/altair-logo-light.png'},
{'a': 2, 'b': 2, 'image': 'https://avatars.githubusercontent.com/u/11796929?s=200&v=4'}]
)
alt.Chart(source).mark_circle(size=200).encode(
x='a',
y='b',
tooltip=['image'] # Must be a list for the image to render
)

View File

@@ -0,0 +1,19 @@
"""
Interactive Rectangular Brush
=============================
This example shows how to add a simple rectangular brush to a scatter plot.
By clicking and dragging on the plot, you can highlight points within the
range.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data
source = data.cars()
brush = alt.selection(type='interval')
alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color=alt.condition(brush, 'Cylinders:O', alt.value('grey')),
).add_selection(brush)

View File

@@ -0,0 +1,49 @@
"""
Interactive Chart with Cross-Highlight
======================================
This example shows an interactive chart where selections in one portion of
the chart affect what is shown in other panels. Click on the bar chart to
see a detail of the distribution in the upper panel.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data
source = data.movies.url
pts = alt.selection(type="single", encodings=['x'])
rect = alt.Chart(data.movies.url).mark_rect().encode(
alt.X('IMDB_Rating:Q', bin=True),
alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),
alt.Color('count()',
scale=alt.Scale(scheme='greenblue'),
legend=alt.Legend(title='Total Records')
)
)
circ = rect.mark_point().encode(
alt.ColorValue('grey'),
alt.Size('count()',
legend=alt.Legend(title='Records in Selection')
)
).transform_filter(
pts
)
bar = alt.Chart(source).mark_bar().encode(
x='Major_Genre:N',
y='count()',
color=alt.condition(pts, alt.ColorValue("steelblue"), alt.ColorValue("grey"))
).properties(
width=550,
height=200
).add_selection(pts)
alt.vconcat(
rect + circ,
bar
).resolve_legend(
color="independent",
size="independent"
)

View File

@@ -0,0 +1,45 @@
"""
Interactive Crossfilter
=======================
This example shows a multi-panel view of the same data, where you can interactively
select a portion of the data in any of the panels to highlight that portion in any
of the other panels.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data
source = alt.UrlData(
data.flights_2k.url,
format={'parse': {'date': 'date'}}
)
brush = alt.selection(type='interval', encodings=['x'])
# Define the base chart, with the common parts of the
# background and highlights
base = alt.Chart().mark_bar().encode(
x=alt.X(alt.repeat('column'), type='quantitative', bin=alt.Bin(maxbins=20)),
y='count()'
).properties(
width=160,
height=130
)
# gray background with selection
background = base.encode(
color=alt.value('#ddd')
).add_selection(brush)
# blue highlights on the transformed data
highlight = base.transform_filter(brush)
# layer the two charts & repeat
alt.layer(
background,
highlight,
data=source
).transform_calculate(
"time",
"hours(datum.date)"
).repeat(column=["distance", "delay", "time"])

View File

@@ -0,0 +1,24 @@
"""
Interactive Legend
------------------
The following shows how to create a chart with an interactive legend, by
binding the selection to ``"legend"``. Such a binding only works with
``selection_single`` or ``selection_multi`` when projected over a single
field or encoding.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data
source = data.unemployment_across_industries.url
selection = alt.selection_multi(fields=['series'], bind='legend')
alt.Chart(source).mark_area().encode(
alt.X('yearmonth(date):T', axis=alt.Axis(domain=False, format='%Y', tickSize=0)),
alt.Y('sum(count):Q', stack='center', axis=None),
alt.Color('series:N', scale=alt.Scale(scheme='category20b')),
opacity=alt.condition(selection, alt.value(1), alt.value(0.2))
).add_selection(
selection
)

View File

@@ -0,0 +1,16 @@
"""
Simple Interactive Colored Scatterplot
--------------------------------------
This example shows how to make an interactive scatterplot.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data
source = data.cars()
alt.Chart(source).mark_circle().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
).interactive()

View File

@@ -0,0 +1,32 @@
"""
Interval Selection Example
==========================
This is an example of creating a stacked chart for which the domain of the
top chart can be selected by interacting with the bottom chart.
"""
# category: area charts
import altair as alt
from vega_datasets import data
source = data.sp500.url
brush = alt.selection(type='interval', encodings=['x'])
base = alt.Chart(source).mark_area().encode(
x = 'date:T',
y = 'price:Q'
).properties(
width=600,
height=200
)
upper = base.encode(
alt.X('date:T', scale=alt.Scale(domain=brush))
)
lower = base.properties(
height=60
).add_selection(brush)
upper & lower

View File

@@ -0,0 +1,27 @@
"""
Iowa's renewable energy boom
----------------------------
This example is a fully developed stacked chart using the sample dataset of Iowa's electricity sources.
"""
# category: case studies
import altair as alt
from vega_datasets import data
source = data.iowa_electricity()
alt.Chart(source, title="Iowa's renewable energy boom").mark_area().encode(
x=alt.X(
"year:T",
title="Year"
),
y=alt.Y(
"net_generation:Q",
stack="normalize",
title="Share of net generation",
axis=alt.Axis(format=".0%"),
),
color=alt.Color(
"source:N",
legend=alt.Legend(title="Electricity source"),
)
)

View File

@@ -0,0 +1,78 @@
'''
Isotype Visualization
=====================
Isotype Visualization shows the distribution of animals across UK and US.
Inspired by `Only An Ocean Between, 1943 <http://www.thomwhite.co.uk/?p=1303>`_. Population Live Stock, p.13.
This is adapted from Vega-Lite example https://vega.github.io/editor/#/examples/vega-lite/isotype_bar_chart
'''
# category: case studies
import altair as alt
import pandas as pd
source = pd.DataFrame([
{'country': 'Great Britain', 'animal': 'cattle'},
{'country': 'Great Britain', 'animal': 'cattle'},
{'country': 'Great Britain', 'animal': 'cattle'},
{'country': 'Great Britain', 'animal': 'pigs'},
{'country': 'Great Britain', 'animal': 'pigs'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'}
])
domains = ['person', 'cattle', 'pigs', 'sheep']
shape_scale = alt.Scale(
domain=domains,
range=[
'M1.7 -1.7h-0.8c0.3 -0.2 0.6 -0.5 0.6 -0.9c0 -0.6 -0.4 -1 -1 -1c-0.6 0 -1 0.4 -1 1c0 0.4 0.2 0.7 0.6 0.9h-0.8c-0.4 0 -0.7 0.3 -0.7 0.6v1.9c0 0.3 0.3 0.6 0.6 0.6h0.2c0 0 0 0.1 0 0.1v1.9c0 0.3 0.2 0.6 0.3 0.6h1.3c0.2 0 0.3 -0.3 0.3 -0.6v-1.8c0 0 0 -0.1 0 -0.1h0.2c0.3 0 0.6 -0.3 0.6 -0.6v-2c0.2 -0.3 -0.1 -0.6 -0.4 -0.6z',
'M4 -2c0 0 0.9 -0.7 1.1 -0.8c0.1 -0.1 -0.1 0.5 -0.3 0.7c-0.2 0.2 1.1 1.1 1.1 1.2c0 0.2 -0.2 0.8 -0.4 0.7c-0.1 0 -0.8 -0.3 -1.3 -0.2c-0.5 0.1 -1.3 1.6 -1.5 2c-0.3 0.4 -0.6 0.4 -0.6 0.4c0 0.1 0.3 1.7 0.4 1.8c0.1 0.1 -0.4 0.1 -0.5 0c0 0 -0.6 -1.9 -0.6 -1.9c-0.1 0 -0.3 -0.1 -0.3 -0.1c0 0.1 -0.5 1.4 -0.4 1.6c0.1 0.2 0.1 0.3 0.1 0.3c0 0 -0.4 0 -0.4 0c0 0 -0.2 -0.1 -0.1 -0.3c0 -0.2 0.3 -1.7 0.3 -1.7c0 0 -2.8 -0.9 -2.9 -0.8c-0.2 0.1 -0.4 0.6 -0.4 1c0 0.4 0.5 1.9 0.5 1.9l-0.5 0l-0.6 -2l0 -0.6c0 0 -1 0.8 -1 1c0 0.2 -0.2 1.3 -0.2 1.3c0 0 0.3 0.3 0.2 0.3c0 0 -0.5 0 -0.5 0c0 0 -0.2 -0.2 -0.1 -0.4c0 -0.1 0.2 -1.6 0.2 -1.6c0 0 0.5 -0.4 0.5 -0.5c0 -0.1 0 -2.7 -0.2 -2.7c-0.1 0 -0.4 2 -0.4 2c0 0 0 0.2 -0.2 0.5c-0.1 0.4 -0.2 1.1 -0.2 1.1c0 0 -0.2 -0.1 -0.2 -0.2c0 -0.1 -0.1 -0.7 0 -0.7c0.1 -0.1 0.3 -0.8 0.4 -1.4c0 -0.6 0.2 -1.3 0.4 -1.5c0.1 -0.2 0.6 -0.4 0.6 -0.4z',
'M1.2 -2c0 0 0.7 0 1.2 0.5c0.5 0.5 0.4 0.6 0.5 0.6c0.1 0 0.7 0 0.8 0.1c0.1 0 0.2 0.2 0.2 0.2c0 0 -0.6 0.2 -0.6 0.3c0 0.1 0.4 0.9 0.6 0.9c0.1 0 0.6 0 0.6 0.1c0 0.1 0 0.7 -0.1 0.7c-0.1 0 -1.2 0.4 -1.5 0.5c-0.3 0.1 -1.1 0.5 -1.1 0.7c-0.1 0.2 0.4 1.2 0.4 1.2l-0.4 0c0 0 -0.4 -0.8 -0.4 -0.9c0 -0.1 -0.1 -0.3 -0.1 -0.3l-0.2 0l-0.5 1.3l-0.4 0c0 0 -0.1 -0.4 0 -0.6c0.1 -0.1 0.3 -0.6 0.3 -0.7c0 0 -0.8 0 -1.5 -0.1c-0.7 -0.1 -1.2 -0.3 -1.2 -0.2c0 0.1 -0.4 0.6 -0.5 0.6c0 0 0.3 0.9 0.3 0.9l-0.4 0c0 0 -0.4 -0.5 -0.4 -0.6c0 -0.1 -0.2 -0.6 -0.2 -0.5c0 0 -0.4 0.4 -0.6 0.4c-0.2 0.1 -0.4 0.1 -0.4 0.1c0 0 -0.1 0.6 -0.1 0.6l-0.5 0l0 -1c0 0 0.5 -0.4 0.5 -0.5c0 -0.1 -0.7 -1.2 -0.6 -1.4c0.1 -0.1 0.1 -1.1 0.1 -1.1c0 0 -0.2 0.1 -0.2 0.1c0 0 0 0.9 0 1c0 0.1 -0.2 0.3 -0.3 0.3c-0.1 0 0 -0.5 0 -0.9c0 -0.4 0 -0.4 0.2 -0.6c0.2 -0.2 0.6 -0.3 0.8 -0.8c0.3 -0.5 1 -0.6 1 -0.6z',
'M-4.1 -0.5c0.2 0 0.2 0.2 0.5 0.2c0.3 0 0.3 -0.2 0.5 -0.2c0.2 0 0.2 0.2 0.4 0.2c0.2 0 0.2 -0.2 0.5 -0.2c0.2 0 0.2 0.2 0.4 0.2c0.2 0 0.2 -0.2 0.4 -0.2c0.1 0 0.2 0.2 0.4 0.1c0.2 0 0.2 -0.2 0.4 -0.3c0.1 0 0.1 -0.1 0.4 0c0.3 0 0.3 -0.4 0.6 -0.4c0.3 0 0.6 -0.3 0.7 -0.2c0.1 0.1 1.4 1 1.3 1.4c-0.1 0.4 -0.3 0.3 -0.4 0.3c-0.1 0 -0.5 -0.4 -0.7 -0.2c-0.3 0.2 -0.1 0.4 -0.2 0.6c-0.1 0.1 -0.2 0.2 -0.3 0.4c0 0.2 0.1 0.3 0 0.5c-0.1 0.2 -0.3 0.2 -0.3 0.5c0 0.3 -0.2 0.3 -0.3 0.6c-0.1 0.2 0 0.3 -0.1 0.5c-0.1 0.2 -0.1 0.2 -0.2 0.3c-0.1 0.1 0.3 1.1 0.3 1.1l-0.3 0c0 0 -0.3 -0.9 -0.3 -1c0 -0.1 -0.1 -0.2 -0.3 -0.2c-0.2 0 -0.3 0.1 -0.4 0.4c0 0.3 -0.2 0.8 -0.2 0.8l-0.3 0l0.3 -1c0 0 0.1 -0.6 -0.2 -0.5c-0.3 0.1 -0.2 -0.1 -0.4 -0.1c-0.2 -0.1 -0.3 0.1 -0.4 0c-0.2 -0.1 -0.3 0.1 -0.5 0c-0.2 -0.1 -0.1 0 -0.3 0.3c-0.2 0.3 -0.4 0.3 -0.4 0.3l0.2 1.1l-0.3 0l-0.2 -1.1c0 0 -0.4 -0.6 -0.5 -0.4c-0.1 0.3 -0.1 0.4 -0.3 0.4c-0.1 -0.1 -0.2 1.1 -0.2 1.1l-0.3 0l0.2 -1.1c0 0 -0.3 -0.1 -0.3 -0.5c0 -0.3 0.1 -0.5 0.1 -0.7c0.1 -0.2 -0.1 -1 -0.2 -1.1c-0.1 -0.2 -0.2 -0.8 -0.2 -0.8c0 0 -0.1 -0.5 0.4 -0.8z'
]
)
color_scale = alt.Scale(
domain=domains,
range=['rgb(162,160,152)', 'rgb(194,81,64)', 'rgb(93,93,93)', 'rgb(91,131,149)']
)
alt.Chart(source).mark_point(filled=True, opacity=1, size=100).encode(
alt.X('x:O', axis=None),
alt.Y('animal:O', axis=None),
alt.Row('country:N', header=alt.Header(title='')),
alt.Shape('animal:N', legend=None, scale=shape_scale),
alt.Color('animal:N', legend=None, scale=color_scale),
).transform_window(
x='rank()',
groupby=['country', 'animal']
).properties(width=550, height=140)

View File

@@ -0,0 +1,63 @@
'''
Isotype Visualization with Emoji
================================
Isotype Visualization shows the distribution of animals across UK and US, using unicode emoji
marks rather than custom SVG paths (see https://altair-viz.github.io/gallery/isotype.html).
This is adapted from Vega-Lite example https://vega.github.io/vega-lite/examples/isotype_bar_chart_emoji.html.
'''
# category: case studies
import altair as alt
import pandas as pd
source = pd.DataFrame([
{'country': 'Great Britain', 'animal': 'cattle'},
{'country': 'Great Britain', 'animal': 'cattle'},
{'country': 'Great Britain', 'animal': 'cattle'},
{'country': 'Great Britain', 'animal': 'pigs'},
{'country': 'Great Britain', 'animal': 'pigs'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'Great Britain', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'cattle'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'}
])
alt.Chart(source).mark_text(size=45, baseline='middle').encode(
alt.X('x:O', axis=None),
alt.Y('animal:O', axis=None),
alt.Row('country:N', header=alt.Header(title='')),
alt.Text('emoji:N')
).transform_calculate(
emoji="{'cattle': '🐄', 'pigs': '🐖', 'sheep': '🐏'}[datum.animal]"
).transform_window(
x='rank()',
groupby=['country', 'animal']
).properties(width=550, height=140)

View File

@@ -0,0 +1,38 @@
"""
Isotype Grid
------------
This example is a grid of isotype figures.
"""
# category: other charts
import altair as alt
import pandas as pd
data = pd.DataFrame([dict(id=i) for i in range(1, 101)])
person = (
"M1.7 -1.7h-0.8c0.3 -0.2 0.6 -0.5 0.6 -0.9c0 -0.6 "
"-0.4 -1 -1 -1c-0.6 0 -1 0.4 -1 1c0 0.4 0.2 0.7 0.6 "
"0.9h-0.8c-0.4 0 -0.7 0.3 -0.7 0.6v1.9c0 0.3 0.3 0.6 "
"0.6 0.6h0.2c0 0 0 0.1 0 0.1v1.9c0 0.3 0.2 0.6 0.3 "
"0.6h1.3c0.2 0 0.3 -0.3 0.3 -0.6v-1.8c0 0 0 -0.1 0 "
"-0.1h0.2c0.3 0 0.6 -0.3 0.6 -0.6v-2c0.2 -0.3 -0.1 "
"-0.6 -0.4 -0.6z"
)
alt.Chart(data).transform_calculate(
row="ceil(datum.id/10)"
).transform_calculate(
col="datum.id - datum.row*10"
).mark_point(
filled=True,
size=50
).encode(
x=alt.X("col:O", axis=None),
y=alt.Y("row:O", axis=None),
shape=alt.ShapeValue(person)
).properties(
width=400,
height=400
).configure_view(
strokeWidth=0
)

View File

@@ -0,0 +1,28 @@
"""
Line Chart with Layered Aggregates
----------------------------------
This example shows how to make a multi-series line chart of the daily closing
stock prices for AAPL, AMZN, GOOG, IBM, and MSFT between 2000 and 2010, along
with a layered rule showing the average values.
"""
# category: line charts
import altair as alt
from vega_datasets import data
source = data.stocks()
base = alt.Chart(source).properties(width=550)
line = base.mark_line().encode(
x='date',
y='price',
color='symbol'
)
rule = base.mark_rule().encode(
y='average(price)',
color='symbol',
size=alt.value(2)
)
line + rule

View File

@@ -0,0 +1,16 @@
"""
Layered Area Chart
------------------
This example shows a layered area chart.
"""
# category: area charts
import altair as alt
from vega_datasets import data
source = data.iowa_electricity()
alt.Chart(source).mark_area(opacity=0.3).encode(
x="year:T",
y=alt.Y("net_generation:Q", stack=None),
color="source:N"
)

View File

@@ -0,0 +1,16 @@
"""
Layered Bar Chart
-----------------
This example shows a segmented bar chart that is layered rather than stacked.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.iowa_electricity()
alt.Chart(source).mark_bar(opacity=0.7).encode(
x='year:O',
y=alt.Y('net_generation:Q', stack=None),
color="source",
)

View File

@@ -0,0 +1,32 @@
"""
Bar and Tick Chart
------------------
How to layer a tick chart on top of a bar chart.
"""
# category: bar charts
import altair as alt
import pandas as pd
source = pd.DataFrame({
'project': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
'score': [25, 57, 23, 19, 8, 47, 8],
'goal': [25, 47, 30, 27, 38, 19, 4]
})
bar = alt.Chart(source).mark_bar().encode(
x='project',
y='score'
).properties(
width=alt.Step(40) # controls width of bar.
)
tick = alt.Chart(source).mark_tick(
color='red',
thickness=2,
size=40 * 0.9, # controls width of tick.
).encode(
x='project',
y='goal'
)
bar + tick

View File

@@ -0,0 +1,30 @@
"""
Layered chart with Dual-Axis
----------------------------
This example shows how to create a second independent y axis.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
base = alt.Chart(source).encode(
alt.X('month(date):T', axis=alt.Axis(title=None))
)
area = base.mark_area(opacity=0.3, color='#57A44C').encode(
alt.Y('average(temp_max)',
axis=alt.Axis(title='Avg. Temperature (°C)', titleColor='#57A44C')),
alt.Y2('average(temp_min)')
)
line = base.mark_line(stroke='#5276A7', interpolate='monotone').encode(
alt.Y('average(precipitation)',
axis=alt.Axis(title='Precipitation (inches)', titleColor='#5276A7'))
)
alt.layer(area, line).resolve_scale(
y = 'independent'
)

View File

@@ -0,0 +1,41 @@
"""
Text over a Heatmap
-------------------
An example of a layered chart of text over a heatmap using the cars dataset.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.cars()
# Configure common options
base = alt.Chart(source).transform_aggregate(
num_cars='count()',
groupby=['Origin', 'Cylinders']
).encode(
alt.X('Cylinders:O', scale=alt.Scale(paddingInner=0)),
alt.Y('Origin:O', scale=alt.Scale(paddingInner=0)),
)
# Configure heatmap
heatmap = base.mark_rect().encode(
color=alt.Color('num_cars:Q',
scale=alt.Scale(scheme='viridis'),
legend=alt.Legend(direction='horizontal')
)
)
# Configure text
text = base.mark_text(baseline='middle').encode(
text='num_cars:Q',
color=alt.condition(
alt.datum.num_cars > 100,
alt.value('black'),
alt.value('white')
)
)
# Draw the chart
heatmap + text

View File

@@ -0,0 +1,29 @@
"""
Layered Histogram
=================
This example shows how to use opacity to make a layered histogram in Altair.
"""
# category: histograms
import pandas as pd
import altair as alt
import numpy as np
np.random.seed(42)
# Generating Data
source = pd.DataFrame({
'Trial A': np.random.normal(0, 0.8, 1000),
'Trial B': np.random.normal(-2, 1, 1000),
'Trial C': np.random.normal(3, 2, 1000)
})
alt.Chart(source).transform_fold(
['Trial A', 'Trial B', 'Trial C'],
as_=['Experiment', 'Measurement']
).mark_bar(
opacity=0.3,
binSpacing=0
).encode(
alt.X('Measurement:Q', bin=alt.Bin(maxbins=100)),
alt.Y('count()', stack=None),
alt.Color('Experiment:N')
)

View File

@@ -0,0 +1,21 @@
"""
Line Chart with datum for color
-------------------------------
An example of using ``datum`` and ``repeat`` to color a multi-series line chart.
This is adapted from this corresponding Vega-Lite Example:
`Repeat and Layer to Show Different Movie Measures <https://vega.github.io/vega-lite/examples/repeat_layer.html>`_.
"""
# category: line charts
import altair as alt
from vega_datasets import data
source = data.movies()
alt.Chart(source).mark_line().encode(
x=alt.X("IMDB_Rating", bin=True),
y=alt.Y(
alt.repeat("layer"), aggregate="mean", title="Mean of US and Worldwide Gross"
),
color=alt.datum(alt.repeat("layer")),
).repeat(layer=["US_Gross", "Worldwide_Gross"])

View File

@@ -0,0 +1,24 @@
"""
Line Chart with Cumulative Sum
------------------------------
This chart creates a simple line chart from the cumulative sum of a fields.
"""
# category: line charts
import altair as alt
from vega_datasets import data
source = data.wheat()
alt.Chart(source).mark_line().transform_window(
# Sort the data chronologically
sort=[{'field': 'year'}],
# Include all previous records before the current record and none after
# (This is the default value so you could skip it and it would still work.)
frame=[None, 0],
# What to add up as you go
cumulative_wheat='sum(wheat)'
).encode(
x='year:O',
# Plot the calculated field created by the transformation
y='cumulative_wheat:Q'
).properties(width=600)

View File

@@ -0,0 +1,32 @@
"""
Line Chart with datum
---------------------------------
An example of using ``datum`` to highlight certain values, including a ``DateTime`` value.
This is adapted from two corresponding Vega-Lite Examples:
`Highlight a Specific Value <https://vega.github.io/vega-lite/docs/datum.html#highlight-a-specific-data-value>`_.
"""
# category: line charts
import altair as alt
from vega_datasets import data
source = data.stocks()
lines = (
alt.Chart(source)
.mark_line()
.encode(x="date", y="price", color="symbol")
)
xrule = (
alt.Chart()
.mark_rule(color="cyan", strokeWidth=2)
.encode(x=alt.datum(alt.DateTime(year=2006, month="November")))
)
yrule = (
alt.Chart().mark_rule(strokeDash=[12, 6], size=2).encode(y=alt.datum(350))
)
lines + yrule + xrule

View File

@@ -0,0 +1,21 @@
"""
Line Chart with Sequence Generator
----------------------------------
This examples shows how to create multiple lines using the sequence generator.
"""
# category: line charts
import altair as alt
source = alt.sequence(start=0, stop=12.7, step=0.1, as_='x')
alt.Chart(source).mark_line().transform_calculate(
sin='sin(datum.x)',
cos='cos(datum.x)'
).transform_fold(
['sin', 'cos']
).encode(
x='x:Q',
y='value:Q',
color='key:N'
)

View File

@@ -0,0 +1,24 @@
"""
Line Chart with Points
----------------------
This chart shows a simple line chart with points marking each value. Use
``point=True`` for points with default appearance or customize it with
``OverlayMarkDef()``.
"""
# category: line charts
import altair as alt
import numpy as np
import pandas as pd
x = np.arange(100)
source = pd.DataFrame({
'x': x,
'f(x)': np.sin(x / 5)
})
alt.Chart(source).mark_line(
point=alt.OverlayMarkDef(color="red")
).encode(
x='x',
y='f(x)'
)

View File

@@ -0,0 +1,18 @@
"""
Line Chart with Percent axis
----------------------------
This example shows how to format the tick labels of the y-axis of a chart as percentages.
"""
# category: line charts
import altair as alt
from vega_datasets import data
source = data.jobs.url
alt.Chart(source).mark_line().encode(
alt.X('year:O'),
alt.Y('perc:Q', axis=alt.Axis(format='%')),
color='sex:N'
).transform_filter(
alt.datum.job == 'Welder'
)

View File

@@ -0,0 +1,22 @@
"""
Line Chart with Confidence Interval Band
----------------------------------------
How to make a line chart with a bootstrapped 95% confidence interval band.
"""
# category: line charts
import altair as alt
from vega_datasets import data
source = data.cars()
line = alt.Chart(source).mark_line().encode(
x='Year',
y='mean(Miles_per_Gallon)'
)
band = alt.Chart(source).mark_errorband(extent='ci').encode(
x='Year',
y=alt.Y('Miles_per_Gallon', title='Miles/Gallon'),
)
band + line

View File

@@ -0,0 +1,18 @@
"""
Line Chart with Logarithmic Scale
---------------------------------
How to make a line chart on a `Logarithmic scale <https://en.wikipedia.org/wiki/Logarithmic_scale>`_.
"""
# category: line charts
import altair as alt
from vega_datasets import data
source = data.population()
alt.Chart(source).mark_line().encode(
x='year:O',
y=alt.Y(
'sum(people)',
scale=alt.Scale(type="log") # Here the scale is applied
)
)

View File

@@ -0,0 +1,59 @@
"""
London Tube Lines
=================
This example shows the London tube lines against the background of the
borough boundaries. It is based on the vega-lite example at
https://vega.github.io/vega-lite/examples/geo_layer_line_london.html.
"""
# category: case studies
import altair as alt
from vega_datasets import data
boroughs = alt.topo_feature(data.londonBoroughs.url, 'boroughs')
tubelines = alt.topo_feature(data.londonTubeLines.url, 'line')
centroids = data.londonCentroids.url
background = alt.Chart(boroughs).mark_geoshape(
stroke='white',
strokeWidth=2
).encode(
color=alt.value('#eee'),
).properties(
width=700,
height=500
)
labels = alt.Chart(centroids).mark_text().encode(
longitude='cx:Q',
latitude='cy:Q',
text='bLabel:N',
size=alt.value(8),
opacity=alt.value(0.6)
).transform_calculate(
"bLabel", "indexof (datum.name,' ') > 0 ? substring(datum.name,0,indexof(datum.name, ' ')) : datum.name"
)
line_scale = alt.Scale(domain=["Bakerloo", "Central", "Circle", "District", "DLR",
"Hammersmith & City", "Jubilee", "Metropolitan", "Northern",
"Piccadilly", "Victoria", "Waterloo & City"],
range=["rgb(137,78,36)", "rgb(220,36,30)", "rgb(255,206,0)",
"rgb(1,114,41)", "rgb(0,175,173)", "rgb(215,153,175)",
"rgb(106,114,120)", "rgb(114,17,84)", "rgb(0,0,0)",
"rgb(0,24,168)", "rgb(0,160,226)", "rgb(106,187,170)"])
lines = alt.Chart(tubelines).mark_geoshape(
filled=False,
strokeWidth=2
).encode(
alt.Color(
'id:N',
legend=alt.Legend(
title=None,
orient='bottom-right',
offset=0
),
scale=line_scale
)
)
background + labels + lines

View File

@@ -0,0 +1,18 @@
"""
Multi Series Line Chart
-----------------------
This example shows how to make a multi series line chart of the daily closing stock prices for AAPL, AMZN, GOOG, IBM, and MSFT between 2000 and 2010.
"""
# category: line charts
import altair as alt
from vega_datasets import data
source = data.stocks()
alt.Chart(source).mark_line().encode(
x='date',
y='price',
color='symbol',
strokeDash='symbol',
)

View File

@@ -0,0 +1,17 @@
"""
Multifeature Scatter Plot
=========================
This example shows how to make a scatter plot with multiple feature encodings.
"""
# category: scatter plots
import altair as alt
from vega_datasets import data
source = data.iris()
alt.Chart(source).mark_circle().encode(
alt.X('sepalLength', scale=alt.Scale(zero=False)),
alt.Y('sepalWidth', scale=alt.Scale(zero=False, padding=1)),
color='species',
size='petalWidth'
)

View File

@@ -0,0 +1,36 @@
"""
Multi-Line Highlight
====================
This multi-line chart uses an invisible Voronoi tessellation to handle mouseover to
identify the nearest point and then highlight the line on which the point falls.
It is adapted from the Vega-Lite example found at
https://bl.ocks.org/amitkaps/fe4238e716db53930b2f1a70d3401701
"""
# category: interactive charts
import altair as alt
from vega_datasets import data
source = data.stocks()
highlight = alt.selection(type='single', on='mouseover',
fields=['symbol'], nearest=True)
base = alt.Chart(source).encode(
x='date:T',
y='price:Q',
color='symbol:N'
)
points = base.mark_circle().encode(
opacity=alt.value(0)
).add_selection(
highlight
).properties(
width=600
)
lines = base.mark_line().encode(
size=alt.condition(~highlight, alt.value(1), alt.value(3))
)
points + lines

View File

@@ -0,0 +1,67 @@
"""
Multi-Line Tooltip
==================
This example shows how you can use selections and layers to create a
tooltip-like behavior tied to the x position of the cursor.
If you are looking for more standard tooltips, it is recommended to use the
tooltip encoding channel as shown in the
`Scatter Plot With Tooltips <https://altair-viz.github.io/gallery/scatter_tooltips.html>`_
example.
The following example employs a little trick to isolate the x-position of the
cursor: we add some transparent points with only an x encoding (no y encoding)
and tie a *nearest* selection to these, tied to the "x" field.
"""
# category: interactive charts
import altair as alt
import pandas as pd
import numpy as np
np.random.seed(42)
source = pd.DataFrame(np.cumsum(np.random.randn(100, 3), 0).round(2),
columns=['A', 'B', 'C'], index=pd.RangeIndex(100, name='x'))
source = source.reset_index().melt('x', var_name='category', value_name='y')
# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
fields=['x'], empty='none')
# The basic line
line = alt.Chart(source).mark_line(interpolate='basis').encode(
x='x:Q',
y='y:Q',
color='category:N'
)
# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(source).mark_point().encode(
x='x:Q',
opacity=alt.value(0),
).add_selection(
nearest
)
# Draw points on the line, and highlight based on selection
points = line.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 = line.mark_text(align='left', dx=5, dy=-5).encode(
text=alt.condition(nearest, 'y:Q', alt.value(' '))
)
# Draw a rule at the location of the selection
rules = alt.Chart(source).mark_rule(color='gray').encode(
x='x:Q',
).transform_filter(
nearest
)
# Put the five layers into a chart and bind the data
alt.layer(
line, selectors, points, rules, text
).properties(
width=600, height=300
)

View File

@@ -0,0 +1,90 @@
"""
Multiple Interactions
=====================
This example shows how multiple user inputs can be layered onto a chart. The four inputs have functionality as follows:
* Dropdown: Filters the movies by genre
* Radio Buttons: Highlights certain films by Worldwide Gross
* Mouse Drag and Scroll: Zooms the x and y scales to allow for panning.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data
movies = alt.UrlData(
data.movies.url,
format=alt.DataFormat(parse={"Release_Date":"date"})
)
ratings = ['G', 'NC-17', 'PG', 'PG-13', 'R']
genres = ['Action', 'Adventure', 'Black Comedy', 'Comedy',
'Concert/Performance', 'Documentary', 'Drama', 'Horror', 'Musical',
'Romantic Comedy', 'Thriller/Suspense', 'Western']
base = alt.Chart(movies, width=200, height=200).mark_point(filled=True).transform_calculate(
Rounded_IMDB_Rating = "floor(datum.IMDB_Rating)",
Hundred_Million_Production = "datum.Production_Budget > 100000000.0 ? 100 : 10",
Release_Year = "year(datum.Release_Date)"
).transform_filter(
alt.datum.IMDB_Rating > 0
).transform_filter(
alt.FieldOneOfPredicate(field='MPAA_Rating', oneOf=ratings)
).encode(
x=alt.X('Worldwide_Gross:Q', scale=alt.Scale(domain=(100000,10**9), clamp=True)),
y='IMDB_Rating:Q',
tooltip="Title:N"
)
# A slider filter
year_slider = alt.binding_range(min=1969, max=2018, step=1)
slider_selection = alt.selection_single(bind=year_slider, fields=['Release_Year'], name="Release Year_")
filter_year = base.add_selection(
slider_selection
).transform_filter(
slider_selection
).properties(title="Slider Filtering")
# A dropdown filter
genre_dropdown = alt.binding_select(options=genres)
genre_select = alt.selection_single(fields=['Major_Genre'], bind=genre_dropdown, name="Genre")
filter_genres = base.add_selection(
genre_select
).transform_filter(
genre_select
).properties(title="Dropdown Filtering")
#color changing marks
rating_radio = alt.binding_radio(options=ratings)
rating_select = alt.selection_single(fields=['MPAA_Rating'], bind=rating_radio, name="Rating")
rating_color_condition = alt.condition(rating_select,
alt.Color('MPAA_Rating:N', legend=None),
alt.value('lightgray'))
highlight_ratings = base.add_selection(
rating_select
).encode(
color=rating_color_condition
).properties(title="Radio Button Highlighting")
# Boolean selection for format changes
input_checkbox = alt.binding_checkbox()
checkbox_selection = alt.selection_single(bind=input_checkbox, name="Big Budget Films")
size_checkbox_condition = alt.condition(checkbox_selection,
alt.SizeValue(25),
alt.Size('Hundred_Million_Production:Q')
)
budget_sizing = base.add_selection(
checkbox_selection
).encode(
size=size_checkbox_condition
).properties(title="Checkbox Formatting")
( filter_year | filter_genres) & (highlight_ratings | budget_sizing )

View File

@@ -0,0 +1,17 @@
"""
Multiple Marks
==============
This example demonstrates creating a single chart with multiple markers
representing the same data.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.stocks()
alt.Chart(source).mark_line(point=True).encode(
x='date:T',
y='price:Q',
color='symbol:N'
)

View File

@@ -0,0 +1,29 @@
"""
Natural Disasters
-----------------
This example shows a visualization of global deaths from natural disasters.
"""
# category: case studies
import altair as alt
from vega_datasets import data
source = data.disasters.url
alt.Chart(source).mark_circle(
opacity=0.8,
stroke='black',
strokeWidth=1
).encode(
alt.X('Year:O', axis=alt.Axis(labelAngle=0)),
alt.Y('Entity:N'),
alt.Size('Deaths:Q',
scale=alt.Scale(range=[0, 4000]),
legend=alt.Legend(title='Annual Global Deaths')
),
alt.Color('Entity:N', legend=None)
).properties(
width=450,
height=320
).transform_filter(
alt.datum.Entity != 'All natural disasters'
)

View File

@@ -0,0 +1,16 @@
"""
Normalized Stacked Area Chart
-----------------------------
This example shows how to make a normalized stacked area chart.
"""
# category: area charts
import altair as alt
from vega_datasets import data
source = data.iowa_electricity()
alt.Chart(source).mark_area().encode(
x="year:T",
y=alt.Y("net_generation:Q", stack="normalize"),
color="source:N"
)

View File

@@ -0,0 +1,16 @@
"""
Normalized Stacked Bar Chart
----------------------------
This is an example of a normalized stacked bar chart using data which contains crop yields over different regions and different years in the 1930s.
"""
# category: bar charts
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
x=alt.X('sum(yield)', stack="normalize"),
y='variety',
color='site'
)

View File

@@ -0,0 +1,41 @@
"""
Normalized Parallel Coordinates Example
---------------------------------------
A `Parallel Coordinates <https://en.wikipedia.org/wiki/Parallel_coordinates>`_
chart is a chart that lets you visualize the individual data points by drawing
a single line for each of them.
Such a chart can be created in Altair by first transforming the data into a
suitable representation.
This example shows a modified parallel coordinates chart with the Iris dataset,
where the y-axis shows the value after min-max rather than the raw value. It's a
simplified Altair version of `the VegaLite version <https://vega.github.io/vega-lite/examples/parallel_coordinate.html>`_
"""
# category: other charts
import altair as alt
from vega_datasets import data
from altair import datum
source = data.iris()
alt.Chart(source).transform_window(
index='count()'
).transform_fold(
['petalLength', 'petalWidth', 'sepalLength', 'sepalWidth']
).transform_joinaggregate(
min='min(value)',
max='max(value)',
groupby=['key']
).transform_calculate(
minmax_value=(datum.value-datum.min)/(datum.max-datum.min),
mid=(datum.min+datum.max)/2
).mark_line().encode(
x='key:N',
y='minmax_value:Q',
color='species:N',
detail='index:N',
opacity=alt.value(0.5)
).properties(width=500)

View File

@@ -0,0 +1,25 @@
"""
One Dot Per Zipcode
-----------------------
This example shows a geographical plot with one dot per zipcode.
"""
# 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.zipcodes.url
alt.Chart(source).transform_calculate(
"leading digit", alt.expr.substring(alt.datum.zip_code, 0, 1)
).mark_circle(size=3).encode(
longitude='longitude:Q',
latitude='latitude:Q',
color='leading digit:N',
tooltip='zip_code:N'
).project(
type='albersUsa'
).properties(
width=650,
height=400
)

View File

@@ -0,0 +1,17 @@
"""
Pacman Chart
------------
Chart made using ``mark_arc`` and constant values.
This could also be made using
``alt.Chart(source).mark_arc(color = "gold", theta = (5/8)*np.pi, theta2 = (19/8)*np.pi,radius=100)``.
"""
# category: circular plots
import numpy as np
import altair as alt
alt.Chart().mark_arc(color="gold").encode(
theta=alt.datum((5 / 8) * np.pi, scale=None),
theta2=alt.datum((19 / 8) * np.pi),
radius=alt.datum(100, scale=None),
)

View File

@@ -0,0 +1,28 @@
"""
Parallel Coordinates Example
----------------------------
A `Parallel Coordinates <https://en.wikipedia.org/wiki/Parallel_coordinates>`_
chart is a chart that lets you visualize the individual data points by drawing
a single line for each of them.
Such a chart can be created in Altair by first transforming the data into a
suitable representation.
This example shows a parallel coordinates chart with the Iris dataset.
"""
# category: other charts
import altair as alt
from vega_datasets import data
source = data.iris()
alt.Chart(source).transform_window(
index='count()'
).transform_fold(
['petalLength', 'petalWidth', 'sepalLength', 'sepalWidth']
).mark_line().encode(
x='key:N',
y='value:Q',
color='species:N',
detail='index:N',
opacity=alt.value(0.5)
).properties(width=500)

View File

@@ -0,0 +1,21 @@
"""
Calculating Percentage of Total
-------------------------------
This chart demonstrates how to use a joinaggregate transform to display
data values as a percentage of total.
"""
# category: bar charts
import altair as alt
import pandas as pd
source = pd.DataFrame({'Activity': ['Sleeping', 'Eating', 'TV', 'Work', 'Exercise'],
'Time': [8, 2, 4, 8, 2]})
alt.Chart(source).transform_joinaggregate(
TotalTime='sum(Time)',
).transform_calculate(
PercentOfTotal="datum.Time / datum.TotalTime"
).mark_bar().encode(
alt.X('PercentOfTotal:Q', axis=alt.Axis(format='.0%')),
y='Activity:N'
)

View File

@@ -0,0 +1,18 @@
"""
Pie Chart
---------
This example shows how to make a Pie Chart using ``mark_arc``.
This is adapted from a corresponding Vega-Lite Example:
`Pie Chart <https://vega.github.io/vega-lite/examples/arc_pie.html>`_.
"""
# category: circular plots
import pandas as pd
import altair as alt
source = pd.DataFrame({"category": [1, 2, 3, 4, 5, 6], "value": [4, 6, 10, 3, 7, 8]})
alt.Chart(source).mark_arc().encode(
theta=alt.Theta(field="value", type="quantitative"),
color=alt.Color(field="category", type="nominal"),
)

View File

@@ -0,0 +1,24 @@
"""
Pie Chart with Labels
---------------------
This example shows how to layer text over arc marks (``mark_arc``) to label pie charts.
This is adapted from a corresponding Vega-Lite Example:
`Pie Chart with Labels <https://vega.github.io/vega-lite/examples/layer_arc_label.html>`_.
"""
# category: circular plots
import pandas as pd
import altair as alt
source = pd.DataFrame(
{"category": ["a", "b", "c", "d", "e", "f"], "value": [4, 6, 10, 3, 7, 8]}
)
base = alt.Chart(source).encode(
theta=alt.Theta("value:Q", stack=True), color=alt.Color("category:N", legend=None)
)
pie = base.mark_arc(outerRadius=120)
text = base.mark_text(radius=140, size=20).encode(text="category:N")
pie + text

View File

@@ -0,0 +1,36 @@
"""
Polynomial Fit Plot with Regression Transform
=============================================
This example shows how to overlay data with multiple fitted polynomials using
the regression transform.
"""
# category: scatter plots
import numpy as np
import pandas as pd
import altair as alt
# Generate some random data
rng = np.random.RandomState(1)
x = rng.rand(40) ** 2
y = 10 - 1.0 / (x + 0.1) + rng.randn(40)
source = pd.DataFrame({"x": x, "y": y})
# Define the degree of the polynomial fits
degree_list = [1, 3, 5]
base = alt.Chart(source).mark_circle(color="black").encode(
alt.X("x"), alt.Y("y")
)
polynomial_fit = [
base.transform_regression(
"x", "y", method="poly", order=order, as_=["x", str(order)]
)
.mark_line()
.transform_fold([str(order)], as_=["degree", "y"])
.encode(alt.Color("degree:N"))
for order in degree_list
]
alt.layer(base, *polynomial_fit)

View File

@@ -0,0 +1,19 @@
"""
Pyramid Pie Chart
-----------------
Altair reproduction of http://robslink.com/SAS/democd91/pyramid_pie.htm
"""
import altair as alt
import pandas as pd
category = ['Sky', 'Shady side of a pyramid', 'Sunny side of a pyramid']
color = ["#416D9D", "#674028", "#DEAC58"]
df = pd.DataFrame({'category': category, 'value': [75, 10, 15]})
alt.Chart(df).mark_arc(outerRadius=80).encode(
alt.Theta('value:Q', scale=alt.Scale(range=[2.356, 8.639])),
alt.Color('category:N',
scale=alt.Scale(domain=category, range=color),
legend=alt.Legend(title=None, orient='none', legendX=160, legendY=50)),
order='value:Q'
).properties(width=150, height=150).configure_view(strokeOpacity=0)

View File

@@ -0,0 +1,25 @@
"""
Radial Chart
------------
This radial plot uses both angular and radial extent to convey multiple dimensions of data.
This is adapted from a corresponding Vega-Lite Example:
`Radial Plot <https://vega.github.io/vega-lite/examples/arc_radial.html>`_.
"""
# category: circular plots
import pandas as pd
import altair as alt
source = pd.DataFrame({"values": [12, 23, 47, 6, 52, 19]})
base = alt.Chart(source).encode(
theta=alt.Theta("values:Q", stack=True),
radius=alt.Radius("values", scale=alt.Scale(type="sqrt", zero=True, rangeMin=20)),
color="values:N",
)
c1 = base.mark_arc(innerRadius=20, stroke="#fff")
c2 = base.mark_text(radiusOffset=10).encode(text="values:Q")
c1 + c2

Some files were not shown because too many files have changed in this diff Show More