Having problem add text to grouped bar chart
See original GitHub issueHere’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
Only plot text.
base.mark_text().encode(text='value')
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:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top 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 >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
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.
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: