Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iris to penguins #2231

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 47 additions & 44 deletions doc/user_guide/compound_charts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,21 @@ distribution of its points:
import altair as alt
from vega_datasets import data

iris = data.iris.url
penguins = data.penguins.url

chart1 = alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N'
chart1 = alt.Chart(penguins).mark_point().encode(
x=alt.X('Flipper Length (mm):Q', scale=alt.Scale(zero=False)),
y=alt.Y('Body Mass (g):Q', scale=alt.Scale(zero=False)),
color='Species:N'
).properties(
height=300,
width=300
)

chart2 = alt.Chart(iris).mark_bar().encode(
chart2 = alt.Chart(penguins).mark_bar().encode(
x='count()',
y=alt.Y('petalWidth:Q', bin=alt.Bin(maxbins=30)),
color='species:N'
y=alt.Y('Body Mass (g):Q', bin=alt.Bin(maxbins=30)),
color='Species:N'
).properties(
height=300,
width=100
Expand Down Expand Up @@ -224,20 +224,23 @@ showing how ``repeat`` can be used to build the chart more efficiently:
import altair as alt
from vega_datasets import data

iris = data.iris.url
penguins = data.penguins.url

base = alt.Chart().mark_point().encode(
color='species:N'
color='Species:N'
).properties(
width=200,
height=200
).interactive()

chart = alt.vconcat(data=iris)
for y_encoding in ['petalLength:Q', 'petalWidth:Q']:
chart = alt.vconcat(data=penguins)
for y_encoding in ['Flipper Length (mm):Q', 'Body Mass (g):Q']:
row = alt.hconcat()
for x_encoding in ['sepalLength:Q', 'sepalWidth:Q']:
row |= base.encode(x=x_encoding, y=y_encoding)
for x_encoding in ['Beak Length (mm):Q', 'Beak Depth (mm):Q']:
row |= base.encode(
x=alt.X(x_encoding, scale=alt.Scale(zero=False)),
y=alt.Y(y_encoding, scale=alt.Scale(zero=False)),
)
chart &= row
chart

Expand All @@ -252,18 +255,18 @@ method, makes this type of chart a bit easier to produce:

import altair as alt
from vega_datasets import data
iris = data.iris.url
penguins = data.penguins.url

alt.Chart(iris).mark_point().encode(
alt.X(alt.repeat("column"), type='quantitative'),
alt.Y(alt.repeat("row"), type='quantitative'),
color='species:N'
alt.Chart(penguins).mark_point().encode(
alt.X(alt.repeat("column"), type='quantitative', scale=alt.Scale(zero=False)),
alt.Y(alt.repeat("row"), type='quantitative', scale=alt.Scale(zero=False)),
color='Species:N'
).properties(
width=200,
height=200
).repeat(
row=['petalLength', 'petalWidth'],
column=['sepalLength', 'sepalWidth']
row=['Flipper Length (mm)', 'Body Mass (g)'],
column=['Beak Length (mm)', 'Beak Depth (mm)']
).interactive()

The :meth:`Chart.repeat` method is the key here: it lets you specify a set of
Expand Down Expand Up @@ -291,20 +294,20 @@ concatenation:
import altair as alt
from altair.expr import datum
from vega_datasets import data
iris = data.iris.url
penguins = data.penguins.url

base = alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N'
base = alt.Chart(penguins).mark_point().encode(
x=alt.X('Flipper Length (mm):Q', scale=alt.Scale(zero=False)),
y=alt.Y('Body Mass (g):Q', scale=alt.Scale(zero=False)),
color='Species:N'
).properties(
width=160,
height=160
)

chart = alt.hconcat()
for species in ['setosa', 'versicolor', 'virginica']:
chart |= base.transform_filter(datum.species == species)
for species in ['Adelie', 'Chinstrap', 'Gentoo']:
chart |= base.transform_filter(datum.Species == species)
chart

As with the manual approach to :ref:`repeat-chart`, this is straightforward,
Expand All @@ -314,27 +317,27 @@ Using ``alt.facet`` it becomes a bit cleaner:

.. altair-plot::

alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N'
alt.Chart(penguins).mark_point().encode(
x=alt.X('Flipper Length (mm):Q', scale=alt.Scale(zero=False)),
y=alt.Y('Body Mass (g):Q', scale=alt.Scale(zero=False)),
color='Species:N'
).properties(
width=180,
height=180
).facet(
column='species:N'
column='Species:N'
)

For simple charts like this, there is also a ``column`` encoding channel that
can give the same results:

.. altair-plot::

alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N',
column='species:N'
alt.Chart(penguins).mark_point().encode(
x=alt.X('Flipper Length (mm):Q', scale=alt.Scale(zero=False)),
y=alt.Y('Body Mass (g):Q', scale=alt.Scale(zero=False)),
color='Species:N',
column='Species:N'
).properties(
width=180,
height=180
Expand All @@ -348,10 +351,10 @@ layered chart with a hover selection:

hover = alt.selection_single(on='mouseover', nearest=True, empty='none')

base = alt.Chart(iris).encode(
x='petalLength:Q',
y='petalWidth:Q',
color=alt.condition(hover, 'species:N', alt.value('lightgray'))
base = alt.Chart(penguins).encode(
x=alt.X('Flipper Length (mm):Q', scale=alt.Scale(zero=False)),
y=alt.Y('Body Mass (g):Q', scale=alt.Scale(zero=False)),
color=alt.condition(hover, 'Species:N', alt.value('lightgray'))
).properties(
width=180,
height=180,
Expand All @@ -362,12 +365,12 @@ layered chart with a hover selection:
)

text = base.mark_text(dy=-5).encode(
text = 'species:N',
text = 'Species:N',
opacity = alt.condition(hover, alt.value(1), alt.value(0))
)

alt.layer(points, text).facet(
'species:N',
'Species:N',
)

Though each of the above examples have faceted the data across columns,
Expand Down