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.

Adding text to bar chart throws 'data is a required property' error

See original GitHub issue

When I run this code, I get a ‘data is a required property’ error (pasted below the code). When I just try to run the bar graph with data specified within Chart() it works.

bars = alt.Chart().mark_bar(stroke='transparent').encode(
    alt.X('model', scale=alt.Scale(rangeStep=12), axis=alt.Axis(title='')),
    alt.Y('ratio:Q', axis=alt.Axis(title='ratio', grid=False)),
    color=alt.Color('model:N', scale=alt.Scale(range=["#EA98D2", "#659CCA", '#5bb3a4'])),
    column='time:O', 
)

text = alt.Chart().mark_text(
    align='left',
    baseline='middle'
).encode(
    y = alt.Y('ratio'), 
    x = 'model',
    text='ratio')

alt.layer(bars, text, data=df_concat).configure(
    legend=LegendConfig(labelFontSize=16, titleFontSize=16, symbolSize=100, labelFont='Lato'), axis=AxisConfig(labelFontSize=16, tickSize=16, labels=True, titleFontSize=16))

Error Message:

SchemaValidationError: Invalid specification

        altair.vegalite.v2.api.Chart, validating 'required'

        'data' is a required property

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Alcampopianocommented, Oct 29, 2018

@lcyraphael Off the top of my head I think you want to configure the header if I understand correctly.

import altair as alt
from altair.expr import datum
from vega_datasets import data
iris = data.iris.url


alt.Chart(iris).mark_point().encode(
    x='petalLength:Q',
    y='petalWidth:Q',
    color='species:N'
).properties(
    width=180,
    height=180
).facet(
    column='species:N'
).configure_header(titleFontSize=40, labelFontSize=40

)

visualization 52

1reaction
jakevdpcommented, Oct 9, 2018

Ah, I see now. The error is unfortunately not particularly informative here, but the core issue is that you cannot use a faceted chart within a layer (from the perspective of the renderer, it’s ill-defined what the user intends in that case).

The alternative is to create a layered chart first, and then facet it. For example:

import altair as alt
import pandas as pd

df_concat = pd.DataFrame({
'time': [87, 88, 89, 87, 88, 89, 87, 88, 89],
'ratio': [0.27, 0.39, 0.35, 0.39, 0.51, 0.38, 0.20, 0.35, 0.47],
'model': ['b3b', 'b3b', 'b3b', 'baseline', 'baseline', 'baseline', 'truth', 'truth', 'truth']
})

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

text = alt.Chart().mark_text(
    align='left',
    baseline='middle'
).encode(
    y = alt.Y('ratio'), 
    x = 'model',
    text='ratio'
)

alt.layer(bars, text, data=df_concat).facet(
    column='time:O'
).configure(
    legend=alt.LegendConfig(labelFontSize=16, titleFontSize=16, symbolSize=100, labelFont='Lato'),
    axis=alt.AxisConfig(labelFontSize=16, tickSize=16, labels=True, titleFontSize=16)
)

visualization

Read more comments on GitHub >

github_iconTop Results From Across the Web

Altair SchemaValidation Error When Faceting - Stack Overflow
SchemaValidationError: Invalid specification altair.vegalite.v4.api.Chart, validating 'required' 'data' is a required property alt.FacetChart(..
Read more >
Required Property Value Error - OutSystems 11 Documentation
Required Property Value: A valid expression must be set for parameter 'SourceDataPointList'. Cause. You added an Area Chart, Bar Chart, ...
Read more >
Add, change, or remove error bars in a chart - Microsoft Support
On 2-D area, bar, column, line, xy (scatter), or bubble chart, do one of the following: To add error bars to all data...
Read more >
Create Bar Chart using D3 - TutorialsTeacher
Learn how to create SVG bar chart with scales and axes in D3. ... + "," + 100 + ")"); d3.csv("XYZ.csv", function(error, data)...
Read more >
Bar Charts | Google Developers
Like all Google charts, bar charts display tooltips when the user hovers over the data. For a vertical version of this chart, see...
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