Skip to content

Commit

Permalink
Merge pull request #336 from telamonian/matplotlib-metadata-needs-bac…
Browse files Browse the repository at this point in the history
…kground

Adds metadata to help display matplotlib figures legibly
  • Loading branch information
blink1073 authored Aug 29, 2018
2 parents 4424ef4 + c4bb66a commit 3a24c2b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ python:
- 3.6
- 3.5
- 3.4
- 3.3
- 2.7
sudo: false
install:
Expand Down
37 changes: 34 additions & 3 deletions ipykernel/pylab/backend_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import matplotlib
from matplotlib.backends.backend_agg import new_figure_manager, FigureCanvasAgg # analysis: ignore
from matplotlib import colors
from matplotlib._pylab_helpers import Gcf

from IPython.core.getipython import get_ipython
Expand All @@ -33,7 +34,10 @@ def show(close=None, block=None):
close = InlineBackend.instance().close_figures
try:
for figure_manager in Gcf.get_all_fig_managers():
display(figure_manager.canvas.figure)
display(
figure_manager.canvas.figure,
metadata=_fetch_figure_metadata(figure_manager.canvas.figure)
)
finally:
show._to_draw = []
# only call close('all') if any to close
Expand Down Expand Up @@ -72,7 +76,7 @@ def draw_if_interactive():

if not hasattr(fig, 'show'):
# Queue up `fig` for display
fig.show = lambda *a: display(fig)
fig.show = lambda *a: display(fig, metadata=_fetch_figure_metadata(fig))

# If matplotlib was manually set to non-interactive mode, this function
# should be a no-op (otherwise we'll generate duplicate plots, since a user
Expand Down Expand Up @@ -124,7 +128,7 @@ def flush_figures():
active = set([fm.canvas.figure for fm in Gcf.get_all_fig_managers()])
for fig in [ fig for fig in show._to_draw if fig in active ]:
try:
display(fig)
display(fig, metadata=_fetch_figure_metadata(fig))
except Exception as e:
# safely show traceback if in IPython, else raise
ip = get_ipython()
Expand Down Expand Up @@ -163,3 +167,30 @@ def configure_once(*args):
ip.events.register('post_run_cell', configure_once)

_enable_matplotlib_integration()

def _fetch_figure_metadata(fig):
"""Get some metadata to help with displaying a figure."""
# determine if a background is needed for legibility
if _is_transparent(fig.get_facecolor()):
# the background is transparent
ticksLight = _is_light([label.get_color()
for axes in fig.axes
for axis in (axes.xaxis, axes.yaxis)
for label in axis.get_ticklabels()])
if ticksLight.size and (ticksLight == ticksLight[0]).all():
# there are one or more tick labels, all with the same lightness
return {'needs_background': 'dark' if ticksLight[0] else 'light'}

return None

def _is_light(color):
"""Determines if a color (or each of a sequence of colors) is light (as
opposed to dark). Based on ITU BT.601 luminance formula (see
https://stackoverflow.com/a/596241)."""
rgbaArr = colors.to_rgba_array(color)
return rgbaArr[:,:3].dot((.299, .587, .114)) > .5

def _is_transparent(color):
"""Determine transparency from alpha."""
rgba = colors.to_rgba(color)
return rgba[3] < .5
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import sys

v = sys.version_info
if v[:2] < (2,7) or (v[0] >= 3 and v[:2] < (3,3)):
error = "ERROR: %s requires Python version 2.7 or 3.3 or above." % name
if v[:2] < (2,7) or (v[0] >= 3 and v[:2] < (3,4)):
error = "ERROR: %s requires Python version 2.7 or 3.4 or above." % name
print(error, file=sys.stderr)
sys.exit(1)

Expand Down Expand Up @@ -101,12 +101,13 @@
glob(pjoin('data_kernelspec', '*'))),
]


setuptools_args['python_requires'] = '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'

extras_require = setuptools_args['extras_require'] = {
'test:python_version=="2.7"': ['mock'],
# pytest 3.3 doesn't work on Python 3.3
'test:python_version=="3.3"': ['pytest==3.2.*'],
'test:python_version!="3.3"': ['pytest>=3.2'],
'test': [
'pytest',
'pytest-cov',
'nose', # nose because there are still a few nose.tools imports hanging around
],
Expand Down

0 comments on commit 3a24c2b

Please sign in to comment.