Pltf Plots Python API
Pltf Plots Python API
User's guide
Simcenter Amesim 2310
Unpublished work. © 2023 Siemens
This Documentation contains trade secrets or otherwise confidential information owned by Siemens Industry Software Inc. or
its affiliates (collectively, “Siemens”), or its licensors. Access to and use of this Documentation is strictly limited as set forth in
Customer’s applicable agreement(s) with Siemens. This Documentation may not be copied, distributed, or otherwise disclosed
by Customer without the express written permission of Siemens, and may not be used in any way not expressly authorized by
Siemens.
This Documentation is for information and instruction purposes. Siemens reserves the right to make changes in specifications
and other information contained in this Documentation without prior notice, and the reader should, in all cases, consult
Siemens to determine whether any changes have been made.
No representation or other affirmation of fact contained in this Documentation shall be deemed to be a warranty or give rise to
any liability of Siemens whatsoever.
If you have a signed license agreement with Siemens for the product with which this Documentation will be used, your use of
this Documentation is subject to the scope of license and the software protection and security provisions of that agreement.
If you do not have such a signed license agreement, your use is subject to the Siemens Universal Customer Agreement, which
may be viewed at [Link] as supplemented by the product specific terms
which may be viewed at [Link]
SIEMENS MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THIS DOCUMENTATION INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT OF
INTELLECTUAL PROPERTY. SIEMENS SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL OR
PUNITIVE DAMAGES, LOST DATA OR PROFITS, EVEN IF SUCH DAMAGES WERE FORESEEABLE, ARISING OUT OF OR RELATED
TO THIS DOCUMENTATION OR THE INFORMATION CONTAINED IN IT, EVEN IF SIEMENS HAS BEEN ADVISED OF THE POSSIBILITY
OF SUCH DAMAGES.
TRADEMARKS: The trademarks, logos, and service marks (collectively, "Marks") used herein are the property of Siemens or other
parties. No one is permitted to use these Marks without the prior written consent of Siemens or the owner of the Marks,
as applicable. The use herein of third party Marks is not an attempt to indicate Siemens as a source of a product, but is
intended to indicate a product from, or associated with, a particular third party. A list of Siemens’ Marks may be viewed at:
[Link]/global/en/legal/[Link]. The registered trademark Linux® is used pursuant to a
sublicense from LMI, the exclusive licensee of Linus Torvalds, owner of the mark on a world-wide basis.
See here for e-mail addresses for your local office: Sales, pricing and general
information
[Link]
about_us/contact/[Link]#lms
Introduction 1-1
Getting started
Setting the environment ─────────────────────── 2-1
Plot a basic line graph ──────────────────────── 2-4
Simple examples
Plot a bar graph ─────────────────────────── 4-1
Plot a spider graph ────────────────────────── 4-6
Advanced examples
The Plot API demo ────────────────────────── 5-1
Create a plot in an App ──────────────────────── 5-4
Index Index-1
These functions are very useful when you want to generate a report from:
• Scripts that will plot the results of analysis performed by a series of automated tasks
• Apps that will display graphs directly in the Apps graphical interface
This manual explains how the API is structured. It describes the basic steps that make it possible to call
the Simcenter Amesim Plot from lines of code through the Python language.
You are advised to consult the Plots Python API References for the complete list of the available
functions with a short description for each. Through these functions you will be able to go further in
customizing graphs.
The following tables classify and illustrate the kinds of plots you can create with the Plots Python API.
They include lines, bars, density, maps, and pie graphs.
Parallel
1D line Bar Pie coordinates Spider
Horizontal lines Vertical bars Pie chart Horizontal lines Spider lines
Vertical lines Horizontal bars 3D Pie chart Vertical lines Spider areas
Vertical stacked
bars
Horizontal
stacked bars
First, check your AME environment variable. It must contain the absolute path to the root Simcenter
Amesim directory.
• under Windows: the %PATH% environment variable must contain at least %AME% and %AME%
\win32.
• under Linux: the $PATH environment variable must contain at least $AME and your
$LD_LIBRARY_PATH must contain at least $AME/lnx.
Note:
A complete Python 3.8.8 distribution including the Scientific Python toolbox is supplied with
Simcenter Amesim. It includes four libraries:
• Numpy 1.19.5
• Scipy 1.6
• Matplotlib 3.3.3
• PySide2 5.15.2
Note:
It is recommended to have some knowledge on the Python language. The official Python
documentation is available through the Simcenter Amesim platform manuals:
• Python Tutorial
import amepyplot
Note:
Several classes and functions contain references to PySide2, the Python binding for the Qt C++
framework.
Depending on the context of your script, the display of your graphs in a Simcenter Amesim plot
window can differ. Three cases occur when scripts are used to interact with Simcenter Amesim.
Standalone script
This kind of script is particularly designed for automating tasks such as setting or getting parameter
values, running temporal simulations or linear analysis, and post-processing result variables from
outside Simcenter Amesim. Simcenter Amesim does not need to be open to interact with it, this
is typically the case when using the Simulation scripting facility or the Simcenter Amesim API.
In that case you need to create a Qt Application that allows you to manage the plot widget.
Note:
The main event loop receives events from the window system and dispatches these to the
application widgets.
Below is a template of how the code needs to be structured to display your plot in an Simcenter
Amesim plot window.
import sys
import math
import amepyplot
from PySide2 import QtWidgets
[Link]()
Python tools
In Simcenter Amesim the Python tools are located at system level or component level. These tools
call Python scripts to interact with an open model within Simcenter Amesim. They can be launched
from the script analysis tool, the script assistant component (SCRCALL01 submodel), and the editable
parameters group facility.
In this case the plot window is attached to the main Simcenter Amesim window.
Below is a template of how the code needs to be structured to display your plot in an Simcenter
Amesim plot window.
import sys
import math
import amepyplot
from PySide2 import QtWidgets
import apps
[Link]()
Apps
In the App Designer there is a dedicated widget to integrate a plot within your Graphical User Interface.
When you insert this widget in your UI the associated code is automatically generated. You just need to
attach your graph to this widget through its name.
Below is a template of how the code needs to be structured to display your plot in an Simcenter
Amesim plot widget.
import math
import amepyplot
...
#>>>>>>> Extra initialization declarations here
When using the Simulation scripting functions, you can run a simulation, then load the results and get
specific variables. This is typically done through the functions amerunsingle, ameloadt, and amegetvar.
The following piece of Python code illustrates the use of these functions:
Here the time and the shaft angle are the two variables that are retrieved from the simulation results.
You are going to plot the 2D curve of the rotangleval as a function of timeval.
This file is a new module where you can define different built-in graph display functions. In this
example you only define the 2D line graph.
The function is named plot2D and has as arguments the x values, y values, x label, and y label.
import amepyplot
def plot2D(x,y,xlabel,ylabel):
2. In the function plot2D you first create the plot widget PlotWidget(), set the plot window size using
the function resize(), and access the generated graph using firstGraph().
Note:
Here there is only one graph on the plot window, however you can define a layout that
contains several graphs on the same plot window.
import amepyplot
def plot2D(x,y,xlabel,ylabel):
plot = [Link]()
[Link](800, 550)
graph = [Link]()
3. Next, you need to create the x and y items of the curve (using the Item object) and create the 2D
curve that will be plotted (Curve2D).
Note:
The unit is already defined in the name of the variable, that's why the unit argument of the
object Item is empty.
import amepyplot
4. The final step to fully define the function plot2D is to attach the curve to the graph and display the
widget.
import amepyplot
[Link](curve)
[Link]()
return plot
5. Since the function to plot a 2D line graph is completed you just need to call it from your main
script. Remember that you need to import the new module ameplot.
...
import ameplot
from PySide2 import QtWidgets
...
...
app = [Link]()
if app is None:
app = [Link]([])
Using the Plots Python API you are able to display the results that are calculated from a standalone
scripts. You can create the following curve for instance:
Note:
The function plot2D returns the plotWidget; you then have access to the graph for further
customization.
This is the first object you need to create in any script to be able to use the API.
How to create
widget = [Link]()
Note:
The full documentation of the plot widget object can be found in the Plots Python API Reference
It can be displayed as a top level window or placed inside other widgets. To put it inside another widget
(we will call it A), you need to specify that A is the parent of the plot widget. To do this, you can indicate
the parent at the creation of the widget like in this example:
# Creation of a dialog
A = [Link]()
When the plot widget is a child of another widget, you need to use a layout to specify the behavior of
the plot widget inside its parent. For example, using a vertical box layout like in the example below, you
specify that all the children of the dialog A will be placed one above the other, and every widget will use
all the space available horizontally:
Once you have performed these steps, you can display the top level window (whether it is a parent
dialog or the plot widget) using the method show. Note that you can also specify the required size:
The dialog will be shown when the QApplication will be executed with the line:
Note:
Inside an App, you can create the plot widget using the App designer (see the advanced example
Create a plot in an App).
• The graphs
• Create graph(s). For example, to add a graph in a second row, proceed as follows:
• Create a picture of the widget (method toPixmap). Here is an example of how to save this image on
your computer:
• Update all the graphs (can be useful after the modification of data)
• Manage interactions allowed on the graph. By default, the plot widget doesn't allow any
modification. If you want to allow some modification, use the method setDefaultInteractions like
in this example:
A simple 2D curve is composed of 2 vectors of values: one for the x values, and the other for the y
values. The two vectors must have the same size. Use the Item object to store the x and y values.
Note:
You can store the unit and the title of the data in the item. This information is displayed in the
graph (the title of the y item is used as the default title for a 2D curve).
If you retrieve the data from the scripting API, you can use this piece of code to create the items:
Note:
The full documentation of the Item object can be found in the Plots Python API Reference.
Create a curve
Once the Item objects are created, you can create the curve. Note that you are able to choose between
several types of curve depending on the data you want to visualize:
• Curve1DDiscrete can be used when the x data are not continuous but discrete (like a list of runs or a
list of criteria).
• Curve2D is the classic 2D curve. The x and y data are assumed to be continuous.
• Several types of 3D curves like CurveM1D, CurveMatrix or CurveXYZ. These kinds of curve can be
displayed in a 2D graph or a 3D graph.
# Create a 2D curve
curve = amepyplot.Curve2D(itemX, itemY)
Once the curve is created, you need to add it to a graph. You can access to the graph using the methods
available on the plot widget. Here is an example that adds the curve to the first graph of the plot widget:
This customization can be carried out on the graph type (2D, 3D, Pie chart...), on the displayed texts
(graph title, axis title, legend...), or on the axes (colors, unit text, tick marks...). This section shows how
to do this customization.
Graph type
The basic customization is to choose the type of graph you want to use. In the Graph documentation,
you can find a list of functions (called Graph type functions) that allow you to modify the graph type.
Note:
The Graph type functions section also contains some functions dedicated to a specific graph type.
Check the documentation to know if a function can be used on your type of graph.
When a graph is created, the default type is 2D. You can modify the type using the convertTo...functions
on the Graph object.
Note:
You can change the graph type before or after adding the curves. Neverthless, the graph type
needs to support the curves type.
You can display some predefined texts and indicators on the graph. The visibility of these objects is
mainly managed by the graph and can be modified using the function configureTitles.
Note:
The axis unit visibility is managed by the Axis object.
The Legend object can use several positions (using predefined anchors) that you can manage using the
same function.
Axis customization
You can customize the axes of a graph. To do this, first, access the Axis object through the graph, for
example:
axis = [Link]()
Then you are able to customize almost all the properties of the axis:
• Visibility
• Unit visibility
• Graph grid (the grid drawn on the graph area, according to axis ticks)
• Range
• Tick-marks
• ...
Note:
See the documentation of the Axis object for further information. You can also consult the Plot
API App that contains examples of axis customization (see the associated documentation).
Each curve has a Display object associated with it. This object specifies how the curve is drawn.
The first step is to select the type of display that you want to use. Each type of curve supports some
specific displays. To know the displays that can be used for your curve, consult the documentation
of the corresponding curve object. For example, the Curve2D supports the Display2DLine and the
DisplayDensity (see Curve2D).
At the creation of the curve, you can specify the display you want to use. If you do not specifiy, a default
display is used. For example, the Curve2D uses a Display2DLine by default:
Note:
The two curve creations in the above code example are equivalent.
Note:
You can consult the documentation of the object CurveDisplay to see examples of rendering for all
the available displays.
To know what kind of display is used for a specific curve, you can do the following:
The result for the previous curve with density display is:
Display customization
Once you have choosen a display type, you will be able to customize it to get the exact rendering you
want.
Here is an example where we want to draw a 2D curve using a blue line with a thickness of 2 pixels and
a dashed line style. Furthermore, we want to display a red cross symbol on each point:
# Creation of the display: we can set many parameters directly at the creation
display = amepyplot.Display2DLine([Link], [Link], 2)
Here is another example of a CurveM1D. We want to display it in a 3D graph with light, using a cold
colorscale with light blue isolines (without iso-values):
Note:
You can find many examples of display customization in the Plot API App. You can consult the
associated documentation
The data values are stored in the Item objects. To modify these values, you first need to access the
corresponding items. Here is an example on a 2D curve:
xItem = [Link]()
yItem = [Link]()
The new values can come from a new simulation, a new computation or something else. Note that the
size of the new values must be coherent. That means for example that to be used in a 2D curve, the x
values and the y values must have the same size.
[Link](xNewValues)
[Link](yNewValues)
Once the values have been updated, you need the graph to be redrawn. This can be done for a single
graph like this:
[Link]()
[Link]()
The base class for all the annotation objects is GraphObject. You can consult the reference
documentation that presents a hierarchy diagram of the available objects that inherit from this class.
• Linked to a graph: a GraphObject is attached to a specific graph. That is why all these objects need a
parent graph to be created.
• Set a position: you can set the position in the graph coordinates using the function setModelPosition.
If an object is placed like this, it will move according to a user zoom / pan / rotate.
• Manipulation: among all the GraphObject classes, there are two main categories: the manipulable
one (that uses the base class GraphManipulator) and the other one. The manipulable objects can be
used for annotation and their manipulation can be prevented using the function avoidManipulation.
• Associate information: you can add specific data on these objects using the method setUserData.
• Retrieve object: you can get an object from its parent graph using the previously associated data. To
do so, use the function findObject on the graph.
Here is an example of the creation of the "problem" text in the figure above:
# Customize arrow
[Link]([Link], [Link])
[Link](5)
Note:
The GraphText object can use graph anchors to have a "static" position in the drawing area.
The base class for all the manipulable objects is GraphManipulator. You can consult the reference
documentation that presents a hierarchy diagram of the available objects that inherit from this class.
You can also consult the section dedicated to annotations that presents the basic information about
these classes.
• Manipulation: you can specify the permitted manipulations using the function setAllowedMotions. It
will allow you, for example, to accept only the horizontal motion of a GraphPoint.
• Callbacks: the manipulators provide callbacks on user interactions. You can connect any of these
callbacks to associate your specific behavior. Here is an example where we want to change the curve
color to blue when the mainpulator is pressed:
# Set user data to be able to retrieve the manipulator from the graph
[Link]("color manipulator")
Note:
A manipulator has no default behavior. It means that you need to provide and connect a
working function that does the required behavior on the manipulation.
• Contextual menu: you can add contextual actions to a manipulator using the method addAction.
Note:
You can find an example of curve manipulation in the Plot API App. You can consult the reference
documentation
All the interactions with menus and toolbars are done through the MenusAndToolBars object. You can
get it from the plot widget like this:
Menus
To access to a specific menu, you need to know its name. Use the function allMenusNames to obtain the
names of all available menus. Then you can add an action like this:
# Add a separator
[Link]()
Toolbars
To access to a specific toolbar, you need to know its name. Use the function allToolBarsNames to obtain
the names of all available toolbars. Then you can add an action like this:
# Add a separator
[Link]()
When using the Simulation scripting functions, you can run a simulation, then load the results and get
specific variables. It is typically done through the functions amerunsingle, ameloadt, and amegetvar.
In this example it is assumed that 5 batch runs are simulated that correspond to different scenarios.
The following piece of Python code illustrates the use of these functions:
Here the variables energy_1, energy_2, energy_3, energy_4, and energy_5 contain all values during the
simulation time. You are going to plot a bar graph that shows the final values of all variables.
1. In the main script you first need to define the labels titles and the values that are used.
Note:
data_labels is the list of the titles of the bars and data is the list of the values.
This file is a new module where you can define different built-in graph display functions. In this
example you only define the bar graph.
The function is named plotBar and has as arguments the data and the labels.
import amepyplot
3. In the function plotBar you first create the plot widget using PlotWidget(), set the size of the plot
window using resize(), and access the graph using firstGraph().
Note:
Here there is only one graph on the plot window however you can define a layout that
contains several graphs in the same plot window.
import amepyplot
4. Next, you need to create the data items (using Item object) that gather the values, and the curve
that is represented by one set of discrete values: Curve1DDiscrete.
The values of each variable are displayed as labels and their positions are set to
[Link].
Note:
By default the DisplayBarchart is used for 1D discrete curves. Then dedicated functions can
be applied to the graph to set horizontal bars or stacked bars.
import amepyplot
# Create data
item = [Link](data)
# Create curve
curve = amepyplot.Curve1DDiscrete(labels, item)
[Link]().configure(label=[Link])
5. The final step to fully define the function plotBar is to attach the curve to the graph using the
function addCurve.
import amepyplot
# Create data
item = [Link](data)
# Create curve
curve = amepyplot.Curve1DDiscrete(labels, item)
[Link]().configure(label=[Link])
# Configure graph
[Link](showXAxisTitle=False, showTitle=False)
[Link]()
return plot
Note:
Some functions are used to configure the graph elements such as the title and the axes.
6. Since the function to plot a 2D line graph is complete you just need to call it from your main script.
Remember that you need to import the new module ameplot.
...
import ameplot
from PySide2 import QtWidgets
...
...
'Scenario 4',
'Scenario 5']
data = [energy_1[0][0][-1],
energy_2[0][0][-1],
energy_3[0][0][-1],
energy_4[0][0][-1],
energy_5[0][0][-1]]
Using the Plots Python API you are able to display the results that are calculated from a standalone
script. You can create the following curve for instance:
Note:
The function plotBar returns the plotWidget; you then have access to the graph for further
customization.
In this example it is assumed that 7 criteria are calculated within the model for analysis of its behavior.
The following piece of Python code illustrates the use of these functions:
Here the variables consumption, performance, tipin, tipout, openthrottle, closethrottle, and gearchange
contain all values during the simulation time. You are going to plot a spider graph that shows the final
values of all variables.
1. In the main script you first need to define the labels titles and the values that are used.
data_labels = ['Consumption',
'Performance',
'Tip-in',
'Tip-out',
'Wide\nopen throttle',
'Zero\nthrottle',
'Gear change']
data = [consumption[0][0][-1],
performance[0][0][-1],
tipin[0][0][-1],
tipout[0][0][-1],
openthrottle[0][0][-1],
closethrottle[0][0][-1],
gearchange[0][0][-1]]
Note:
data_labels is the list of the titles of the spider chart axes and data is the list of the values.
This file is a new module where you can define different built-in graph display functions. In this
example you only define the spider graph.
The function is named plotSpider and has as arguments the data and the labels.
import amepyplot
3. In the function plotSpider you first create the plot widget PlotWidget(), set the size of the plot
window using the function resize(), and access the graph (through firstGraph()).
Note:
Here there is only one graph in the plot window however you can define a layout that
contains several graphs in the same plot window.
import amepyplot
[Link](800, 550)
graph = [Link]()
4. Next, you need to create the data items (using Item object) that gather the values, and the curve
that is represented by one set of discrete values: Curve1DDiscrete.
The display of the curve can be changed using the function changeDisplay and to modify the
symbol for the markers, use configureSymbol on the curve display.
Note:
For spider graphs, you can also use the Display2DArea to fill the area defined by the curve
with color.
import amepyplot
# Create data
item = [Link](data)
# Create curve
curve = amepyplot.Curve1DDiscrete(labels, item)
5. The final step to fully define the function plotSpider is to attach the curve to the graph using
the function addCurve. For the spider graph, you need to apply a conversion of the graph
(convertToSpider) otherwise the display will be a basic 2D line.
import amepyplot
# Create data
item = [Link](data)
# Create curve
curve = amepyplot.Curve1DDiscrete(labels, item)
# Configure graph
[Link](showXAxisTitle=False, showTitle=False)
[Link]().setUnitDrawn(False)
[Link]()
return plot
Note:
Some functions are used to configure the graph elements such as the drawing area (straight
or rounded), the title, and the axes.
6. Since the function to plot a spider graph is complete you just need to call it from your main script.
Remember that you need to import the new module ameplot.
...
import ameplot
from PySide2 import QtWidgets
...
...
data_labels = ['Consumption',
'Performance',
'Tip-in',
'Tip-out',
'Wide\nopen throttle',
'Zero\nthrottle',
'Gear change']
data = [consumption[0][0][-1],
performance[0][0][-1],
tipin[0][0][-1],
tipout[0][0][-1],
openthrottle[0][0][-1],
closethrottle[0][0][-1],
gearchange[0][0][-1]]
Using the Plots Python API you are able to display results that are calculated from a standalone scripts.
You can create the following curve for instance:
Note:
The function plotSpider returns the plotWidget; you then have access to the graph for further
customization.
When launching the App you can scroll through the list of plot types and observe the Python code and
the generated graph display.
If you want to generate such plot in your own App you can copy and paste the code and provide a
PlotWidget reference to the Plot method.
Below are some plot types that you can find in the demo:
• Plot contour
• Plot surface
A piece of code is copied from the Plot API demo in order to demonstrate how to use it in the main
project of an app. It is recommended to keep the app from the Plot API demo open.
2. From the User Interface editor, select the Amesim Display Widgets Plot and drag-and-drop it
onto your MainForm.
5. In the Plot API demo app select Separated Axes as plot type.
7. Then from the Python editor you can paste it in the section Insert extra code here/End of extra
code of the [Link] file at the line 70.
8. Add the import of the modules in the section Insert extra code here/End of extra code at the line
11.
9. Call the createPlot method in the section Extra initialization declarations here/End of initialization
at line 38 to display the plot in the widget.
Note:
The values are defined by mathematical functions in the code. You can also retrieve variables from
the model to have them in the plot instead.