finam_plot.XyPlot#

class finam_plot.XyPlot(inputs, title=None, colors=None, pos=None, size=None, update_interval=1, **plot_kwargs)[source]#

Bases: PlotBase

Line and scatter plots for multiple instant series, push-based.

Inputs are expected to be one of:

  • 1-D structured grids. The axis is plotted as x, while the data is plotted as y.

  • 1-D NoGrid. x is enumerated, data is plotted as y.

  • 2-D NoGrid. x is the first column, while y is the second column.

Uses matplotlib.pyplot.plot().

             +----------+
--> [custom] |          |
--> [custom] |  XyPlot  |
--> [......] |          |
             +----------+

Note

This component is push-based without an internal time step.

Examples

import finam_plot as fmp

plot = fmp.XyPlot(
    inputs=["Value1", "Value2"],
    colors=["red", "#ff00ee"],
    marker="o", lw=2.0, # plot kwargs
)
Parameters:
  • inputs (list of str) – List of input names (plot series) that will become available for coupling.

  • title (str, optional) – Title for plot and window.

  • colors (list of str, optional) – List of colors for the inputs. Uses matplotlib default colors by default.

  • pos (tuple(number, number), optional) – Figure position. int is interpreted as pixels, float is interpreted as fraction of screen size.

  • size (tuple(number, number), optional) – Figure size. int is interpreted as pixels, float is interpreted as fraction of screen size.

  • update_interval (int, optional) – Redraw interval (independent of data retrieval).

  • **plot_kwargs – Keyword arguments passed to plot function. See matplotlib.pyplot.plot().

Attributes:
connector

The component’s tools.ConnectHelper.

inputs

dict: The component’s inputs.

logger

Logger for this component.

logger_name

Logger name derived from base logger name and class name.

metadata

The component’s meta data.

name

Component name.

outputs

dict: The component’s outputs.

status

The component’s current status.

uses_base_logger_name

Whether this class has a base_logger_name attribute.

Methods

connect(start_time)

Connect exchange data and metadata with linked components.

create_connector([pull_data, in_info_rules, ...])

Initialize the component's tools.ConnectHelper.

create_figure()

Creates a figure with the plot's title and size.

finalize()

Finalize and clean up the component.

initialize()

Initialize the component.

repaint([relim])

Repaints the plot window.

should_repaint()

Returns whether the plot should repaint, based on it's undate interval.

try_connect(start_time[, exchange_infos, ...])

Exchange the info and data with linked components.

update()

Update the component by one time step.

validate()

Validate the correctness of the component's settings and coupling.

with_name(name)

Renames the component and returns self.

connect(start_time)#

Connect exchange data and metadata with linked components.

The method can be called multiple times if there are failed pull attempts.

After each method call, the component should have status ComponentStatus.CONNECTED if connecting was completed, ComponentStatus.CONNECTING if some but not all required initial input(s) could be pulled, and ComponentStatus.CONNECTING_IDLE if nothing could be pulled.

Parameters:

start_time (datetime) – The composition’s starting time. Can be before the component’s actual time.

create_connector(pull_data=None, in_info_rules=None, out_info_rules=None, cache=True)#

Initialize the component’s tools.ConnectHelper.

See also try_connect(), connector and ConnectHelper for details.

Parameters:
  • pull_data (arraylike) – Names of the inputs that are to be pulled.

  • in_info_rules (dict) – Info transfer rules for inputs. See the examples for details.

    See also tools.FromInput, tools.FromOutput and tools.FromValue.

  • out_info_rules (dict) – Info transfer rules for outputs. See the examples for details.

    See also tools.FromInput, tools.FromOutput and tools.FromValue.

  • cache (bool) – Whether data and Info objects passed via try_connect() are cached for later calls. Default True.

Examples

The following examples show the usage of this method in _initialize().

Simple usage if no input data or any metadata from connected components is required:

self.inputs.add(name="In", time=self.time, grid=fm.NoGrid())
self.outputs.add(name="Out", time=self.time, grid=fm.NoGrid())
self.create_connector()

To pull specific inputs, use pull_data like this:

self.inputs.add(name="In1", time=self.time, grid=fm.NoGrid())
self.inputs.add(name="In2", time=self.time, grid=fm.NoGrid())

self.create_connector(pull_data=["In1", "In2"])

With the in_info_rules and out_info_rules, metadata can be transferred between coupling slots.

Here, the metadata for an output is taken from an input:

self.inputs.add(name="In", time=self.time, grid=None, units=None)
self.outputs.add(name="Out")

self.create_connector(
    out_info_rules={
        "Out": [
            fm.tools.FromInput("In")
        ]
    }
)

The Info object for output Out will be created and pushed automatically in try_connect() as soon as the metadata for In becomes available.

Here, the metadata of an output is composed from the metadata of two inputs and a user-defined value:

self.inputs.add(name="In1", time=self.time, grid=None, units=None)
self.inputs.add(name="In2", time=self.time, grid=None, units=None)
self.outputs.add(name="Out")

self.create_connector(
    out_info_rules={
        "Out": [
            fm.tools.FromInput("In1", ["time", "grid"]),
            fm.tools.FromInput("In2", ["units"]),
            fm.tools.FromValue("source", "FINAM"),
        ]
    }
)

The Info object for output Out would be automatically composed in try_connect() as soon as the infos of both inputs become available. time and grid would be taken from In1, units from In2, and source would be set to "finam".

Rules are evaluated in the given order. Later rules can overwrite attributes set by earlier rules.

create_figure()#

Creates a figure with the plot’s title and size.

finalize()#

Finalize and clean up the component.

After the method call, the component should have status ComponentStatus.FINALIZED.

initialize()#

Initialize the component.

After the method call, the component’s inputs and outputs must be available, and the component should have status ComponentStatus.INITIALIZED.

repaint(relim=False)#

Repaints the plot window.

should_repaint()#

Returns whether the plot should repaint, based on it’s undate interval.

try_connect(start_time, exchange_infos=None, push_infos=None, push_data=None)#

Exchange the info and data with linked components.

Values passed by the arguments are cached internally for later calls to the method if the connector was created with cache=True (the default). Thus, it is sufficient to provide only data and infos that became newly available. Giving the same data or infos repeatedly overwrites the cache.

Sets the component’s status according to success of exchange.

See also create_connector(), connector and ConnectHelper for details.

Parameters:
  • start_time (datetime) – the composition’s starting time as passed to Component.try_connect()

  • exchange_infos (dict of [str, Info]) – currently or newly available input data infos by input name

  • push_infos (dict of [str, Info]) – currently or newly available output data infos by output name

  • push_data (dict of [str, array-like]) – currently or newly available output data by output name

update()#

Update the component by one time step. Push new values to outputs.

After the method call, the component should have status ComponentStatus.UPDATED or ComponentStatus.FINISHED.

validate()#

Validate the correctness of the component’s settings and coupling.

After the method call, the component should have status ComponentStatus.VALIDATED.

with_name(name)#

Renames the component and returns self.

property connector#

The component’s tools.ConnectHelper.

See also create_connector() and try_connect().

property inputs#

The component’s inputs.

Type:

dict

property logger#

Logger for this component.

property logger_name#

Logger name derived from base logger name and class name.

property metadata#

The component’s meta data. Will only be called after the connect phase of the composition.

Returns an empty dict unless overwritten in component implementation.

property name#

Component name.

property outputs#

The component’s outputs.

Type:

dict

property status#

The component’s current status.

property uses_base_logger_name#

Whether this class has a base_logger_name attribute. True.