Layered or Facet bar plot with label values in Altair
See original GitHub issueThis 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.
Any help on overcoming this error and rendering text on every bar in each age bucket? Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
It’s difficult to help you without a complete code snippet.