question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Layered or Facet bar plot with label values in Altair

See original GitHub issue

This is related to this SO question. I am trying to recreate @jakevdp 's solution, using the population dataset.

import altair as alt
from altair.expr import datum, if_
from vega_datasets import data

source = data.population.url

bars = alt.Chart(source).mark_bar(stroke='transparent').encode(
    alt.X('gender:N', scale=alt.Scale(rangeStep=12), axis=alt.Axis(title='')),
    alt.Y('sum(people):Q', axis=alt.Axis(title='population', grid=False)),
    color=alt.Color('gender:N', scale=alt.Scale(range=["#EA98D2", "#659CCA"])),
    column='age:O'
).transform_filter(
    datum.year == 2000
).transform_calculate(
    'gender', if_(datum.sex == 2, 'Female', 'Male')
)

text = bars.mark_text(
    color='black',
    dx = 10,
).encode(
    text='sum(people):Q'
)

alt.layer(bars, text).configure_view(
    stroke='transparent'
).configure_axis(
    domainWidth=0.8
)

Gives me the following error:

SchemaValidationError: Invalid specification

        altair.vegalite.v2.api.LayerChart->layer->items, validating 'anyOf'

        {'data': {'url': 'https://vega.github.io/vega-datasets/data/population.json'}, 'mark': {'type': 'bar', 'stroke': 'transparent'}, 'encoding': {'color': {'type': 'nominal', 'field': 'gender', 'scale': {'range': ['#EA98D2', '#659CCA']}}, 'column': {'type': 'ordinal', 'field': 'age'}, 'x': {'type': 'nominal', 'axis': {'title': ''}, 'field': 'gender', 'scale': {'rangeStep': 12}}, 'y': {'type': 'quantitative', 'aggregate': 'sum', 'axis': {'grid': False, 'title': 'population'}, 'field': 'people'}}, 'transform': [{'filter': '(datum.year === 2000)'}, {'calculate': "if((datum.sex === 2),'Female','Male')", 'as': 'gender'}]} is not valid under any of the given schemas

It looks like I cannot use alt.layer and column together. When I commented out the line:

    #    column='age:O'

It renders. image

Any help on overcoming this error and rendering text on every bar in each age bucket? Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jakevdpcommented, Jun 7, 2018

Layering only works with single-panel charts, so you cannot layer a faceted charts. The reason why is that, in general, matching panels between different faceted layers is tricky.

The workaround is to facet a layered chart, which is supported, though it’s important that you specify the data in the right place. There’s some discussion of this in the documentation.

Modifying your example:

import altair as alt
from altair.expr import datum, if_
from vega_datasets import data

source = data.population.url

bars = alt.Chart().mark_bar(stroke='transparent').encode(
    alt.X('gender:N', scale=alt.Scale(rangeStep=12), axis=alt.Axis(title='')),
    alt.Y('sum(people):Q', axis=alt.Axis(title='population', grid=False)),
    color=alt.Color('gender:N', scale=alt.Scale(range=["#EA98D2", "#659CCA"])),
)

text = bars.mark_text(
    color='black',
    dx = 10,
).encode(
    text='sum(people):Q'
)

alt.layer(bars, text, data=source).facet(
    column='age:O'  
).transform_filter(
    datum.year == 2000
).transform_calculate(
    'gender', if_(datum.sex == 2, 'Female', 'Male')
).configure_axis(
    domainWidth=0.8
).configure_view(
    stroke='transparent'
)
0reactions
jakevdpcommented, Oct 17, 2019

It’s difficult to help you without a complete code snippet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Layered or Facet bar plot with label values in Altair
The configure_* methods only work for the top-level chart; the error is attempting to tell you this, but it's not as clear as...
Read more >
Compound Charts: Layer, HConcat, VConcat, Repeat, Facet
Along with the basic Chart object, Altair provides a number of compound plot types that can be used to create stacked, layered, faceted,...
Read more >
altair line up concatenated charts into a grid - You.com
Along with the basic Chart object, Altair provides a number of compound plot types that can be used to create stacked, layered, faceted,...
Read more >
altair.LayerChart — Altair 4.1.0 documentation
Default value: 0.7 for non-aggregate plots with point , tick , circle , or square marks or layered bar charts and 1 otherwise....
Read more >
How to use the altair.layer function in altair - Snyk
Chart (data).mark_rule().encode( x=alt.X(alt.repeat('column'), aggregate='mean',type='quantitative', axis=None), color=alt.value('firebrick'), size=alt.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found