How to export a matplotlib plot from a Python data function to Spotfire as an image?

Dear Community

I am running an analysis in a Python data function that leads to a complicated graph which I would not be able to plot using Spotfire's built-in visualizations.

I would like to create the plot within Python (using matplotlib) and display it Spotfire. In the past I have done this with R data functions following this tutorial:

The Spotfire Python Data Functions guide says that matplotlib figure objects can be return to Spotfire from Python:


I have tried out the code:

import numpy as np
import matplotlib

import matplotlib.pyplot as plt

ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
w = plt.plot(x, y)


where w is my output variable (Document Property, Binary), but that fails with the following error message:


Could not execute function call 'Untitled'

Error executing Python script:

spotfire.sbdf.SBDFError: unknown type 'matplotlib.lines.Line2D' in list

Traceback (most recent call last):
  File "", line 358, in _write_outputs
    output.write(self.globals, self.debug)
  File "", line 141, in write
    sbdf.export_data(globals_dict[], self.file,
  File "", line 160, in export_data
  File "", line 243, in _export_columnize_data
    column_types = {default_column_name: _ValueTypeId.infer_from_type(list(obj), "list")}
  File "", line 1007, in infer_from_type
    raise SBDFError("unknown type '%s' in %s" % (_utils.type_name(vals_type), value_description))

   at Spotfire.Dxp.Data.DataFunctions.Executors.LocalPythonFunctionClient.<RunFunction>d__8.MoveNext()
   at Spotfire.Dxp.Data.DataFunctions.Executors.PythonScriptExecutor.<ExecuteFunction>d__11.MoveNext()
   at Spotfire.Dxp.Data.DataFunctions.DataFunctionExecutorService.<ExecuteFunction>d__8.MoveNext()


The Spotfire build I am using is

Does anyone have any suggestions?


Thank you!