### The Plotly component

Visualize data with the Plotly component. The Plotly implementations for the MATLAB and Python versions of Simian GUI differ significantly, so they are documented in separate subsections below. It is possible to combine multiple types of plots in the same figure, which is demonstrated with an example.

In addition to the properties and methods listed below, this component inherits properties and methods from the superclass Component. For example, any Plotly component has a label and hidden property even though these are not explicitly listed here.

#### MATLAB

The Plotly component creates a Plotly figure in which data can be visualized.

##### Properties
NameDescriptionDatatypeDefault
defaultValueDefault settings for the Plotly figure. This will be an object of type utils​.Plotly (described below) by default. An example on how to use this property to create an initial plot is given below.utils​.Plotlyutils​.Plotly
This hides the Plotly logo in the toolbar at the top.
##### utils.Plotly

The utils.Plotly class provides methods that resemble MATLAB plotting functions with which data can be presented in several chart types. When the Plotly utils class object is put in the payload and returned to the form, the created plot is shown in the form. Since the data to plot is often calculated based on certain inputs, the Plotly utils class object is often added in the guiEvent function, as illustrated by the example below the properties and methods described next.

###### utils.Plotly properties
NameDescriptionDatatypeDefault
configAdditional configuration of the plot such as toggling responsiveness or zooming.Structstruct("displaylogo", false)
dataData to be shown in the figure.Cell{}
layoutLayout to be used in the figureStructstruct()
###### utils.Plotly methods
NameSyntaxDescription
areaobj.area(varargin)Add a series to an area plot.
barobj.bar(varargin)Add a series to a bar plot.
boxplotobj.boxplot(varargin)Add a box to a box plot.
bubblechartobj.bubblechart(varargin)Add bubbles to the bubble chart.
contourobj.contour(varargin)Add a series to a contour plot.
clfobj.clf(keepLayout)Clear the plot. Use obj.clf(true) to keep the title, legend, etc. and only reset the data and config properties.
legendobj.legend(varargin)Add a legend to a plot.
loglogobj.loglog(varargin)Add a series to a 2-D plot.
pieobj.pie(varargin)Add a series to a pie chart.
plotobj.plot(varargin)Add a series to a 2-D plot.
plot3obj.plot3(varargin)Add a series to a 3-D plot.
polarplotobj.polarplot(varargin)Add a series to a 2-D polarplot.
semilogxobj.semilogx(varargin)Add a series to a 2-D plot.
semilogyobj.semilogy(varargin)Add a series to a 2-D plot.
surfobj.surf(varargin)Add a series to a surface plot
titleobj.title(varargin)Add a title to a plot.
xlabelobj.xlabel(varargin)Add a xlabel to a plot.
ylabelobj.ylabel(varargin)Add a ylabel to a plot.
##### Usage

Below is an example illustrating the MATLAB plotly functionality. You can call multiple plotting functions on the same Plotly object to plot multiple things in the same figure. The resulting plot is shown below the code.

function payload = guiInit(metaData)
form            = Form();
component.Plotly("my_plot", form);

% When this button is clicked, the data is plotted.
btn         = component.Button("plot_data", form);
btn.label   = "Plot data";
btn.setEvent("PlotData");
end

function payload = guiEvent(metaData, payload)
plotObj.clf();

% Add a line plot and a bar chart in the same figure.
nPoints = 4;
plotObj.bar(1:nPoints, rand(nPoints, 2));
plotObj.plot(1:nPoints, rand(nPoints, 1), "LineWidth", 3);

% Add peripherals such as labels etc.
plotObj.title("Recent efficiency compared to average efficiency");
plotObj.xlabel("N");
plotObj.ylabel("Efficiency [-]");
plotObj.legend("This year", "Last year", "Average last ten years");

end


The same plot can also be achieved during initialization by calling the methods above on the object in the defaultValue property of the Plotly component object:

function payload = guiInit(metaData)
form            = Form();

% Add a Plotly component and add a line plot and a bar chart in the same figure.
comp    = component.Plotly("plot", form);
nPoints = 4;
comp.defaultValue.bar(1:nPoints, rand(nPoints, 2));
comp.defaultValue.plot(1:nPoints, rand(nPoints, 1), "LineWidth", 3);

% Add peripherals such as labels etc.
comp.defaultValue.title("Recent efficiency compared to average efficiency");
comp.defaultValue.xlabel("N");
comp.defaultValue.ylabel("Efficiency [-]");
comp.defaultValue.legend("This year", "Last year", "Average last ten years");

% When this button is clicked, the data is plotted.
btn         = component.Button("plot_data", form);
btn.label   = "Plot data";
btn.setEvent("PlotData");
end


In addition to the chart types available through the methods of the Plotly class, you can use all functionality from the Python Plotly library if you set the submission data such that it can be interpreted as a Plotly component correctly. For example, you can change the font-size of the ticks on the x-axis using plotObj.layout.xaxis.tickfont.size = 10;.

#### Python

The Plotly functionality in the Python version of Simian GUI differs significantly from the MATLAB implementation as the Plotly library is directly available from Python, which can be used to visualize data in the form. Most functionality from the Python Plotly API can be used (animations and controls are not supported) when a Plotly Figure object is put in the Plotly object's figure property.

The Plotly component creates a Plotly figure in which data can be visualized.

##### Properties
NameDescriptionDatatypeDefault
configAdditional configuration of the plot such as toggling responsiveness or zooming.Dict{"displaylogo": False}
dataData to be shown in the figure.Listlist()
figurePlotly Figure object containing the figure to be shown.FigureNone
layoutLayout to be used in the figureDictdict()
##### utils.Plotly

This utility class should be used in guiEvent, not in guiInit. The utils.getSubmissionData function returns a utils.Plotly object containing the payload data for the Plotly component. When the Plotly utils class object is put in the payload and returned to the form, the created plot is shown in the form.

###### utils.Plotly properties
NameDescriptionDatatypeDefault
configAdditional configuration of the plot such as toggling responsiveness or zooming.Dict{"displaylogo": False}
dataData to be shown in the figure.Listlist()
figurePlotly Figure object containing the figure to be shown.FigureNone
layoutLayout to be used in the figureDictdict()
###### utils.Plotly methods
NameSyntaxDescription
preparePayloadValueplot_dict = preparePayloadValue(​self)Prepares a dict representation of the figure in the Plotly component, which can be put in the payload without using setSubmissionData.
##### Usage

Below is an example illustrating the Python plotly functionality with an initial bar plot that is extended with an extra line when the guiEvent function is executed. Note that in Python the Plotly Express functions are available to allow for easier creation and modification of plotly figures.

import plotly.express as px
import plotly.graph_objects as go

def gui_init(meta_data):
app_form = Form()
plot_obj = component.Plotly("plot", app_form)
plot_obj.figure = go.Figure(
go.Bar(
x=[1, 2, 3, 4, 5],
y=[0.81, 0.58, 0.25, 0.45, 0.41],
name="This year",
)
)
plot_obj.figure.update_layout(
title="This year's efficiency compared to the average efficiency",
xaxis_title="N",
yaxis_title="Efficiency [-]",
autosize=False,
width=600,
height=400,
)

# When this button is clicked, the data is plotted.
btn = component.Button("plot_data", app_form)
btn.label = "Plot data"
btn.setEvent("PlotData")

return {"form": app_form}

x=[1, 2, 3, 4, 5],
y=[0.35, 0.95, 0.32, 0.54, 0.23],
name="Average last<br>ten years",
)

# Update the payload with the new values in the Plotly object.



The form generated with the above code will look like this:

#### Resizing

Please note that the Plotly component spans the entire width of its parent component, even if the plot itself is smaller. This means that if you set layout.width and layout.height, the plot itself becomes smaller but the containing component does not, resulting in subsequent components ending up way below the plot. In order to mitigate this, you can reduce the width of the component by placing it in a Columns component, like so:

function payload = guiInit(metaData)
form            = Form();