AmesimKnowledge

Simple examples

Plot a bar graph

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:

Copy


# Post-processing the results
Results1, Varnames1 = ameloadt('myModel', '1')
Results2, Varnames2 = ameloadt('myModel', '2')
Results3, Varnames3 = ameloadt('myModel', '3')
Results4, Varnames4 = ameloadt('myModel', '4')
Results5, Varnames5 = ameloadt('myModel', '5')
energy_1 = amegetvar(Results1, Varnames1, 'FX00_1 function of input x')
energy_2 = amegetvar(Results2, Varnames2, 'FX00_1 function of input x')
energy_3 = amegetvar(Results3, Varnames3, 'FX00_1 function of input x')
energy_4 = amegetvar(Results4, Varnames4, 'FX00_1 function of input x')
energy_5 = amegetvar(Results5, Varnames5, 'FX00_1 function of input x')

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.

Procedure

  1. In the main script you first need to define the labels titles and the values that are used.At this step the code is the following:

    Copy`# Post-processing the results Results1, Varnames1 = ameloadt('myModel', '1') Results2, Varnames2 = ameloadt('myModel', '2') Results3, Varnames3 = ameloadt('myModel', '3') Results4, Varnames4 = ameloadt('myModel', '4') Results5, Varnames5 = ameloadt('myModel', '5') energy_1 = amegetvar(Results1, Varnames1, 'FX00_1 function of input x') energy_2 = amegetvar(Results2, Varnames2, 'FX00_1 function of input x') energy_3 = amegetvar(Results3, Varnames3, 'FX00_1 function of input x') energy_4 = amegetvar(Results4, Varnames4, 'FX00_1 function of input x') energy_5 = amegetvar(Results5, Varnames5, 'FX00_1 function of input x')

data_labels = ['Scenario 1', 'Scenario 2', 'Scenario 3', '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]]` Note data_labels is the list of the titles of the bars and data is the list of the values.

  1. Create a new python file and save it as ameplot.py.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.

    At this step the code is the following: Copy`import amepyplot

def plotBar(data, labels):`

  1. 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.

    At this step the code is the following: Copy`import amepyplot

def plotBar(data, labels): plot = amepyplot.PlotWidget() plot.resize(800, 550) graph = plot.firstGraph()`

  1. 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 DisplayBarchart.Outside.

    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.

    At this step the code is the following: Copy`import amepyplot

def plotBar(data, labels): plot = amepyplot.PlotWidget() plot.resize(450, 400) graph = plot.firstGraph()

*# Create data*
item = amepyplot.Item(data)

*# Create curve*
curve = amepyplot.Curve1DDiscrete(labels, item)
curve.display().configure(label=amepyplot.DisplayBarchart.Outside)`
  1. The final step to fully define the function plotBar is to attach the curve to the graph using the function addCurve.

    At this step the code is the following: Copy`import amepyplot

def plotBar(data, labels): plot = amepyplot.PlotWidget() plot.resize(450, 400) graph = plot.firstGraph()

*# Create data*
item = amepyplot.Item(data)

*# Create curve*
curve = amepyplot.Curve1DDiscrete(labels, item)
curve.display().configure(label=amepyplot.DisplayBarchart.Outside)

*# Add the curve to the graph*
graph.addCurve(curve)

*# Configure graph*
graph.configureTitles(showXAxisTitle=False, showTitle=False)

plot.show()

return plot`
Note 
 Some functions are used to configure the graph elements such as the title and the axes.
  1. 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.

    Copy`... import ameplot from PySide2 import QtWidgets

... ...

# Post-processing the results Results1, Varnames1 = ameloadt('myModel', '1') Results2, Varnames2 = ameloadt('myModel', '2') Results3, Varnames3 = ameloadt('myModel', '3') Results4, Varnames4 = ameloadt('myModel', '4') Results5, Varnames5 = ameloadt('myModel', '5') energy_1 = amegetvar(Results1, Varnames1, 'FX00_1 function of input x') energy_2 = amegetvar(Results2, Varnames2, 'FX00_1 function of input x') energy_3 = amegetvar(Results3, Varnames3, 'FX00_1 function of input x') energy_4 = amegetvar(Results4, Varnames4, 'FX00_1 function of input x') energy_5 = amegetvar(Results5, Varnames5, 'FX00_1 function of input x')

data_labels = ['Scenario 1', 'Scenario 2', 'Scenario 3', '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]]

# Create the application if needed app = QtWidgets.QApplication.instance() if app is None: app = QtWidgets.QApplication([])

# Call the created function plot = ameplot.plotBar(data, data_labels)

# Start the application app.exec_()`

Results

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:

Figure 13: Simcenter Amesim plot window

Note

The function plotBar returns the plotWidget; you then have access to the graph for further customization.

Source: https://docs.sw.siemens.com/en-US/doc/254352342/PL20250521841123434.amesim_collection.Plots_Python_API/xid1206365 · retrieved 2026-07-17