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.

Getting legend for multilayer chart

See original GitHub issue

Is it possible to make a legend for such a chart

import altair as alt
import numpy as np
import pandas as pd

x = np.arange(100)
data = pd.DataFrame({'x': x,
                     'sin(x)': np.sin(x / 5),
                     'data': np.sin(x / 5) + 0.3*np.random.rand(100)})

line = alt.Chart(data).mark_line(strokeWidth=6, color='orange').encode(
    x='x',
    y='sin(x)'
)

point = alt.Chart(data).mark_point(color='black').encode(
    x='x',
    y='data'
)
(line + point)

image

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:31 (14 by maintainers)

github_iconTop GitHub Comments

6reactions
jakevdpcommented, Jun 19, 2020

Yes, in newer versions of vega-lite you can set encodings to a constant datum value, which will be used to populate the legend. Altair doesn’t yet support this, though.

In Altair it would probably look something like this (Note that this does not work in the current release):

alt.Chart(data).mark_line().encode(
  x='x',
  y='y',
  color=alt.datum("My Line")
)
3reactions
ajasjacommented, Jun 28, 2018

Thanks! This is indeed a bit hacky:) But I got even a bit closer.

x = np.arange(100)
data = pd.DataFrame({'x': x,
                     'sin(x)': np.sin(x / 5),
                     'data': np.sin(x / 5) + 0.3*np.random.rand(100),
                     'line_label': 100 * ['line'],
                     'points_label': 100 * ['points']})

line = alt.Chart(data).mark_line(strokeWidth=6, color='orange').encode(
    x='x',
    y='sin(x)',
    opacity=alt.Opacity('line_label', legend=alt.Legend(title=""))
)

point = alt.Chart(data).mark_point(color='black').encode(
    x='x',
    y='data',
    shape=alt.Shape('points_label', legend=alt.Legend(title=""))
)
(line + point)

PS: this is probably a different debate (e.g. https://github.com/altair-viz/altair/issues/947); I just found out I could do

shape=alt.Shape('points_label', title="" )

instead of

shape=alt.Shape('points_label', legend=alt.Legend(title="") )

Big kudos points! 👍 Is this documented somewhere or is it a more try and see?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding legend to layerd chart in altair - python - Stack Overflow
In the resulting chart, I would like to add a legend that shows, that the blue line shows the maximum temperature and the...
Read more >
PPT Design Tip: One Legend for Multiple Charts (PowerPoint)
PPT design tip: One legend for multiple charts. Russ Borrelli from eSlide walks you through his tips on how to easily create a...
Read more >
Add a legend to a chart - Microsoft Support
Click the chart. · Click Chart Filters Filters next to the chart, and click Select Data. · Select an entry in the Legend...
Read more >
Configure Legend - FusionCharts
A legend is a chart element used to display the series name for each dataset, in case of multi-series or combination charts. Legends...
Read more >
How to create a creative multi-layer Doughnut Chart in Excel
Move the legend to the right. This makes sense, because just like in the chart, the data points represent an order from the...
Read more >

github_iconTop Related Medium Post

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