Is it possible to use custom strings for legend labels?
See original GitHub issueIs it possible to pass a custom list of strings for the legend labels? In a chart, I want to show missing values as a part of the total so I use a stacked barchart where the segments/pieces(?) come from two columns: Missing
and Difference
, combined together they add up to the total. The legend shows the two variables as Missing
and Difference
and I would like to be able to change Difference -> Total somehow.
df = pd.DataFrame(
{'Year': [2014, 2015, 2016, 2017, 2018, 2019],
'Total': [376, 1198, 1871, 4386, 4920, 2417],
'Missing': [299, 758, 1063, 233, 32, 11],
'Difference': [77, 440, 808, 4153, 4888, 2406]}
)
melted = pd.melt(df, id_vars=['Year'], value_vars=['Difference', 'Missing'])
alt.Chart(
data=melted,
background='white'
).mark_bar(
).encode(
alt.X('Year:O', title=''),
alt.Y('value:Q', title=''),
alt.Color('variable:N',
scale=alt.Scale(domain=['Difference', 'Missing'],
range=[sand, red])),
order=alt.Order('variable', sort='descending')
)
In the plot below I have the chart I want but the Legend says Difference
and I would rather it say Total
. I know I can change the original column names or the values within the melted dataframe, but I was hoping for a cleaner way to change the legend labels.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Is it possible to add a string as a legend item in matplotlib
Sure. ax.legend() has a two argument form that accepts a list of objects (handles) and a list of strings (labels). Use a dummy...
Read more >Composing Custom Legends — Matplotlib 3.6.2 documentation
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is...
Read more >Custom legends in Matplotlib - Python Graph Gallery
This post explains how to customize the legend on a chart with matplotlib. It provides many examples covering the most common use cases...
Read more >GGPlot Legend Title, Position and Labels - Datanovia
It's possible to use the function guides () to set or remove the legend of a particular aesthetic (fill, color, size, shape, etc)....
Read more >Swap LEGEND for LABELS I Dynamically LABEL LINE ...
In this video I show you how to do dynamically label line chart series in Power BI instead of adding a legend to...
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
You can use the legend
values
setting to specify a subset of the range to show in the legend; you can’t use it to change the scale range to contain different values. Here’s an example of how it’s intended to be used:Another more slick option would be to actually use your
Total
entry and setstack=None
to create an unstacked bar chart (here I use a fold transform in the chart rather than apd.melt
external to the chart)