Skip to content

Commit

Permalink
Merge pull request #98 from spotify/chris/fix_bokeh_legend
Browse files Browse the repository at this point in the history
Fix bokeh deprecated legend parameter
  • Loading branch information
cphalpert committed Nov 27, 2019
2 parents 895470e + d8653cc commit 62ca7fa
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Chartify
|status| |release| |python|

.. |status| image:: https://img.shields.io/badge/Status-Beta-blue.svg
.. |release| image:: https://img.shields.io/badge/Release-2.6.1-blue.svg
.. |release| image:: https://img.shields.io/badge/Release-2.7.0-blue.svg
.. |python| image:: https://img.shields.io/badge/Python-3.6-blue.svg

Chartify is a Python library that makes it easy for data scientists to create charts.
Expand Down
2 changes: 1 addition & 1 deletion chartify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

__author__ = """Chris Halpert"""
__email__ = 'chalpert@spotify.com'
__version__ = '2.6.1'
__version__ = '2.7.0'

_IPYTHON_INSTANCE = False

Expand Down
136 changes: 95 additions & 41 deletions chartify/_core/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ def _get_color_and_order(self,
)
return colors, color_order

@staticmethod
def _plot_with_legend(method, **kwargs):
"""Call plotting method with the associated kwargs.
Removes the legend parameter if it is set to None because
Bokeh breaks if None is passed to a legend parameter
"""
legend_label = kwargs.pop('legend_label', None)
legend_group = kwargs.pop('legend_group', None)

if legend_label is not None:
return method(**kwargs, legend_label=legend_label)
elif legend_group is not None:
return method(**kwargs, legend_group=legend_group)
else:
return method(**kwargs)

@staticmethod
def _cannonical_series_name(series_name):
if series_name is None:
Expand Down Expand Up @@ -356,18 +374,20 @@ def line(self,
color_value = str(
color_value) if color_value is not None else color_value

self._chart.figure.line(
self._plot_with_legend(
self._chart.figure.line,
legend_label=color_value,
x=x_column,
y=y_column,
source=source,
line_width=line_width,
color=color,
line_join=line_join,
line_cap=line_cap,
legend=color_value,
line_dash=line_dash,
alpha=alpha,
y_range_name=self._y_range_name)
y_range_name=self._y_range_name
)

# Set legend defaults if there are multiple series.
if color_column is not None:
Expand Down Expand Up @@ -432,13 +452,14 @@ def scatter(self,
color_value = str(
color_value) if color_value is not None else color_value

self._chart.figure.scatter(
self._plot_with_legend(
self._chart.figure.scatter,
legend_label=color_value,
x=x_column,
y=y_column,
size=size_column,
source=source,
fill_color=color,
legend=color_value,
marker=marker,
line_color=color,
alpha=alpha,
Expand Down Expand Up @@ -624,23 +645,28 @@ def area(self,
color_value) if color_value is not None else color_value

if vertical:
self._chart.figure.patch(
self._plot_with_legend(
self._chart.figure.patch,
legend_label=color_value,
x=x_column,
y=y_column,
alpha=alpha,
source=source,
legend=color_value,
color=color,
y_range_name=self._y_range_name)
y_range_name=self._y_range_name
)

else:
self._chart.figure.patch(
self._plot_with_legend(
self._chart.figure.patch,
legend_label=color_value,
x=y_column,
y=x_column,
alpha=alpha,
source=source,
legend=color_value,
color=color,
y_range_name=self._y_range_name)
y_range_name=self._y_range_name
)

# Set legend defaults if there are multiple series.
if color_column is not None:
Expand Down Expand Up @@ -753,18 +779,23 @@ def histogram(self,
color_value) if color_value is not None else color_value

if vertical:
self._chart.figure.quad(
self._plot_with_legend(
self._chart.figure.quad,
legend_label=color_value,
top='values',
bottom=0,
left='min_edge',
right='max_edge',
source=source,
fill_color=color,
line_color=color,
alpha=.3,
legend=color_value)
alpha=.3
)

else:
self._chart.figure.quad(
self._plot_with_legend(
self._chart.figure.quad,
legend_label=color_value,
top='max_edge',
bottom='min_edge',
left=0,
Expand All @@ -773,7 +804,7 @@ def histogram(self,
fill_color=color,
line_color=color,
alpha=.3,
legend=color_value)
)

# Set legend defaults if there are multiple series.
if color_column is not None:
Expand Down Expand Up @@ -1396,29 +1427,37 @@ def bar(self,

if color_column:
legend = bokeh.core.properties.field('color_column')
legend = 'color_column'
else:
legend = None

if vertical:
self._chart.figure.vbar(
self._plot_with_legend(
self._chart.figure.vbar,
legend_group=legend,
x='factors',
width=bar_width,
top=numeric_column,
bottom=0,
line_color='white',
source=source,
fill_color=colors,
legend=legend)
)

else:
self._chart.figure.hbar(

self._plot_with_legend(
self._chart.figure.hbar,
legend_group=legend,
y='factors',
height=bar_width,
right=numeric_column,
left=0,
line_color='white',
source=source,
fill_color=colors,
legend=legend)
)

# Set legend defaults if there are multiple series.
if color_column is not None:
self._chart.style._apply_settings('legend')
Expand Down Expand Up @@ -1670,26 +1709,32 @@ def bar_stacked(self,
""" % (stack_column))
stack_values = stack_order

legend = [bokeh.core.properties.value(str(x)) for x in stack_values]
legend = [str(value) for value in stack_values]

if vertical:
self._chart.figure.vbar_stack(
stack_values,
self._plot_with_legend(
self._chart.figure.vbar_stack,
legend_label=legend,
stackers=stack_values,
x='factors',
width=bar_width,
line_color='white',
source=source,
fill_color=colors,
legend=legend)
)

else:
self._chart.figure.hbar_stack(
stack_values,
self._plot_with_legend(
self._chart.figure.hbar_stack,
legend_label=legend,
stackers=stack_values,
y='factors',
height=bar_width,
line_color='white',
source=source,
fill_color=colors,
legend=legend)
)

self._chart.style._apply_settings('legend')
# Reverse order of vertical legends to ensure that the order
# is consistent with the stack order.
Expand Down Expand Up @@ -1751,7 +1796,7 @@ def lollipop(self,
numeric_column)

if color_column:
legend = bokeh.core.properties.field('color_column')
legend = 'color_column'
else:
legend = None

Expand All @@ -1764,15 +1809,19 @@ def lollipop(self,
line_width=2,
line_color=colors,
source=source)
self._chart.figure.circle(
'factors',
numeric_column,

self._plot_with_legend(
self._chart.figure.circle,
legend_group=legend,
x='factors',
y=numeric_column,
size=10,
fill_color=colors,
line_color=colors,
line_width=3,
source=source,
legend=legend)
)

else:
self._chart.figure.segment(
0,
Expand All @@ -1782,15 +1831,18 @@ def lollipop(self,
line_width=2,
line_color=colors,
source=source)
self._chart.figure.circle(
numeric_column,
'factors',

self._plot_with_legend(
self._chart.figure.circle,
legend_group=legend,
x=numeric_column,
y='factors',
size=10,
fill_color=colors,
line_color=colors,
line_width=3,
source=source,
legend=legend)
)

# Set legend defaults if there are multiple series.
if color_column is not None:
Expand Down Expand Up @@ -1868,22 +1920,23 @@ def parallel(self,
color_value = numeric_column
legend = None
else:
legend = bokeh.core.properties.value(str(color_value))
legend = str(color_value)

if vertical:
x_value, y_value = 'factors', str(color_value)
else:
y_value, x_value = 'factors', str(color_value)

self._chart.figure.line(
self._plot_with_legend(
self._chart.figure.line,
legend_label=legend,
x=x_value,
y=y_value,
source=source,
line_width=line_width,
color=color,
line_join=line_join,
line_cap=line_cap,
legend=legend,
line_dash=line_dash,
alpha=alpha)

Expand Down Expand Up @@ -1986,14 +2039,15 @@ def scatter(self,
else:
y_value, x_value = 'factors', numeric_column

self._chart.figure.scatter(
self._plot_with_legend(
self._chart.figure.scatter,
legend_label=legend,
x=x_value,
y=y_value,
size=size_column,
fill_color=color,
line_color=color,
source=source,
legend=legend,
marker=marker,
alpha=alpha
)
Expand Down

0 comments on commit 62ca7fa

Please sign in to comment.