Skip to content

Commit

Permalink
Merge pull request #26 from lmcinnes/main
Browse files Browse the repository at this point in the history
A minor polish for word cloud plots
  • Loading branch information
jc-healy authored Apr 18, 2023
2 parents a8b3012 + cf2bd36 commit 58771f2
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 119 deletions.
50 changes: 48 additions & 2 deletions thisnotthat/bokeh_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def add_text_layer(
angle: float = 0,
text_color: str = "#444444",
text_outline_color: str = "#dddddd",
background_fill_color: str = "#eeeeee22",
padding: int = 0,
border_radius: int = 5,
text_font: Dict[str, str] = {"value": "helvetica"},
text_font_style: str = "normal",
text_line_height: float = 0.9,
Expand Down Expand Up @@ -96,6 +99,9 @@ def add_text_layer(
angle=angle,
text_color=text_color,
text_outline_color=text_outline_color,
background_fill_color=background_fill_color,
padding=padding,
border_radius=border_radius,
text_font=text_font,
text_font_style=text_font_style,
text_line_height=text_line_height,
Expand All @@ -116,7 +122,7 @@ def add_text_layer(
upper_transition_val = max_text_size - text_transition_width
lower_transition_val = min_text_size + text_transition_width
text_resize_callback = bokeh.models.callbacks.CustomJS(
args=dict(labels=labels),
args=dict(labels=labels, background_fill_color=background_fill_color),
code="""
const scale = cb_obj.end - cb_obj.start;
const text_size = (%f / scale);
Expand All @@ -131,8 +137,10 @@ def add_text_layer(
}
if (alpha > 0) {
labels.text_alpha = alpha;
labels.background_fill_color = background_fill_color;
} else {
labels.text_alpha = 0.0;
labels.background_fill_color = "#ffffff00";
}
labels.text_font_size = text_size + "pt";
labels.change.emit();
Expand Down Expand Up @@ -243,12 +251,30 @@ class BokehPlotPane(pn.viewable.Viewer, pn.reactive.Reactive):
selection_fill_alpha: float (optional, default = 1.0)
The alpha value to use for selected points in the scatterplot.
selection_line_color: str (optional, default = "black")
The line colour of selected points in the scatterplot.
selection_line_width: float (optional, default = 0.5)
The line width of selected points in the scatterplot.
selection_line_alpha: float (optional, default = 0.5)
The line alpha of selected points in the scatterplot.
nonselection_fill_alpha: float (optional, default = 0.1)
The alpha value to use for points that are not in an active selection in the scatterplot.
nonselection_fill_color: str (optional, default = "gray")
The colour to use to fill points that are not in an active selection in the scatterplot.
nonselection_line_color: str (optional, default = None)
The outline color to use for points that are not in an active selection in the scatterplot.
nonselection_line_width: float (optional, default = 0.0)
The outline line weight for points that are not in an active selection in the scatterplot.
nonselection_line_alpha: float (optional, default = 0.0)
The outline alpha for points that are not in an active selection in the scatterplot.
background_fill_color: str (optional, default = "#FFFFFF")
The colour to use for the background of the scatterplot.
Expand Down Expand Up @@ -330,8 +356,14 @@ def __init__(
hover_line_width: float = 2,
tooltip_template: str = """<div>@hover_text</div>""",
selection_fill_alpha: float = 1.0,
selection_line_color: str = "label",
selection_line_width: float = 0.5,
selection_line_alpha: float = 0.5,
nonselection_fill_alpha: float = 0.1,
nonselection_fill_color: str = "gray",
nonselection_line_color: Optional[str] = None,
nonselection_line_width: float = 0.0,
nonselection_line_alpha: float = 0.0,
background_fill_color: str = "#ffffff",
border_fill_color: str = "whitesmoke",
toolbar_location: str = "above",
Expand All @@ -340,6 +372,7 @@ def __init__(
title_location: str = "above",
show_legend: bool = True,
legend_location: str = "outside",
sizing_mode="fixed",
name: str = "Plot",
):
super().__init__(name=name)
Expand Down Expand Up @@ -418,6 +451,7 @@ def __init__(
tools=tools,
title=title,
title_location=title_location,
sizing_mode=sizing_mode,
)
self.plot.toolbar.active_scroll = self.plot.select_one(
bokeh.models.WheelZoomTool
Expand All @@ -434,8 +468,14 @@ def __init__(
hover_line_color=hover_line_color,
hover_line_width=hover_line_width,
selection_fill_alpha=selection_fill_alpha,
selection_line_color=selection_line_color,
selection_line_width=selection_line_width,
selection_line_alpha=selection_line_alpha,
nonselection_fill_alpha=nonselection_fill_alpha,
nonselection_fill_color=nonselection_fill_color,
nonselection_line_color=nonselection_line_color,
nonselection_line_width=nonselection_line_width,
nonselection_line_alpha=nonselection_line_alpha,
)
if show_legend:
self._legend = bokeh.models.Legend(
Expand Down Expand Up @@ -507,7 +547,7 @@ def __init__(
self.plot.ygrid.grid_line_color = None
self.plot.xaxis.bounds = (0, 0)
self.plot.yaxis.bounds = (0, 0)
self.pane = pn.pane.Bokeh(self.plot)
self.pane = pn.pane.Bokeh(self.plot, sizing_mode=sizing_mode)

self.show_legend = show_legend
self.color_by_vector = pd.Series([], dtype=object)
Expand Down Expand Up @@ -718,6 +758,9 @@ def add_cluster_labels(
text_font_style: str = "normal",
text_line_height: float = 0.9,
text_alpha: float = 1.0,
background_fill_color: str = "#eeeeee22",
padding = 0,
border_radius=5,
max_text_size: float = 64.0,
min_text_size: float = 2.0,
text_transition_width: float = 16.0,
Expand Down Expand Up @@ -792,6 +835,9 @@ def add_cluster_labels(
angle=angle,
text_color=text_color,
text_outline_color=text_outline_color,
background_fill_color=background_fill_color,
padding=padding,
border_radius=border_radius,
text_font=text_font,
text_font_style=text_font_style,
text_line_height=text_line_height,
Expand Down
4 changes: 2 additions & 2 deletions thisnotthat/deckgl_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def __init__(
"height": "64px",
"text-align": "right",
},
margin=[5, 0],
margin=(5, 0),
),
self.select_method,
)
Expand Down Expand Up @@ -207,7 +207,7 @@ def __init__(
"height": "64px",
"text-align": "left",
},
margin=[5, 0],
margin=(5, 0),
)
self.points["data"] = self.dataframe
self.deck_pane.param.trigger("object")
Expand Down
24 changes: 16 additions & 8 deletions thisnotthat/instance_viewer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import panel as pn
import param
import pandas as pd
import time

from typing import *

Expand All @@ -24,7 +25,7 @@ class InformationPane(pn.reactive.Reactive):
markdown_template: str
A string in markdown providing formatting for a single row of data from the dataframe. Within the string
a substitution of the row value of of a column will be done where ``{column_name}`` appears in the string.
a substitution of the row value of a column will be done where ``{column_name}`` appears in the string.
width: int (optional, default = 200)
The width of the pane.
Expand Down Expand Up @@ -69,13 +70,17 @@ def __init__(
disable_math: bool = False,
extensions: List[str] = ["extra", "smarty", "codehilite"],
style: dict = {},
margin: List[int] = [5, 5],
margin: Tuple[int] = (5, 5),
sizing_mode: str = "stretch_height",
throttle = 200,
name: str = "Information",
):
super().__init__(name=name)
self.data = raw_dataframe
self.markdown_template = markdown_template
self.placeholder_text = placeholder_text
self.throttle = throttle
self._last_update = time.perf_counter() * 1000.0
self.markdown = pn.pane.Markdown(
self.placeholder_text,
width=width - 20,
Expand All @@ -84,9 +89,10 @@ def __init__(
dedent=dedent,
disable_math=disable_math,
extensions=extensions,
sizing_mode=sizing_mode,
style=style,
)
self.pane = pn.Column(self.markdown, width=width, height=height, scroll=True)
self.pane = pn.Column(self.markdown, width=width, height=height, sizing_mode=sizing_mode, scroll=True)

def _get_model(self, *args, **kwds):
return self.pane._get_model(*args, **kwds)
Expand All @@ -96,10 +102,12 @@ def _info_pane_update_selection(self) -> None:
if len(self.selected) == 0:
self.markdown.object = self.placeholder_text
else:
substitution_dict = {
col: self.data[col].iloc[self.selected[-1]] for col in self.data.columns
}
self.markdown.object = self.markdown_template.format(**substitution_dict)
if time.perf_counter() * 1000.0 - self._last_update > self.throttle:
substitution_dict = {
col: self.data[col].iloc[self.selected[-1]] for col in self.data.columns
}
self.markdown.object = self.markdown_template.format(**substitution_dict)
self._last_update = time.perf_counter() * 1000.0

def link_to_plot(self, plot):
"""Link this pane to a plot pane using a default set of params that can sensibly be linked.
Expand All @@ -114,4 +122,4 @@ def link_to_plot(self, plot):
link:
The link object.
"""
return self.link(plot, selected="selected", bidirectional=True)
return plot.link(self, selected="selected", bidirectional=False)
Loading

0 comments on commit 58771f2

Please # to comment.