-
-
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
Changes from 18 commits
56ee7c7
046211b
3d9ef3a
eb6acd0
b1a8904
8cd1301
d4bdbfa
0c67b2f
d12d9a1
7d0b3dd
cc9a950
8f60567
29e4f85
806c2a1
eef6d5e
7417a5e
e07ef31
50ffbd4
8c0df01
6ff20a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import bokeh.plotting as bkp | ||
import numpy as np | ||
from bokeh.layouts import gridplot | ||
from bokeh.models.annotations import Title | ||
from bokeh.models.annotations import Title, Legend | ||
|
||
from . import backend_kwarg_defaults, backend_show | ||
from ...plot_utils import ( | ||
|
@@ -66,18 +66,17 @@ def plot_density( | |
if data_labels is None: | ||
data_labels = {} | ||
|
||
legend_items = {} | ||
for m_idx, plotters in enumerate(to_plot): | ||
for ax_idx, (var_name, selection, values) in enumerate(plotters): | ||
for var_name, selection, values in plotters: | ||
label = make_label(var_name, selection) | ||
|
||
if data_labels: | ||
data_label = data_labels[m_idx] | ||
if ax_idx != 0 or data_label == "": | ||
data_label = None | ||
else: | ||
data_label = None | ||
|
||
_d_helper( | ||
plotted = _d_helper( | ||
values.flatten(), | ||
label, | ||
colors[m_idx], | ||
|
@@ -90,8 +89,18 @@ def plot_density( | |
outline, | ||
shade, | ||
axis_map[label], | ||
data_label=data_label, | ||
) | ||
if data_label is not None: | ||
if axis_map[label] in legend_items.keys(): | ||
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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it will make the code neat. Thanks, for the suggestion. |
||
|
||
for ax1, legend in legend_items.items(): | ||
legend = Legend(items=legend, location="center_right", orientation="horizontal",) | ||
ax1.add_layout(legend, "above") | ||
ax1.legend.click_policy = "hide" | ||
|
||
if backend_show(show): | ||
grid = gridplot(ax.tolist(), toolbar_location="above") | ||
|
@@ -113,11 +122,10 @@ def _d_helper( | |
outline, | ||
shade, | ||
ax, | ||
data_label, | ||
): | ||
|
||
extra = dict() | ||
if data_label is not None: | ||
extra["legend_label"] = data_label | ||
plotted = [] | ||
|
||
if vec.dtype.kind == "f": | ||
if credible_interval != 1: | ||
|
@@ -133,29 +141,41 @@ def _d_helper( | |
ymax = density[-1] | ||
|
||
if outline: | ||
ax.line(x, density, line_color=color, line_width=line_width, **extra) | ||
ax.line( | ||
[xmin, xmin], | ||
[-ymin / 100, ymin], | ||
line_color=color, | ||
line_dash="solid", | ||
line_width=line_width, | ||
plotted.append(ax.line(x, density, line_color=color, line_width=line_width, **extra)) | ||
plotted.append( | ||
ax.line( | ||
[xmin, xmin], | ||
[-ymin / 100, ymin], | ||
line_color=color, | ||
line_dash="solid", | ||
line_width=line_width, | ||
muted_color=color, | ||
muted_alpha=0.2, | ||
) | ||
) | ||
ax.line( | ||
[xmax, xmax], | ||
[-ymax / 100, ymax], | ||
line_color=color, | ||
line_dash="solid", | ||
line_width=line_width, | ||
plotted.append( | ||
ax.line( | ||
[xmax, xmax], | ||
[-ymax / 100, ymax], | ||
line_color=color, | ||
line_dash="solid", | ||
line_width=line_width, | ||
muted_color=color, | ||
muted_alpha=0.2, | ||
) | ||
) | ||
|
||
if shade: | ||
ax.patch( | ||
np.r_[x[::-1], x, x[-1:]], | ||
np.r_[np.zeros_like(x), density, [0]], | ||
fill_color=color, | ||
fill_alpha=shade, | ||
**extra | ||
plotted.append( | ||
ax.patch( | ||
np.r_[x[::-1], x, x[-1:]], | ||
np.r_[np.zeros_like(x), density, [0]], | ||
fill_color=color, | ||
fill_alpha=shade, | ||
muted_color=color, | ||
muted_alpha=0.2, | ||
**extra | ||
) | ||
) | ||
|
||
else: | ||
|
@@ -165,35 +185,46 @@ def _d_helper( | |
_, hist, edges = histogram(vec, bins=bins) | ||
|
||
if outline: | ||
ax.quad( | ||
top=hist, | ||
bottom=0, | ||
left=edges[:-1], | ||
right=edges[1:], | ||
line_color=color, | ||
fill_color=None, | ||
**extra | ||
plotted.append( | ||
ax.quad( | ||
top=hist, | ||
bottom=0, | ||
left=edges[:-1], | ||
right=edges[1:], | ||
line_color=color, | ||
fill_color=None, | ||
muted_color=color, | ||
muted_alpha=0.2, | ||
**extra | ||
) | ||
) | ||
else: | ||
ax.quad( | ||
top=hist, | ||
bottom=0, | ||
left=edges[:-1], | ||
right=edges[1:], | ||
line_color=color, | ||
fill_color=color, | ||
fill_alpha=shade, | ||
**extra | ||
plotted.append( | ||
ax.quad( | ||
top=hist, | ||
bottom=0, | ||
left=edges[:-1], | ||
right=edges[1:], | ||
line_color=color, | ||
fill_color=color, | ||
fill_alpha=shade, | ||
muted_color=color, | ||
muted_alpha=0.2, | ||
**extra | ||
) | ||
) | ||
|
||
if hpd_markers: | ||
ax.diamond(xmin, 0, line_color="black", fill_color=color, size=markersize) | ||
ax.diamond(xmax, 0, line_color="black", fill_color=color, size=markersize) | ||
plotted.append(ax.diamond(xmin, 0, line_color="black", fill_color=color, size=markersize)) | ||
plotted.append(ax.diamond(xmax, 0, line_color="black", fill_color=color, size=markersize)) | ||
|
||
if point_estimate is not None: | ||
est = calculate_point_estimate(point_estimate, vec, bw) | ||
ax.circle(est, 0, fill_color=color, line_color="black", size=markersize) | ||
plotted.append(ax.circle(est, 0, fill_color=color, line_color="black", size=markersize)) | ||
|
||
_title = Title() | ||
_title.text = vname | ||
ax.title = _title | ||
ax.title.text_font_size = "13pt" | ||
|
||
return plotted | ||
OriolAbril marked this conversation as resolved.
Show resolved
Hide resolved
|
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?