-
-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the legends interactive in bokeh #1024
Conversation
@ahartikainen @OriolAbril I have started working on the issue and have added the interactive legend feature to |
It is basically this way. The problem currently is that we probably don't want to have legends on top of the figure, so we would really need a clever way to position the legend outside the plot. Also for multiple figures should share one legend. Here should be one working example https://gist.github.com/ahartikainen/506c23b58db06e162da9c188ceab146c E.g. location probably can take also tuple, but I'm not sure. |
@ahartikainen I looked about it and found this open issue Single Legend for GlyphRenderers on Multiple Plots in bokeh. There's a workaround given in the end which we can look for. |
@ahartikainen , I have tried to adjust the position of legends and this is what I've come up with. Also, I've tried to optimize the code for better plotting. We just have to fix the position of title, and the legend position looks good to me. |
About the other task of multiple figures sharing one legend, I've tried some methods (like passing glyph renders of one plot to another) but it didn't work. Also, in official bokeh, there is an open issue about the same. I'm thinking of trying to use the |
Looks great. I think that one-legend-thing might be hard to do. Yeah I think dropdown should be quite "easy" (yeah, they never are) to implement. Maybe something like dropdown + multiple selections (right) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
legend_items[axis_map[label]].append((data_label, plotted)) | ||
|
||
for ax1, lg in legend_items.items(): | ||
legend = Legend(items=lg, location="center_right", orientation="horizontal",) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good compromise.
I don't think there is any "universal" way to position legend so it looks good
@ahartikainen So, I'll start working on other plots' legends. Also, I noticed the interactive legends od EnergyPlot are not working. I'll start working on this plot along with other plots having legend( I guess one or two more). |
I am thinking of working on dropdowns under the |
Sounds good. No worries if something doesn't work. |
@ahartikainen , I have added interactive legends to all other plots (having legends). The discrepancy of |
@ahartikainen I have changed some font sizes. Interactive legends are working fine. This is how they are looking now: |
if legend and label is not None: | ||
return glyphs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what is making the documentation build fail. Maybe bokeh plots can return ax, (extra, elements)
or something like this? Maybe a return_glyphs argument only for internal use? @ahartikainen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, maybe add some argument to bokeh function to return this for internal use. So it would default to false and can not be seen in the main function.
btw did you mean
if legend and label is not None
or
if (legend is not None) and (label is not)
None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have done as @OriolAbril suggested. I just defined return_glyphs
as None
and returned it along the axes. If the user doesn't specify the legend, it will plot the graph without legend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to be careful when returning different values from plotting, the examples involving kde will have to be updated too, a priori they will seem to work but thumb generation will fail.
Still getting sphinx error for |
The examples assume the return value is |
Okay, thanks. I'll do the changes.
Sorry, for not carefully noticing your comment before. |
I still think our plotting code should return array of This should be the case until we figure out something more advanced. |
I have provided an internal argument that enables/disables the return value of plot_kde. Is this what you're looking for? Also what should be added to its docstring? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this looks good.
Maybe add docstring that this is internal use (is there a way for user to return the glyphs?) or that it is for advanced use
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about return_glyphs
being only in bokeh backend undocumented or allowing advanced users to use it too and therefore making it an arg of general plot_kde and adding it to the docstring. We should probably wait for @ahartikainen to weigh in.
I have commented on how I would handle the docstring if this was eventually chosen, otherwise ignore the comments.
ax.x_range.range_padding = ax.y_range.range_padding = 0 | ||
|
||
if backend_show(show): | ||
bkp.show(ax, toolbar_location="above") | ||
|
||
if legend and label is not None and return_glyph: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the if should be only on return_glyph
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we are getting the problem of unused arguments label
and legend
. Should I pop them out from kwargs in bokeh backend?
arviz/plots/kdeplot.py
Outdated
|
||
Returns | ||
------- | ||
axes : matplotlib axes or bokeh figures | ||
axes : matplotlib axes or bokeh figures and glyphs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(docstring comment)
Returns
-------
axes : matplotlib.Axes or bokeh.plotting.Figure
Object containing the kde plot
glyphs : list, optional
Bokeh glyphs present in plot. Only provided if ``return_glyph`` is True.
Followed numpy docs like np.unique
legend_items[axis_map[label]].append((data_label, plotted)) | ||
else: | ||
legend_items[axis_map[label]] = [] | ||
legend_items[axis_map[label]].append((data_label, plotted)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could legend items be a defaultdict?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it will make the code neat. Thanks, for the suggestion.
CHANGELOG.md
Outdated
@@ -12,6 +12,7 @@ | |||
* Add `skipna` argument to `hpd` and `summary` (#1035) | |||
* Added `transform` argument to `plot_trace`, `plot_forest`, `plot_pair`, `plot_posterior`, `plot_rank`, `plot_parallel`, `plot_violin`,`plot_density`, `plot_joint` (#1036) | |||
* Add `marker` functionality to `bokeh_plot_elpd` (#1040) | |||
* Add the functionality for `interactive legends` for bokeh plots of `densityplot`, `energyplot` and `essplot` (#1024) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe link to bokeh docs on interactive pegends instead of formatting it like code?
LGTM! And I love this feature, I'll probably merge tomorrow. It would be great to include this in next release. Note: I have edited the description so it won't close the issue. Reason is I think it would be great to test these interactive legends and afterwards try to get them working in plot_trace (definitely in another PR). |
Exactly, my thoughts. I was going to ask you about the legend implementation in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Fixes part of issue #953 . Make the legends interactive in bokeh.