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.

Tooltip example doesn't work with data aggregate transforms

See original GitHub issue

I was trying to add a scatterplot tooltip based on the Multi-Line Tooltip example and think I ran across a bug. The example works fine when data are plotted directly but exhibits weird behavior when an aggregation function is applied.

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

# Create data and labels
a = np.random.randint(-5, 5, 200)
b = a * np.random.random(200)
s = np.random.choice(['first', 'second', 'third', 'fourth'], 200)
data = pd.DataFrame.from_dict({'a': a, 'b': b, 's': s}, orient='columns')

# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection_single(name='nearest', nearest=True, on='mouseover',  empty='none')

# Plot the mean of a against the mean of b
points = alt.Chart().mark_point(filled=True, size=100).encode(
    x='mean(a):Q',
    y='mean(b):Q',
    color='s:N'
)

# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart().mark_point().encode(
    x='mean(a):Q',
    y='mean(b):Q',
    opacity=alt.value(0),
).properties(
    selection=nearest
)

# Draw text labels near the points, and highlight based on selection
text = points.mark_text(align='left', dx=5, dy=-5).encode(
    text=alt.condition(nearest, 's:N', alt.value(' '))
)

alt.layer(points, selectors, text, data=data)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jakevdpcommented, May 2, 2018

If your goal is to show tooltips, you could use the tooltip encoding channel. It’s not fully operational yet (there is some current work in the renderers that will make the tooltip implementation much better in the near future) but it works using the HTML title attribute now. For example:

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

# Create data and labels
a = np.random.randint(-5, 5, 200)
b = a * np.random.random(200)
s = np.random.choice(['first', 'second', 'third', 'fourth'], 200)
data = pd.DataFrame.from_dict({'a': a, 'b': b, 's': s}, orient='columns')

# Plot the mean of a against the mean of b
alt.Chart(data).mark_point(filled=True, size=100).encode(
    x='mean(a):Q',
    y='mean(b):Q',
    color='s:N',
    tooltip='s:N'
)
screen shot 2018-05-02 at 12 31 10 pm

The whole thing about using selections and layers should only be used if you want to do something more sophisticated.

0reactions
gschivleycommented, May 3, 2018

Additional documentation for anyone who comes across this.

Another thing about your example: there’s no reason to add transparent selectors that are identical to your points. Just tie the selection to the points themselves.

The reason that the example you refer to uses transparent selectors is that we want to select on just the x-value, not the x and y value. There’s an example of a simpler nearest selection here: https://altair-viz.github.io/gallery/us_state_capitals.html

From vega-lite documentation it looks like the transparent selectors are used because the nearest transform isn’t supported for continuous mark types (lines and area).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I compute aggregates of aggregates for labels/tooltips?
The way to do this is with aggregate transforms, though it's a little bit tricky because you need to make sure to group...
Read more >
Power BI - Problem, Design, Solution - Concatenated Tooltip
This happens because the Tooltip field requires that any column used in it will be able to aggregate or roll up the values...
Read more >
Tooltip | Vega-Lite
Tooltips can provide details of a particular data point on demand. Tooltips for each single-view in Vega-Lite can be (1) generated based on...
Read more >
Customizing tooltips in Power BI Desktop - Microsoft Learn
You can further customize a tooltip by selecting an aggregation function. Select the arrow beside the field in the Tooltips bucket. Then, select ......
Read more >
Tableau Tooltips - Full beginners guide including Viz in Tooltips.
Tooltips are additional data details that display when you hover over one or more marks in the view. When you select one or...
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