Tooltips not appearing for secondary plot - altair

I’m curious why the tooltips of the 2nd plot (point_max) are not appearing in the final plot though the data points themselves are fine.
The tooltips for the first plot (rule_q) are active in the combined plot and when plotting ‘point_max’ on its own the tooltips do appear normally.
Even if the tooltips for ‘rule_q’ are removed, those for the ‘point_max’ chart never show up in the combined plot.
The relevant code:
base = alt.Chart(yrDF,title=titStr).transform_calculate(
color = 'datum.Complete > 0.95 ? "#f4be2b" : "lightgray"'
).interactive()
rule_q= base.mark_rule(size=3).encode(
alt.X('date:T'),
alt.Y('meanMinus:Q'),
alt.Y2('meanPlus:Q'),
color=alt.Color('color:N',scale=None),
href='urls:N',
tooltip=[
alt.Tooltip('date:T', title='Date',format='%b %d, %Y'),
]
)
point_max= alt.Chart(yrDF).mark_point(size=12).encode(
x=alt.X('date:T'),
y=alt.Y('max:Q'),
tooltip=[
alt.Tooltip('max:Q',format='.0f')
]
)
chart= rule_q + point_max

This is a known bug in Vega-Lite; see https://github.com/vega/vega-lite/issues/5732
A potential workaround is to add interactive() separately to each chart, rather than adding it to the base chart. See also Altair - Unable to get tooltips for one layer in a line chart.

Related

Displaying multiple values in Altair/Streamlit tooltips on a bar chart

My DataFrame looks similar to this:
name
reached points
Jose Laderman
13
William Kane
13
I am currently displaying the aggregated count of students reached points of an assignment on an Altair bar chart within Streamlit like this:
brush = alt.selection(type='interval', encodings=['x'])
interactive_test = alt.Chart(df_display_all).mark_bar(opacity=1, width=5).encode(
x= alt.X('reached points', scale=alt.Scale(domain=[0, maxPoints])),
y=alt.Y('count()', type='quantitative', axis=alt.Axis(tickMinStep=1), title='student count'),
).properties(width=1200)
upper = interactive_test.encode(
alt.X('reached points', sort=alt.EncodingSortField(op='count', order='ascending'), scale=alt.Scale(domain=brush, domainMin=-0.5))
)
lower = interactive_test.properties(
height=60
).add_selection(brush)
concat_distribution_interactive = alt.vconcat(upper, lower)
Which produces this output and everything looks fine
The information I want my tooltip to show is a list of students that reached the specific amounts of reached points I'm hovering over. When adding something like:
tooltip='name'
the way my bar chart seems to display values has now been altered to this
When adding something like
tooltip='reached points'
The data seems to be displayed normally but without a tooltip that gives me the necessary information. Is it possible to display tooltip data that isn't used in my x or y axis but still part of the DataFrame I'm putting into the chart?

Having both X and Y axes' Scales in Altair respond to a selection interval

I'm bringing this question from Altair's github. (https://github.com/altair-viz/altair/issues/2456) Is there a way to get the Scale on Y-axis in the bottom chart to respond to the selection brush? I'd like to be able to pan around the top chart with a selection and see the zoomed-in results in the bottom chart. If I uncomment the alt.Y, then both the X and Y axes show Years and it's messed up. Is there a way to pass just an X or Y value in the 'brush' maybe? Thank you very much!
brush = alt.selection_interval(init={'x':[1950, 1970], 'y':[1500000, 2500000]}, encodings=['x', 'y'])
base = alt.Chart().mark_line().encode(
x=alt.X('Year:Q', title=None),
y='Deaths:Q',
color='Entity:N'
)
alt.vconcat(
base.add_selection(brush).encode().properties(height=150, width=150),
base.encode(
alt.X('Year:Q', scale=alt.Scale(domain=brush)),
#alt.Y('Deaths:Q', scale=alt.Scale(domain=brush)) # (un)commenting this line makes it work/fail only along the x-axis
).properties(
height=500, width=500
),
data='https://vega.github.io/vega-datasets/data/disasters.csv'
)
Yes, see Open the Chart in the Vega Editor
It filters the data of the 2nd chart using a filter transform on the brush param.

Set opacity for marks but not in legend

I can modify the classic Simple Scatter Plot with Tooltips, to add opacity to marks, but I'd like to legend colors to stay 100% opaque. In the chart I'm trying to make, I have a df with tens of thousands of rows.
import altair as alt
from vega_datasets import data
source = data.cars()
alt.Chart(source).mark_circle(size=60, opacity=0.1).encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin'
)
I've tried alt.Legend's symbolOpacity and gradientOpacity to no avail
color=alt.Color(
'Origin:N',
legend=alt.Legend(
# symbolOpacity=1,
gradientOpacity=1,
)
)
As of now, this seems to be a bug with vega-lite. Per #jvp's suggestion, I've filed a bug report here
UPDATE -- IT'S FIXED

Plotting values using matplotlib and find minimum by looking the graph

I have dictionary as:
```{'0.0': 2.445616223564293,
'0.05': 2.445594095119315,
'0.1': 2.4455740588234223,
'0.15': 2.4455560866270947,
'0.2': 2.4455401509059596,
'0.25': 2.4455262244535803,
'1.0': 2.4455411399961293,
'1.05': 2.44555597697399,
'1.1': 2.4455724183134344,
'1.15': 2.4455904432448716,
'1.2': 2.445610031303073,
'1.25': 2.4456311623222002,
'2.0': 2.4461204322901566,
'3.0': 2.447205696789686,
'4.0': 2.4486856713473726,
'5.0': 2.4504762863004363,
'10.0': 2.4623061878090624,
'20.0': 2.4922549001247876}```
Here all the values are different by some small factor. However when I plot it using matplotlib the plot is not distinctive.
I want to plot "keys" in x-axis and "values" in y-axis and then find x which has minimum y value by looking the plot.
I tried this code:
```plt.plot(*zip(*data))```
But the plot is not clear. How can I solve this problem such that plot is clearly able to show the difference in values.
The problem is your interpretation of zip(*data). I would suggest before plotting you first print and see what you are trying to plot.
print (list(zip(*data))) would print a list of splitted strings (keys of your data). To plot the keys on the x-axis and the values of the y-axis, simply do the following. I leave the visualization of the minimum up to you. If you want to plot the difference, subtract the first value from the complete list of values and then plot it on the y-axis.
plt.plot(data.keys(), data.values(), '-bx')
plt.xticks(rotation=45)

Plot points playing games when I try to size by a value count in bokeh

I'm trying to get the plot points in a scatter graph to size according to the frequency of values in a column of data. The data is coming from a questionnaire.
My questions are: What am I doing wrong, and what can I do to fix it?
I can push out a simple plot with x and y values coming from 2 columns of data. The X axis represents a level (1-100), and the Y axis represents a choice users can make for each level (1-4). For this plot I want to track how many people choose 1-4 on each level - so I need to capture that 1-4 has been selected, then indicate how many times.
Simple plot works fine, though those points have multiple occurrences.
Here's the code for that:
# Set up the graph
WT_Number = data.wt # This is the X axis
CFG_Number = data.cfg # This is the Y axis
wt_cfg_plot = figure(plot_width=1000, plot_height=400,
title="Control Form Groups chosen by WT unit")
# Set up the plot points, including the Hover Tool
cr = wt_cfg_plot.scatter(WT_Number, CFG_Number, size=7,
fill_color="blue",
line_color=None, alpha=0.7, hover_fill_color="firebrick",
hover_line_color=None, hover_alpha=1)
Problem: I then added a value count and set it as the size, to get the plot points to adjust according to the value frequency. But now it pumps out this chart and throws an error:
Plot points are reacting to the code, but now they're doing their own thing.
I added a variable for the value counts (cfg_freq), and used that as the size:
cfg_freq = data['cfg'].value_counts()*4
cr = wt_cfg_plot.scatter(WT_Number, CFG_Number, size=cfg_freq, fill_color="blue",
line_color=None, alpha=0.7, hover_fill_color="firebrick",
hover_line_color=None, hover_alpha=1)
Here's the the last part of the error being thrown:
File "/Applications/anaconda/lib/python3.5/site-packages/bokeh/core/properties.py", line 722, in setattr
(name, self.class.name, text, nice_join(matches)))
AttributeError: unexpected attribute 'size' to Chart, possible attributes are above, background_fill_alpha, background_fill_color, below, border_fill_alpha, border_fill_color, disabled, extra_x_ranges, extra_y_ranges, h_symmetry, height, hidpi, left, legend, lod_factor, lod_interval, lod_threshold, lod_timeout, logo, min_border, min_border_bottom, min_border_left, min_border_right, min_border_top, name, outline_line_alpha, outline_line_cap, outline_line_color, outline_line_dash, outline_line_dash_offset, outline_line_join, outline_line_width, plot_height, plot_width, renderers, responsive, right, tags, title, title_standoff, title_text_align, title_text_alpha, title_text_baseline, title_text_color, title_text_font, title_text_font_size, title_text_font_style, tool_events, toolbar_location, tools, v_symmetry, webgl, width, x_mapper_type, x_range, xgrid, xlabel, xscale, y_mapper_type, y_range, ygrid, ylabel or yscale

Resources