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:

https://community.tibco.com/wiki/spotfire-tips-tricks-create-r-graphics-...

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

https://docs.tibco.com/pub/sfire-analyst/latest/doc/pdf/TIB_sfire-analys...

 

I have tried out the code:

import numpy as np
import matplotlib
matplotlib.use('Agg')

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 "data_function.py", line 358, in _write_outputs
    output.write(self.globals, self.debug)
  File "data_function.py", line 141, in write
    sbdf.export_data(globals_dict[self.name], self.file, default_column_name=self.name)
  File "sbdf.py", line 160, in export_data
    default_column_name)
  File "sbdf.py", line 243, in _export_columnize_data
    column_types = {default_column_name: _ValueTypeId.infer_from_type(list(obj), "list")}
  File "sbdf.py", 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 10.10.0.64.

Does anyone have any suggestions?

 

Thank you!

 

Login