Skip to content

Commit 965da78

Browse files
committed
Pull request #289: add backend
Merge in LCL/wolframclientforpython from feature/matplotlib to master * commit 'be25946cfec47e308803a69794b3f6102a6c49f2': move backend location add backend
2 parents 3c57184 + be25946 commit 965da78

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎wolframclient/language/hooks/__init__.py‎

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Custom matplotlib backend that sends figure image bytes to Wolfram via wl_print.
3+
4+
Usage:
5+
import matplotlib
6+
matplotlib.use('module://backend_hello')
7+
import matplotlib.pyplot as plt
8+
9+
plt.plot([1, 2, 3], [4, 5, 6])
10+
plt.show()
11+
"""
12+
13+
import io
14+
from matplotlib.backend_bases import FigureCanvasBase, FigureManagerBase
15+
from matplotlib.backends.backend_agg import FigureCanvasAgg
16+
from matplotlib._pylab_helpers import Gcf
17+
from wolframclient.language.side_effects import wl_print
18+
from wolframclient.language import wl, wlexpr
19+
20+
21+
class FigureCanvas(FigureCanvasAgg):
22+
manager_class = type(None) # placeholder, overridden below
23+
24+
25+
class FigureManager(FigureManagerBase):
26+
27+
@classmethod
28+
def pyplot_show(cls, *, block=None):
29+
for manager in Gcf.get_all_fig_managers():
30+
manager.show()
31+
32+
def show(self):
33+
buf = io.BytesIO()
34+
self.canvas.figure.savefig(buf, format="png")
35+
wl_print(wl.ImportByteArray(buf.getvalue(), "PNG"))
36+
37+
38+
FigureCanvas.manager_class = FigureManager

0 commit comments

Comments
 (0)