The silx.gui.plot package provides a 1D, 2D plot widget that supports multiple backends. This package is structured as follows.
PlotWidget and PlotWindow provides the user API. PlotWidget is a Qt widget (actually a QMainWindow) displaying a 1D, 2D plot area. It provides different interaction modes. PlotWindow is a Qt widget (actually a QMainWindow) which adds a set of toolbar buttons and associated functionalities to PlotWidget. The toolbar QActions are implemented in PlotActions.
Plot, PlotEvents and PlotInteraction implement the plotting API regardless of the rendering backend and regardless of its integration in Qt. The plotting API in defined in Plot. The different interaction modes (zoom, drawing, pan) are implemented in PlotInteraction. Each interaction mode is implemented with a state machine structure (implemented in Interaction). The different events emitted by Plot and by the interaction modes are created with helper functions defined in PlotEvents.
BackendBase defines the API any plot backend should provide in BackendBase. BackendMatplotlib implements a matplotlib backend. It is splitted in two classes:
For PlotWidget and Plot modules, see their respective documentations: PlotWidget, Plot.
The following modules are the modules used internally by the plot package.
Base class for Plot backends.
It documents the Plot backend API.
This API is a simplified version of PyMca PlotBackend API.
Class defining the API a backend of the Plot should provide.
Add a 1D curve given by x an y to the graph.
Parameters: |
|
---|---|
Returns: | The handle used by the backend to univocally access the curve |
Add an image to the plot.
Parameters: |
|
---|---|
Returns: | The handle used by the backend to univocally access the image |
Add an item (i.e. a shape) to the plot.
Parameters: |
|
---|---|
Returns: | The handle used by the backend to univocally access the item |
Add a point, vertical line or horizontal line marker to the plot.
Parameters: |
|
---|---|
Returns: | Handle used by the backend to univocally access the marker |
Convert a position in data space to a position in pixels in the widget.
Parameters: |
|
---|---|
Returns: | The corresponding position in pixels or None if the data position is not in the displayed area. |
Return type: | A tuple of 2 floats: (xPixel, yPixel) or None. |
Get the graph X (bottom) limits.
Returns: | Minimum and maximum values of the X axis |
---|
Get the graph Y (left) limits.
Parameters: | axis (str) – The axis for which to get the limits: left or right |
---|---|
Returns: | Minimum and maximum values of the Y axis |
Plot area bounds in widget coordinates in pixels.
Returns: | bounds as a 4-tuple of int: (left, top, width, height) |
---|
Get a list of strings with the supported colormap names.
The list should at least contain and start by: [‘gray’, ‘reversed gray’, ‘temperature’, ‘red’, ‘green’, ‘blue’]
Get a list of items at a pixel position.
Parameters: |
|
---|---|
Returns: | All picked items from back to front. One dict per item, with ‘kind’ key in ‘curve’, ‘marker’, ‘image’; ‘legend’ key, the item legend. and for curves, ‘xdata’ and ‘ydata’ keys storing picked position on the curve. |
Return type: | list of dict |
Convert a position in pixels in the widget to a position in the data space.
Parameters: |
|
---|---|
Returns: | The corresponding position in data space or None if the pixel position is not in the plot area. |
Return type: | A tuple of 2 floats: (xData, yData) or None. |
Trigger a Plot.replot().
Default implementation triggers a synchronous replot if plot is dirty. This method should be overridden by the embedding widget in order to provide an asynchronous call to replot in order to optimize the number replot operations.
Remove an existing item from the plot.
Parameters: | item – A backend specific item handle returned by a add* method |
---|
Reset the displayed area of the plot.
Autoscale any axis that is in autoscale mode. Keep current limits on axes not in autoscale mode
Extra margins can be added around the data inside the plot area. Margins are given as one ratio of the data range per limit of the data (xMin, xMax, yMin and yMax limits). For log scale, extra margins are applied in log10 of the data.
Parameters: | dataMargins (A 4-tuple of float as (xMin, xMax, yMin, yMax).) – Ratios of margins to add around the data inside the plot area for each side |
---|
Save the graph to a file (or a StringIO)
Parameters: |
|
---|
Set/Reset the color of a curve to show that it is active.
Parameters: |
|
---|
Toggle the display of a crosshair cursor and set its attributes.
To override in interactive backends.
Parameters: |
|
---|
Set the cursor shape.
To override in interactive backends.
Parameters: | cursor (str) – Name of the cursor shape or None |
---|
Set grid.
Parameters: | which – None to disable grid, ‘major’ for major grid, ‘both’ for major and minor grid |
---|
Set the main title of the plot.
Parameters: | title (str) – Title associated to the plot |
---|
Set the X axis label.
Parameters: | label (str) – label associated to the plot bottom X axis |
---|
Set the limits of X axis.
Parameters: |
|
---|
Set the left Y axis label.
Parameters: |
|
---|
Set the limits of the Y axis.
Parameters: |
|
---|
Set whether to keep data aspect ratio or not.
Parameters: | flag (Boolean, default True) – True to respect data aspect ratio |
---|
Set the limits of the X and Y axes at once.
Parameters: |
|
---|
Set the X axis scale between linear and log.
Parameters: | flag (bool) – If True, the bottom axis will use a log scale |
---|
Matplotlib Plot backend.
Base class for Matplotlib backend without a FigureCanvas.
For interactive on screen plot, see BackendMatplotlibQt.
See BackendBase.BackendBase for public API documentation.
A QDialog widget to set-up the colormap.
It uses a description of colormaps as dict compatible with Plot.
To run the following sample code, a QApplication must be initialized.
Create the colormap dialog and set the colormap description and data range:
>>> from silx.gui.plot.ColormapDialog import ColormapDialog
>>> dialog = ColormapDialog()
>>> dialog.setColormap(name='red', normalization='log',
... autoscale=False, vmin=1., vmax=2.)
>>> dialog.setDataRange(1., 100.) # This scale the width of the plot area
>>> dialog.show()
Get the colormap description (compatible with Plot) from the dialog:
>>> cmap = dialog.getColormap()
>>> cmap['name']
'red'
It is also possible to display an histogram of the image in the dialog. This updates the data range with the range of the bins.
>>> import numpy
>>> image = numpy.random.normal(size=512 * 512).reshape(512, -1)
>>> hist, bin_edges = numpy.histogram(image, bins=10)
>>> dialog.setHistogram(hist, bin_edges)
The updates of the colormap description are also available through the signal: ColormapDialog.sigColormapChanged.
A QDialog widget to set the colormap.
Parameters: |
|
---|
Return the colormap description as a dict.
See Plot for documentation on the colormap dict.
Returns the data range used for the histogram area.
Returns: | (dataMin, dataMax) or None if no data range is set |
---|---|
Return type: | 2-tuple of float |
Returns the counts and bin edges of the displayed histogram.
Returns: | (hist, bin_edges) |
---|---|
Return type: | 2-tuple of numpy arrays |
Override key handling.
It disables leaving the dialog when editing a text field.
Set the colormap description
If some arguments are not provided, the current values are used.
Parameters: |
|
---|
Set the range of data to use for the range of the histogram area.
Parameters: |
|
---|
Set the histogram to display.
This update the data range with the bounds of the bins. See setDataRange().
Parameters: |
|
---|
Signal triggered when the colormap is changed.
It provides a dict describing the colormap to the slot. This dict can be used with Plot.
Color conversion function and dictionary.
This module provides an implementation of state machines for interaction.
Sample code of a state machine with two states (‘idle’ and ‘active’) with transitions on left button press/release:
from silx.gui.plot.Interaction import *
class SampleStateMachine(StateMachine):
class Idle(State):
def onPress(self, x, y, btn):
if btn == LEFT_BTN:
self.goto('active')
class Active(State):
def enter(self):
print('Enabled') # Handle enter active state here
def leave(self):
print('Disabled') # Handle leave active state here
def onRelease(self, x, y, btn):
if btn == LEFT_BTN:
self.goto('idle')
def __init__(self):
# State machine has 2 states
states = {
'idle': SampleStateMachine.Idle,
'active': SampleStateMachine.Active
}
super(TwoStates, self).__init__(states, 'idle')
# idle is the initial state
stateMachine = SampleStateMachine()
# Triggers a transition to the Active state:
stateMachine.handleEvent('press', 0, 0, LEFT_BTN)
# Triggers a transition to the Idle state:
stateMachine.handleEvent('release', 0, 0, LEFT_BTN)
See ClickOrDrag for another example of a state machine.
See Renaud Blanch, Michel Beaudouin-Lafon. Programming Rich Interactions using the Hierarchical State Machine Toolkit. In Proceedings of AVI 2006. p 51-58. for a discussion of using (hierarchical) state machines for interaction.
State machine for left and right click and left drag interaction.
It is intended to be used through subclassing by overriding click(), beginDrag(), drag() and endDrag().
Left mouse button.
Middle mouse button.
Right mouse button.
Base class for the states of a state machine.
This class is meant to be subclassed.
Called when the state machine enters this state.
Arguments are those provided to the goto() method that triggered the transition to this state.
State machine controller.
This is the entry point of a state machine. It is in charge of dispatching received event and handling the current active state.
Process an event with the state machine.
This method looks up for an event handler in the current state and then in the StateMachine instance. Handler are looked up as ‘onEventName’ method. If a handler is found, it is called with the provided extra arguments, and this method returns the return value of the handler. If no handler is found, this method returns None.
Parameters: | eventName (str) – Name of the event to handle |
---|---|
Returns: | The return value of the handler or None |
Matplotlib computationally modest image class.
Computationally modest image class.
Customization of https://github.com/ChrisBeaumont/ModestImage to allow extent support.
ModestImage is an extension of the Matplotlib AxesImage class better suited for the interactive display of larger images. Before drawing, ModestImage resamples the data array based on the screen resolution and view window. This has very little affect on the appearance of the image, but can substantially cut down on computation since calculations of unresolved or clipped pixels are skipped.
The interface of ModestImage is the same as AxesImage. However, it does not currently support setting the ‘extent’ property. There may also be weird coordinate warping operations for images that I’m not aware of. Don’t expect those to work either.
This module provides a set of QAction to use with PlotWidget.
The following QAction are available:
QAction opening a ColormapDialog to update the colormap.
Both the active image colormap and the default colormap are updated.
Parameters: |
|
---|
QAction to copy PlotWidget content to clipboard.
Parameters: |
|
---|
QAction controlling curve style on a PlotWidget.
It changes the default line and markers style which updates all curves on the plot.
Parameters: |
|
---|
QAction controlling grid mode on a PlotWidget.
Parameters: |
|
---|
QAction controlling aspect ratio on a PlotWidget.
Parameters: |
|
---|
QAction for printing the plot.
It opens a Print dialog.
Current implementation print a bitmap of the plot area and not vector graphics, so printing quality is not great.
Parameters: |
|
---|
QAction controlling reset zoom on a PlotWidget.
Parameters: |
|
---|
QAction for saving Plot content.
It opens a Save as... dialog.
Parameters: |
|
---|
QAction controlling X axis autoscale on a PlotWidget.
Parameters: |
|
---|
QAction controlling X axis log scale on a PlotWidget.
Parameters: |
|
---|
QAction controlling Y axis autoscale on a PlotWidget.
Parameters: |
|
---|
QAction controlling Y orientation on a PlotWidget.
Parameters: |
|
---|
QAction controlling Y axis log scale on a PlotWidget.
Parameters: |
|
---|
Functions to prepare events to be sent to Plot callback.
See Plot documentation for content of events
See Plot documentation for content of events
See Plot documentation for content of events
See Plot documentation for content of events
See Plot documentation for content of events
Implementation of the interaction for the Plot.
Manages focus across multiple event handlers
On press an event handler can acquire focus. By default it looses focus when all buttons are released.
Pan plot content and zoom on wheel state machine.
Proxy to currently use state machine for interaction.
This allows to switch interactive mode.
Parameters: | plot – The Plot to apply interaction to |
---|
Returns the current interactive mode as a dict.
The returned dict contains at least the key ‘mode’. Mode can be: ‘draw’, ‘pan’, ‘select’, ‘zoom’. It can also contains extra keys (e.g., ‘color’) specific to a mode as provided to setInteractiveMode().
Forward event to current interactive mode state machine.
Switch the interactive mode.
Parameters: |
|
---|
True to enable zoom on wheel, False otherwise.
Base class for drawing selection areas.
Base class for drawing selection area based on one input point.
Base class for drawing selection based on 2 input points.
Drawing a horizontal line selection area state machine.
Drawing line selection area state machine.
Drawing selection polygon area state machine.
Drawing rectangle selection area state machine.
Drawing a vertical line selection area state machine.