Skip to content

Commit 109fc05

Browse files
authored
Merge pull request #180 from simvue-io/179-simplify-saving-of-matplotlib-plots
Simplify saving of matplotlib plots
2 parents 066d4b2 + 94e612b commit 109fc05

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

simvue/serialization.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ def get_serializer(data, allow_pickle):
4949
return _serialize_dataframe
5050
elif _is_torch_tensor(data):
5151
return _serialize_torch_tensor
52-
elif allow_pickle:
52+
elif module_name == 'builtins' and class_name == 'module' and not allow_pickle:
53+
try:
54+
import matplotlib
55+
if data == matplotlib.pyplot:
56+
return _serialize_matplotlib
57+
except ImportError:
58+
pass
59+
60+
if allow_pickle:
5361
return _serialize_pickle
5462
return None
5563

@@ -63,6 +71,16 @@ def _serialize_plotly_figure(data):
6371
data = plotly.io.to_json(data, 'json')
6472
return data, mimetype
6573

74+
@check_extra("plot")
75+
def _serialize_matplotlib(data):
76+
try:
77+
import plotly
78+
except ImportError:
79+
return None
80+
mimetype = 'application/vnd.plotly.v1+json'
81+
data = plotly.io.to_json(plotly.tools.mpl_to_plotly(data.gcf()), 'json')
82+
return data, mimetype
83+
6684
@check_extra("plot")
6785
def _serialize_matplotlib_figure(data):
6886
try:

0 commit comments

Comments
 (0)