Artur Veloso Posted June 19, 2021 Share Posted June 19, 2021 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.spotfire.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.d__8.MoveNext() at Spotfire.Dxp.Data.DataFunctions.Executors.PythonScriptExecutor.d__11.MoveNext() at Spotfire.Dxp.Data.DataFunctions.DataFunctionExecutorService.d__8.MoveNext() TheSpotfire build I am using is 10.10.0.64. Does anyone have any suggestions Thank you! Link to comment Share on other sites More sharing options...
Floris Remmen Posted November 15, 2021 Share Posted November 15, 2021 Hi The function needs to return a figure object. If you instantiate a figure like so: ``` import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt f, ax = plt.subplots() x = np.linspace(0, 10) y = np.exp(x) w = ax.plot(x, y) ``` and you return the 'f' object into a document property it works. Link to comment Share on other sites More sharing options...
Osama Alsalmi Posted November 21, 2021 Share Posted November 21, 2021 use these two functions: import os import matplotlib.pyplot as plt import tempfile import bitstring from PIL import Image import numpy as np def _image_file_to_binary(file): bits = bitstring.BitArray(filename=file.name) img = bits.bytes os.unlink(file.name) return img def _pyplot_to_binary(fig): fig.set_canvas(plt.gcf().canvas) file = tempfile.NamedTemporaryFile(delete=False) fig.savefig(file) file.close() return _image_file_to_binary(file)#Example output_binary_document_prop = _pyplot_to_binary()Finally, to view this make sure you to input this as a lable in the text area. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now