spinetoolbox.plotting

Functions for plotting on PlotWidget.

Module Contents

Classes

PlotType

Generic enumeration.

IndexName

XYData

Two-dimensional data for plotting.

TreeNode

A labeled node in tree structure.

ParameterTableHeaderSection

Header section info for Database editor's parameter tables.

_PlotStackedBars

Functions

convert_indexed_value_to_tree(value)

Converts indexed values to tree nodes recursively.

turn_node_to_xy_data(root_node, y_label_position[, ...])

Constructs plottable data and indexes recursively.

raise_if_not_common_x_labels(data_list)

Raises an exception if data has different x axis labels.

raise_if_incompatible_x(data_list)

Raises an exception if the types of x data don't match.

reduce_indexes(data_list)

Removes redundant indexes from given XYData.

combine_data_with_same_indexes(data_list)

Combines data with same data indexes into the same x axis.

_always_single_y_axis(plot_type)

Returns True if a single y-axis should be used.

plot_data(data_list[, plot_widget, plot_type])

Returns a plot widget with plots of the given data.

_plot_single_y_axis(data_list, y_label, axes, plot_type)

Plots all data on single y-axis.

_plot_stacked_line(data_list, y_label, axes)

Plots all data as stacked lines.

_plot_bar(data_list, y_label, axes)

Plots all data as bars.

_plot_double_y_axis(data_list, y_labels, plot_widget, ...)

Plots all data on two y-axes.

_make_x_plottable(xs)

Converts x-axis values to something matplotlib can handle.

_make_time_series_settings(plot_settings)

Creates plot settings suitable for time series step plots.

_make_plot_function(plot_type, x_data_type, axes)

Decides plot method and default keyword arguments based on XYData.

_is_time_stamp_type(data_type)

Tests if a type looks like time stamp.

_bar(x, y, axes, **kwargs)

Plots bar chart on axes but returns patches instead of bar container.

_group_bars(data_list)

Gives data with same x small offsets to prevent bar stacking.

_clear_plot(plot_widget)

Removes plots and legend from plot widget.

_limit_string_x_tick_labels(data, plot_widget)

Limits the number of x tick labels in case x-axis consists of strings.

_table_display_row(row)

Calculates a human-readable row number.

plot_parameter_table_selection(model, model_indexes, ...)

Returns a plot widget with plots of the selected indexes.

plot_value_editor_table_selection(model, model_indexes)

Returns a plot widget with plots of the selected indexes.

plot_pivot_table_selection(model, model_indexes[, ...])

Returns a plot widget with plots of the selected indexes.

plot_db_mngr_items(items, db_maps[, plot_widget])

Returns a plot widget with plots of database manager parameter value items.

_has_x_column(model, source_model)

Checks if pivot source model has x column.

_set_default_node(root_node, key, label)

Gets node from the contents of root_node adding a new node if necessary.

_get_parsed_value(model_index, display_row)

Gets parsed value from model.

_pivot_index_names(indexes)

Gathers index names from pivot table.

_pivot_display_row(row, source_model)

Calculates display row for pivot table.

_convert_to_leaf(y)

Converts parameter value to leaf TreeElement.

add_row_to_exception(row, display_row)

Adds row information to PlottingError if it is raised in the with block.

add_array_plot(plot_widget, value)

Adds an array plot to a plot widget.

add_time_series_plot(plot_widget, value)

Adds a time series step plot to a plot widget.

Attributes

LEGEND_PLACEMENT_THRESHOLD

_BASE_SETTINGS

_SCATTER_PLOT_SETTINGS

_LINE_PLOT_SETTINGS

_SCATTER_LINE_PLOT_SETTINGS

spinetoolbox.plotting.LEGEND_PLACEMENT_THRESHOLD = 8[source]
class spinetoolbox.plotting.PlotType[source]

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

SCATTER[source]
SCATTER_LINE[source]
LINE[source]
STACKED_LINE[source]
BAR[source]
STACKED_BAR[source]
spinetoolbox.plotting._BASE_SETTINGS[source]
spinetoolbox.plotting._SCATTER_PLOT_SETTINGS[source]
spinetoolbox.plotting._LINE_PLOT_SETTINGS[source]
spinetoolbox.plotting._SCATTER_LINE_PLOT_SETTINGS[source]
exception spinetoolbox.plotting.PlottingError[source]

Bases: Exception

An exception signalling failure in plotting.

Initialize self. See help(type(self)) for accurate signature.

class spinetoolbox.plotting.IndexName[source]
label: str[source]
id: int[source]
class spinetoolbox.plotting.XYData[source]

Two-dimensional data for plotting.

x: List[Union[float, int, str, numpy.datetime64]][source]
y: List[Union[float, int]][source]
x_label: IndexName[source]
y_label: str[source]
data_index: List[str][source]
index_names: List[IndexName][source]
class spinetoolbox.plotting.TreeNode[source]

A labeled node in tree structure.

label: Union[str, IndexName][source]
content: Dict[source]
class spinetoolbox.plotting.ParameterTableHeaderSection[source]

Header section info for Database editor’s parameter tables.

label: str[source]
separator: Optional[str][source]
spinetoolbox.plotting.convert_indexed_value_to_tree(value)[source]

Converts indexed values to tree nodes recursively.

Parameters

value (IndexedValue) – value to convert

Returns

root node of the converted tree

Return type

TreeNode

Raises

ValueError – raised when leaf value couldn’t be converted to float

spinetoolbox.plotting.turn_node_to_xy_data(root_node, y_label_position, index_names=None, indexes=None)[source]

Constructs plottable data and indexes recursively.

Parameters
  • root_node (TreeNode) – root node

  • y_label_position (int, optional) – position of y label in indexes

  • index_names (list of IndexName, optional) – list of current index names

  • indexes (list) – list of current indexes

Yields

XYData – plot data

spinetoolbox.plotting.raise_if_not_common_x_labels(data_list)[source]

Raises an exception if data has different x axis labels.

Parameters

data_list (list of XYData) – data to check

Raises

PlottingError – raised if x axis labels don’t match.

spinetoolbox.plotting.raise_if_incompatible_x(data_list)[source]

Raises an exception if the types of x data don’t match.

Parameters

data_list (list of XYData) – data to check

Raises

PlottingError – raised if x data types don’t match.

spinetoolbox.plotting.reduce_indexes(data_list)[source]

Removes redundant indexes from given XYData.

Parameters

data_list (list of XYData) – data to reduce

Returns

reduced data list and list of common data indexes

Return type

tuple

spinetoolbox.plotting.combine_data_with_same_indexes(data_list)[source]

Combines data with same data indexes into the same x axis.

Parameters

data_list (list of XYData) – data to combine

Returns

combined data

Return type

list of XYData

spinetoolbox.plotting._always_single_y_axis(plot_type)[source]

Returns True if a single y-axis should be used.

Parameters

plot_type (PlotType) – plot type

Returns

True if single y-axis is required, False otherwise

Return type

bool

spinetoolbox.plotting.plot_data(data_list, plot_widget=None, plot_type=None)[source]

Returns a plot widget with plots of the given data.

Parameters
  • data_list (list of XYData) – data to plot

  • plot_widget (PlotWidget, optional) – an existing plot widget to draw into or None to create a new widget

  • plot_type (PlotType, optional) – plot type

Returns

a PlotWidget object

spinetoolbox.plotting._plot_single_y_axis(data_list, y_label, axes, plot_type)[source]

Plots all data on single y-axis.

Parameters
  • data_list (list of XYData) – data to plot

  • y_label (str) – y-axis label

  • axes (Axes) – plot axes

  • plot_type (PlotType) – plot type

Returns

legend handles

Return type

list

spinetoolbox.plotting._plot_stacked_line(data_list, y_label, axes)[source]

Plots all data as stacked lines.

Parameters
  • data_list (list of XYData) – data to plot

  • y_label (str) – y-axis label

  • axes (Axes) – plot axes

Returns

legend handles

Return type

list

spinetoolbox.plotting._plot_bar(data_list, y_label, axes)[source]

Plots all data as bars.

Parameters
  • data_list (list of XYData) – data to plot

  • y_label (str) – y-axis label

  • axes (Axes) – plot axes

Returns

legend handles

Return type

list

spinetoolbox.plotting._plot_double_y_axis(data_list, y_labels, plot_widget, plot_type)[source]

Plots all data on two y-axes.

Parameters
  • data_list (list of XYData) – data to plot

  • y_labels (list of str) – y-axis labels

  • plot_widget (PlotWidget) – plot widget

  • plot_type (PlotType) – plot type

Returns

legend handles

Return type

list

spinetoolbox.plotting._make_x_plottable(xs)[source]

Converts x-axis values to something matplotlib can handle.

Parameters

xs (list) – x values

Returns

x values

Return type

list

class spinetoolbox.plotting._PlotStackedBars(axes)[source]
__call__(x, height, **kwargs)[source]
spinetoolbox.plotting._make_time_series_settings(plot_settings)[source]

Creates plot settings suitable for time series step plots.

Parameters

plot_settings (dict) – base plot settings

Returns

time series step plot settings

Return type

dict

spinetoolbox.plotting._make_plot_function(plot_type, x_data_type, axes)[source]

Decides plot method and default keyword arguments based on XYData.

Parameters
  • plot_type (PlotType) – plot type

  • x_data_type (Type) – data type of x-axis

  • axes (Axes) – plot axes

Returns

plot method

Return type

Callable

spinetoolbox.plotting._is_time_stamp_type(data_type)[source]

Tests if a type looks like time stamp.

Parameters

data_type (Type) – data type to test

Returns

True if type is a time stamp type, False otherwise

Return type

bool

spinetoolbox.plotting._bar(x, y, axes, **kwargs)[source]

Plots bar chart on axes but returns patches instead of bar container.

Parameters
  • x (Any) – x data

  • y (Any) – y data

  • axes (Axes) – plot axes

  • **kwargs – keyword arguments passed to bar()

Returns

patches

Return type

list of Patch

spinetoolbox.plotting._group_bars(data_list)[source]

Gives data with same x small offsets to prevent bar stacking.

Parameters

data_list (List of XYData) – squeezed data

Returns

grouped data, bar width and x ticks

Return type

tuple

spinetoolbox.plotting._clear_plot(plot_widget)[source]

Removes plots and legend from plot widget.

Parameters

plot_widget (PlotWidget) – plot widget

spinetoolbox.plotting._limit_string_x_tick_labels(data, plot_widget)[source]

Limits the number of x tick labels in case x-axis consists of strings.

Matplotlib tries to plot every single x tick label if they are strings. This can become very slow if the labels are numerous.

Parameters
  • data (list of XYData) – plot data

  • plot_widget (PlotWidget) – plot widget

spinetoolbox.plotting._table_display_row(row)[source]

Calculates a human-readable row number.

Parameters

row (int) – model row

Returns

row number

Return type

int

spinetoolbox.plotting.plot_parameter_table_selection(model, model_indexes, table_header_sections, value_section_label, plot_widget=None)[source]

Returns a plot widget with plots of the selected indexes.

Parameters
  • model (QAbstractTableModel) – a model

  • model_indexes (Iterable of QModelIndex) – a list of QModelIndex objects for plotting

  • table_header_sections (list of ParameterTableHeaderSection) – table header labels

  • value_section_label (str) – value column’s header label

  • plot_widget (PlotWidget, optional) – an existing plot widget to draw into or None to create a new widget

Returns

a PlotWidget object

Return type

PlotWidget

spinetoolbox.plotting.plot_value_editor_table_selection(model, model_indexes, plot_widget=None)[source]

Returns a plot widget with plots of the selected indexes.

Parameters
  • model (QAbstractTableModel) – a model

  • model_indexes (Iterable of QModelIndex) – a list of QModelIndex objects for plotting

  • plot_widget (PlotWidget, optional) – an existing plot widget to draw into or None to create a new widget

Returns

a PlotWidget object

Return type

PlotWidget

spinetoolbox.plotting.plot_pivot_table_selection(model, model_indexes, plot_widget=None)[source]

Returns a plot widget with plots of the selected indexes.

Parameters
  • model (QAbstractTableModel) – a model

  • model_indexes (Iterable of QModelIndex) – a list of QModelIndex objects for plotting

  • plot_widget (PlotWidget, optional) – an existing plot widget to draw into or None to create a new widget

Returns

a PlotWidget object

Return type

PlotWidget

spinetoolbox.plotting.plot_db_mngr_items(items, db_maps, plot_widget=None)[source]

Returns a plot widget with plots of database manager parameter value items.

Parameters
  • items (list of dict) – parameter value items

  • db_maps (list of DatabaseMapping) – database mappings corresponding to items

  • plot_widget (PlotWidget, optional) – widget to add plots to

spinetoolbox.plotting._has_x_column(model, source_model)[source]

Checks if pivot source model has x column.

Parameters
Returns

True if x pivot table has column, False otherwise

Return type

bool

spinetoolbox.plotting._set_default_node(root_node, key, label)[source]

Gets node from the contents of root_node adding a new node if necessary.

Parameters
  • root_node (TreeNode) – root node

  • key (Hashable) – key to root_node contents

  • label (str) – label of possible new node

Returns

node at given key

Return type

TreeNode

spinetoolbox.plotting._get_parsed_value(model_index, display_row)[source]

Gets parsed value from model.

Parameters
  • model_index (QModelIndex) – model index

  • display_row (Callable) – callable that returns a display row

Returns

parsed value

Return type

Any

Raises

PlottingError – raised if parsing of value failed

spinetoolbox.plotting._pivot_index_names(indexes)[source]

Gathers index names from pivot table.

Parameters

indexes (tuple of str) – “path” of indexes

Returns

names corresponding to given indexes

Return type

tuple of str

spinetoolbox.plotting._pivot_display_row(row, source_model)[source]

Calculates display row for pivot table.

Parameters
  • row (int) – row in source table model

  • source_model (QAbstractItemModel) – pivot model

Returns

human-readable row number

Return type

int

spinetoolbox.plotting._convert_to_leaf(y)[source]

Converts parameter value to leaf TreeElement.

Parameters

y (Any) – parameter value

Returns

leaf element

Return type

float or datetime or TreeNode

spinetoolbox.plotting.add_row_to_exception(row, display_row)[source]

Adds row information to PlottingError if it is raised in the with block.

Parameters
  • row (int) – row

  • display_row (Callable) – function to convert row to display row

spinetoolbox.plotting.add_array_plot(plot_widget, value)[source]

Adds an array plot to a plot widget.

Parameters
  • plot_widget (PlotWidget) – a plot widget to modify

  • value (Array) – the array to plot

spinetoolbox.plotting.add_time_series_plot(plot_widget, value)[source]

Adds a time series step plot to a plot widget.

Parameters
  • plot_widget (PlotWidget) – a plot widget to modify

  • value (TimeSeries) – the time series to plot