Plot.PlotModel

class PlotModel(x_unit=<NL.CommonConcepts.PhysicalQuantity.Unit object>, y_unit=<NL.CommonConcepts.PhysicalQuantity.Unit object>, projection='rectilinear')

A plot model contains plot items.

The items contain data objects like lines, bars etc. and can be named for easy retrieval.:

>>> model = PlotModel(x_unit, y_unit)
>>> model.setItem('line', Line([0, 1], [1, 2]))

>>> line = model.item('line').

The plot model also contains the special items: xAxis, yAxis, legend and title. These items are special in the sense that they are only applied when the frame containing the model is reset. So for most purposes they only serve as initial values.

Parameters:
  • x_unit (Unit) – The unit of the x axis of the plot model data.

  • y_unit (Unit) – The unit of the y axis of the plot model data.

  • projection (str) – The projection.

addItem(item)

Add a plot item to the model.

Parameters:

item (PlotItem based) – The item to add.

addItems(items)

Add multiple items to the model.

Parameters:

items (list of PlotItem) – The items.

axis(name)
Parameters:

name (str) – The name of the axis to get, ‘x’ or ‘y’.

Returns:

The x- or y-axis for the sub-plot.

Return type:

Axis

clearItems(plot_item_type=None)

Clear all items from the model.

Parameters:

plot_item_type (tuple of class) – Optional tuple of item types to clear.

copy(items=True)

Create a copy of the model (sometimes without items).

Parameters:

items (bool) – True, if items should be copied.

Returns:

The copied model.

Return type:

PlotModel

deleteKey(key, emit=True)

Delete the item stored at specific key.

Parameters:
  • key (str) – The key to delete.

  • emit (bool) – True, if the change should be emitted.

findItems(types=None, function=None, **kwargs)

Find all (possibly nested) items by querying the item properties. Mutliple arguments can be supplied and applies in an exclusive way,:

model.findItems(
    types=Line,
    function=lambda item: 'line' in item.label(),
    line_width=2
)

It is always faster and safer to find the item directly by traversing the object hierarchy, e.g.:

item = model.item('my_key')

or for nested items:

item = model.item('my_key').items()[1]

If searching for top level items only, the items method can be very useful.

Parameters:
  • types (NoneType | PlotItem class) – An optional type filter specifying the plot items to find e.g. Line or Scatter.

  • function (NoneType | function) – An optional filter function taking the plot item as its only input.

  • **kwargs (dict) – Optional query properties.

Returns:

The items matching the query.

Return type:

dict

framing()
Returns:

The model framing.

Return type:

Framing

classmethod fromProperties(properties)

Convert properties into plot model.

Parameters:

properties (dict) – The properties.

Returns:

The created PlotModel.

Return type:

PlotModel

item(key)

Get the item stored on a specific key in the model. Simply returns None if if no item exist at the given key.

Parameters:

key (str) – The key to get.

Returns:

The item

Return type:

object

itemKey(item)

Get the key for the given item.

Parameters:

item (PlotItem) – The given item.

Returns:

The key.

Return type:

str

itemKeys()

All currently set item keys.

Returns:

All item keys.

Return type:

iterable of str

items(tag=None, plot_item_type=None)

Get plot items of specific type.

Parameters:
  • tag (str) – Filter items on an optional tag.

  • plot_item_type (class | tuple of class) – Filter items on an optional item type.

Returns:

Returns the items of either plot item type.

Return type:

view | list

legend()
Returns:

The model legend.

Return type:

Legend

limits(name)

Get limits for one axis.

Parameters:

name (str) – The axis name.

Returns:

Limits.

Return type:

tuple of float

plotter()
Returns:

The plotter this model belongs to.

Return type:

Plotter | None

projection()
Returns:

The model projection (‘rectilinear’ or ‘polar’).

Return type:

str

removeItem(item)

Remove an item from the model.

Parameters:

item (PlotItem based) – The item to remove.

selectedItems()
Returns:

The selected plot items.

Return type:

list of PlotItems

setItem(key, item, overwrite=True, emit=True)

Add a plot item to the model and assign it to a specific.

Parameters:
  • key (str) – The key to set.

  • item (PlotItem based) – The item to add.

  • overwrite (bool) – True, if any existing item at that key should be silently overwritten.

  • emit (bool) – True, if the addition should be emitted.

setLimits(name=None, lower_limit=None, upper_limit=None, item_filter=None)

Set model data limits. Passing no limits will autoscale the current plot limits.

Parameters:
  • name (str) – The name of the axis (‘x’ or ‘y’).

  • lower_limit (None | float) – The lower limit.

  • upper_limit (None | float) – The upper limit.

  • item_filter (function) – An additional item filter used when automatically setting limits.

setProjection(projection)

Set the model projection.

Parameters:

projection (str) – The projection (‘rectilinear’ or ‘polar’).

title()
Returns:

The model framing, which contains the title.

Return type:

Framing

static transferUniformProperties(templates, items, exclude_data=True)

Determine which properties that are the same for all of the provided templates, and transfer those properties to the provided items.

Parameters:
  • templates (list of PlotItem) – The list of plot items to use as templates.

  • items (list of PlotItem) – The items to transfer properties to.

  • exclude_data (bool) – True, if we do not wish to transfer the item data.

Returns:

True, if the transfer succeeded.

Return type:

bool

xAxis()
Returns:

The x-axis for the-plot.

Return type:

Axis

yAxis()
Returns:

The y-axis for the-plot.

Return type:

Axis

Usage Examples

The PlotModel is a container for PlotItems like Line, Scatter, Label etc. The most basic PlotModel is constructed without units on the axises:

>>> model = Plot.PlotModel()
>>> model.addItem(Plot.Line([0, 1, 2, 3, 4], [1, 2, 3, 4, 5]))

Set up a PlotModel with Angstrom on the x-axis and eV on the y-axis.

>>> model = PlotModel(x_unit=Angstrom, y_unit=eV)
>>> model.addItem(Line([0, 1, 2, 3, 4] * nm, [1, 2, 3, 4, 5] * eV))

PlotModels can be shown and saved using the Plot.show and Plot.save functions.

>>> Plot.show(model)
>>> Plot.save(model, 'export.hdf5')

Notes

Multiple PlotModels can also be embedded in a common Plot.PlotFrame, and complex plots can be built by combining multiple PlotFrames in a layout like the Plot.GridLayout or the Plot.OverlayLayout.