forked from ctorney/matplotlib-backend-sixel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatplotlib-backend-sixel.py
More file actions
51 lines (39 loc) · 1.64 KB
/
matplotlib-backend-sixel.py
File metadata and controls
51 lines (39 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# SPDX-License-Identifier: CC0-1.0
import sys
from tempfile import NamedTemporaryFile
from matplotlib import interactive, is_interactive
from matplotlib._pylab_helpers import Gcf
from matplotlib.backend_bases import FigureManagerBase, _Backend
from matplotlib.backends.backend_agg import FigureCanvasAgg
from sixel.converter import SixelConverter
# XXX heuristic for interactive repl
if sys.flags.interactive:
interactive(True)
class FigureManagerSixel(FigureManagerBase):
def show(self):
with NamedTemporaryFile() as temppng:
self.canvas.figure.savefig(temppng.name, bbox_inches="tight", format="png")
sixel_convert = SixelConverter(temppng)
sixel_convert.write(sys.stdout)
class FigureCanvasSixel(FigureCanvasAgg):
manager_class = FigureManagerSixel
@_Backend.export
class _BackendSixelAgg(_Backend):
FigureCanvas = FigureCanvasSixel
FigureManager = FigureManagerSixel
# Noop function instead of None signals that this is an "interactive" backend
def mainloop():
return
# XXX: `draw_if_interactive` isn't really intended for on-shot rendering.
# We run the risk of being called on a figure that isn't completely rendered yet,
# so we skip draw calls for figures that we detect as not being fully initialized yet.
# Our heuristic for that is the presence of axes on the figure.
@classmethod
def draw_if_interactive(cls):
manager = Gcf.get_active()
if is_interactive() and manager.canvas.figure.get_axes():
cls.show()
@classmethod
def show(cls, *args, **kwargs):
_Backend.show(*args, **kwargs)
Gcf.destroy_all()