Skip to content

Commit

Permalink
Merge pull request #42 from scipp/really_stop_double_figures
Browse files Browse the repository at this point in the history
Really stop double figures
  • Loading branch information
nvaytet authored Oct 13, 2022
2 parents f2275b2 + 168be20 commit b6fd471
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
4 changes: 0 additions & 4 deletions src/plopp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
except importlib.metadata.PackageNotFoundError:
__version__ = "0.0.0"

import matplotlib.pyplot as plt

plt.ioff()

from .core import Node, node, input_node, widget_node, show_graph, View
from .functions import figure, plot, slicer, inspector, scatter3d, superplot

Expand Down
19 changes: 1 addition & 18 deletions src/plopp/graphics/fig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,15 @@

from ..core.utils import number_to_variable, name_with_unit
from ..core import View
from .io import fig_to_bytes
from .utils import fig_to_bytes, silent_mpl_figure
from .mesh import Mesh
from .line import Line

from contextlib import contextmanager
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipp import DataArray, to_unit
from typing import Any, Tuple


@contextmanager
def silent_mpl_figure():
"""
Context manager to prevent automatic generation of figures in Jupyter.
"""
backend_ = mpl.get_backend()
revert = False
if 'inline' in backend_:
mpl.use("Agg")
revert = True
yield
if revert:
mpl.use(backend_)


class Figure(View):

def __init__(self,
Expand Down
14 changes: 0 additions & 14 deletions src/plopp/graphics/io.py

This file was deleted.

5 changes: 3 additions & 2 deletions src/plopp/graphics/point_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ..core.limits import find_limits, fix_empty_range
from ..core.utils import name_with_unit
from .color_mapper import ColorMapper
from .io import fig_to_bytes
from .utils import fig_to_bytes, silent_mpl_figure
from ..widgets import ToggleTool

import numpy as np
Expand Down Expand Up @@ -90,7 +90,8 @@ def _set_points_colors(self):
def _update_colorbar(self):
dpi = 96
height_inches = 0.89 * self._figsize[1] / dpi
cbar_fig = plt.figure(figsize=(height_inches * 0.2, height_inches), dpi=dpi)
with silent_mpl_figure():
cbar_fig = plt.figure(figsize=(height_inches * 0.2, height_inches), dpi=dpi)
cbar_ax = cbar_fig.add_axes([0.05, 0.02, 0.25, 1.0])
_ = ColorbarBase(cbar_ax,
cmap=self.color_mapper.cmap,
Expand Down
2 changes: 1 addition & 1 deletion src/plopp/graphics/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .fig import Figure
from .io import fig_to_bytes
from .utils import fig_to_bytes


class StaticFig(Figure):
Expand Down
32 changes: 32 additions & 0 deletions src/plopp/graphics/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from contextlib import contextmanager
from io import BytesIO
import matplotlib as mpl


def fig_to_bytes(fig, form='png'):
"""
Convert a Matplotlib figure to png (default) or svg bytes.
"""
buf = BytesIO()
fig.savefig(buf, format=form, bbox_inches='tight')
buf.seek(0)
return buf.getvalue()


@contextmanager
def silent_mpl_figure():
"""
Context manager to prevent automatic generation of figures in Jupyter.
"""
backend_ = mpl.get_backend()
revert = False
if 'inline' in backend_:
mpl.use("Agg")
revert = True
with mpl.pyplot.ioff():
yield
if revert:
mpl.use(backend_)

0 comments on commit b6fd471

Please sign in to comment.