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.

Having problem add text to grouped bar chart

See original GitHub issue

Here’s what I’m trying to do .

I have a dataframe.

# init dataframe
df = pd.DataFrame({
    "group": ['g1','g1','g1','g2','g2','g2'],
    "type": ['a', 'b', 'c', 'a', 'b', 'c'],
    "value": [1,1,1,2,2,2]
})

# alt base
base = alt.Chart(df).encode(
    x='value',
    y='group',
    color='group:N',
    row='type'
)

Only plot bar.

base.make_bar

image

Only plot text.

base.mark_text().encode(text='value')

image

Add them up and raise error.

base.mark_text().encode(text='value') + base.mark_bar()

Trackback

ValueError                                Traceback (most recent call last)
<ipython-input-188-00e0014b2982> in <module>()
----> 1 base.mark_text().encode(text='value') + base.mark_bar()

~/Tools/anaconda3/lib/python3.6/site-packages/altair/vegalite/v3/api.py in __add__(self, other)
    481         if not isinstance(other, TopLevelMixin):
    482             raise ValueError("Only Chart objects can be layered.")
--> 483         return layer(self, other)
    484 
    485     def __and__(self, other):

~/Tools/anaconda3/lib/python3.6/site-packages/altair/vegalite/v3/api.py in layer(*charts, **kwargs)
   1877 def layer(*charts, **kwargs):
   1878     """layer multiple charts"""
-> 1879     return LayerChart(layer=charts, **kwargs)
   1880 
   1881 

~/Tools/anaconda3/lib/python3.6/site-packages/altair/vegalite/v3/api.py in __init__(self, data, layer, **kwargs)
   1826         for spec in layer:
   1827             _check_if_valid_subspec(spec, 'LayerChart')
-> 1828             _check_if_can_be_layered(spec)
   1829         super(LayerChart, self).__init__(data=data, layer=list(layer), **kwargs)
   1830         self.data, self.layer = _combine_subchart_data(self.data, self.layer)

~/Tools/anaconda3/lib/python3.6/site-packages/altair/vegalite/v3/api.py in _check_if_can_be_layered(spec)
   1664         for channel in ['row', 'column', 'facet']:
   1665             if _get(encoding, channel) is not Undefined:
-> 1666                 raise ValueError("Faceted charts cannot be layered.")
   1667     if isinstance(spec, (Chart, LayerChart)):
   1668         return

ValueError: Faceted charts cannot be layered.

What I did wrong here?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Jun 21, 2019

Sure, there’s a section about it in the docs here: https://altair-viz.github.io/user_guide/compound_charts.html

Let me know if anything there could be clarified more.

1reaction
jakevdpcommented, Jun 20, 2019

You’re getting this error because faceted charts cannot be layered (This is because in general there is no guarantee that the facets will align). However, layered charts can be faceted, and so you can accomplish what you want by calling the facet() method on a layered chart. For example:

import altair as alt
import pandas as pd

# init dataframe
df = pd.DataFrame({
    "group": ['g1','g1','g1','g2','g2','g2'],
    "type": ['a', 'b', 'c', 'a', 'b', 'c'],
    "value": [1,1,1,2,2,2]
})

# alt base
base = alt.Chart(df).encode(
    x='value',
    y='group',
    color='group:N',
)

alt.layer(
  base.mark_bar(),
  base.mark_text(dx=5).encode(text='value')
).facet(
  row='type'
)

visualization - 2019-06-20T063817 379

Read more comments on GitHub >

github_iconTop Results From Across the Web

3.9 Adding Labels to a Bar Graph - R Graphics Cookbook
Another common scenario is to add labels for a bar graph of counts instead of values. To do this, use geom_bar() , which...
Read more >
Append text to Grouped Bar Chart in d3js v4 - Stack Overflow
Lots of ways to do this; here's how I would do it. First, keep a reference to the groups g element so that...
Read more >
Grouped Bar Charts with Labels in Matplotlib - Python Charts
Learn how to plot grouped bar charts in Matplotlib. We also show how to center bar labels, match bar label color to the...
Read more >
How do i label each bar in bar group with a "string" on top?
The text command does the two groups with the two bars of each group labeled in the one call for each bar group....
Read more >
How to create a grouped bar chart in R with ... - YouTube
A grouped bar chart plot can be an effective way to represent the variation across a continuous variable for multiple levels of two ......
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