fsleyes.plotting.plotcanvas
This module provides the PlotCanvas class, which plots
DataSeries instances on a matplotlib canvas. The PlotCanvas
is used by the TimeSeriesPanel, HistogramPanel, and
PowerSpectrumPanel views, and potentially by other FSLeyes control
panels.
- class fsleyes.plotting.plotcanvas.PlotCanvas(*args, **kwargs)[source]
Bases:
HasPropertiesThe
PlotCanvascan be used to plotDataSeriesinstances onto amatplotlibFigureCanvasWxAggcanvas.The
PlotCanvasis used by all FSLeyes views which display some sort of 2D data plot, such as theTimeSeriesPanel, and theHistogramPanel.The
PlotCanvasusesmatplotlibfor its plotting. ThematplotlibFigure,Axis, andCanvasinstances can be accessed via thefigure(),axis(), andcanvas()methods, if they are needed. Various display settings can be configured throughPlotCanvasproperties, includinglegend,smooth, etc.Basic usage
After you have created a
PlotCanvas, you can addDataSeriesinstances to thedataSeriesproperty, then calldraw()orasyncDraw().The
draw()method simply callsdrawDataSeries()anddrawArtists(), so you can alternately call those methods directly, or pass your owndrawFuncwhen creating aPlotCanvas.The
PlotCanvasitself is not awxobject, so cannot be displayed - thematplotilb Canvasobject, accessible through thecanvas()method, is what you should add to awxparent object.Data series
A
PlotCanvasinstance plots data contained in one or moreDataSeriesinstances; allDataSeriesclasses are defined in theplottingsub-package.DataSeriesobjects can be plotted by passing them to thedrawDataSeries()method.Or, if you want one or more
DataSeriesto be held, i.e. plotted every time, you can add them to thedataSerieslist. TheDataSeriesin thedataSerieslist will be plotted on every call todrawDataSeries()(in addition to anyDataSeriespassed directly todrawDataSeries()) until they are removed from thedataSerieslist.The draw queue
The
PlotCanvasuses aasync.TaskThreadto asynchronously extract and prepare data for plotting, This is because data preparation may take a long time for certain types ofDataSeries(e.g.TimeSerieswhich are retrieving data from largeImageoverlays), and the main application thread should not be blocked while this is occurring. TheTaskThreadinstance is accessible through thegetDrawQueue()method, in case anything needs to be scheduled on it.- dataSeries
This list contains
DataSeriesinstances which are plotted on every call todrawDataSeries().DataSeriesinstances can be added/removed directly to/from this list.
- artists
This list contains any
matplotlib.Artistinstances which are plotted every call todrawArtists().
- legend
If
True, a legend is added to the plot, with an entry for everyDataSeriesinstance in thedataSerieslist.
- xAutoScale
If
True, the plotlimitsfor the X axis are automatically updated to fit all plotted data.
- yAutoScale
If
True, the plotlimitsfor the Y axis are automatically updated to fit all plotted data.
- xLogScale
Toggle a \(log_{10}\) x axis scale.
- yLogScale
Toggle a \(log_{10}\) y axis scale.
- invertX
Invert the plot along the X axis.
- invertY
Invert the plot along the Y axis.
- xScale
Scale to apply to the X axis data.
- yScale
Scale to apply to the Y axis data.
- xOffset
Offset to apply to the X axis data.
- yOffset
Offset to apply to the Y axis data.
- ticks
Toggle axis ticks and tick labels on/off.
- grid
Toggle an axis grid on/off.
- bgColour
Plot background colour.
- smooth
If
Trueall plotted data is up-sampled, and smoothed using spline interpolation.
- xlabel
A label to show on the x axis.
- ylabel
A label to show on the y axis.
- limits
The x/y axis limits. If
xAutoScaleandyAutoScaleareTrue, these limit values are automatically updated on every call todrawDataSeries().
- showPreparingMessage
Show a message on the canvas whilst data is being prepared for plotting.
- __init__(parent, drawFunc=None, prepareFunc=None)[source]
Create a
PlotCanvas.- Parameters:
parent – The
wxparent object.drawFunc – Custon function to call instead of
draw().prepareFunc – Custom function to call instead of
prepareDataSeries().
- __annotations__ = {}
- __module__ = 'fsleyes.plotting.plotcanvas'
- destroy()[source]
Removes some property listeners, and clears references to all
DataSeries,matplotlibandwxobjects.
- property figure
Returns the
matplotlibFigureinstance.
- property axis
Returns the
matplotlibAxisinstance.
- property canvas
Returns the
matplotlibCanvasinstance.
- draw(*a)[source]
Call
drawDataSeries()and thendrawArtists(). Or, if adrawFuncwas provided, calls that instead.You will generally want to call
asyncDraw()instead of this method.
- asyncDraw(*a)[source]
Schedules
draw()to be run asynchronously. This method should be used in preference to callingdraw()directly in most cases, particularly where the call occurs within a property callback function.This method is automatically called called whenever a
DataSeriesis added to thedataSerieslist, or when any plot display properties change.
- message(msg, clear=True, border=False)[source]
Displays the given message in the centre of the figure.
This is a convenience method provided for use by subclasses.
- getArtist(ds)[source]
Returns the
matplotlib.Artist(typically aLine2Dinstance) associated with the givenDataSeriesinstance. AKeyErroris raised if there is no such artist.
- getDrawnDataSeries()[source]
Returns a list of tuples, each tuple containing the
(DataSeries, x, y)data for oneDataSeriesinstance as it is shown on the plot.
- prepareDataSeries(ds)[source]
Prepares the data from the given
DataSeriesso it is ready to be plotted. Called by the__drawOneDataSeries()method for anyextraSeriespassed to thedrawDataSeries()method (but not applied toDataSeriesthat have been added to thedataSerieslist).This implementation just returns
DataSeries.getData- you can pass aprepareFuncto__init__to perform any custom preprocessing.
- drawArtists(refresh=True, immediate=False)[source]
Draw all
matplotlib.Artistinstances in theartistslist, then refresh the canvas.- Parameters:
refresh – If
True(default), the canvas is refreshed.
- drawDataSeries(extraSeries=None, refresh=False, **plotArgs)[source]
Queues a request to plot all of the
DataSeriesinstances in thedataSerieslist.This method does not do the actual plotting - it is performed asynchronously, to avoid locking up the GUI:
The data for each
DataSeriesinstance is prepared on separate threads (usingidle.run()).A call to
idle.wait()is enqueued on aTaskThread.This
waitfunction waits until all of the data preparation threads have completed, and then passes all of the data to the__drawDataSeries()method.
- Parameters:
extraSeries – A sequence of additional
DataSeriesto be plotted. These series are passed through theprepareDataSeries()method before being plotted.refresh – If
True, the canvas is refreshed. Otherwise, you must callgetCanvas().draw()manually. Defaults toFalse- thedrawArtists()method will refresh the canvas, so if you calldrawArtists()immediately after calling this method (which you should), then you don’t need to manually refresh the canvas.plotArgs – Passed through to the
__drawDataSeries()method.
Note
This method must only be called from the main application thread (the
wxevent loop).
- __drawDataSeries(dataSeries, allXdata, allYdata, oldxlim, oldylim, refresh, xlabel=None, ylabel=None, **plotArgs)
Called by
__drawDataSeries(). Plots all of the data associated with the givendataSeries.- Parameters:
dataSeries – The list of
DataSeriesinstances to plot.allXdata – A list of arrays containing X axis data, one for each
DataSeries.allYdata – A list of arrays containing Y axis data, one for each
DataSeries.oldxlim – X plot limits from the previous draw. If
xAutoScaleis disabled, this limit is preserved.oldylim – Y plot limits from the previous draw. If
yAutoScaleis disabled, this limit is preserved.refresh – Refresh the canvas - see
drawDataSeries().xlabel – If provided, overrides the value of the
xlabelproperty.ylabel – If provided, overrides the value of the
ylabelproperty.plotArgs – Remaining arguments passed to the
__drawOneDataSeries()method.
- __drawOneDataSeries(ds, xdata, ydata, **plotArgs)
Plots a single
DataSeriesinstance. This method is called by thedrawDataSeries()method.- Parameters:
ds – The
DataSeriesinstance.xdata – X axis data.
ydata – Y axis data.
plotArgs – May be used to customise the plot - these arguments are all passed through to the
Axis.plotfunction.
- __dataSeriesChanged(*a)
Called when the
dataSerieslist changes. Adds listeners to any newDataSeriesinstances, and then callsasyncDraw().
- __artistsChanged(*a)
Called when the
artistslist changes. CallsasyncDraw().
- __calcLimits(dataxlims, dataylims, axisxlims, axisylims, axWidth, axHeight)
Calculates and returns suitable axis limits for the current plot. Also updates the
limitsproperty. This method is called by thedrawDataSeries()method.If
xAutoScaleoryAutoScaleare enabled, the limits are calculated from the data range, using the canvas width and height to maintain consistent padding around the plotted data, irrespective of the canvas size.. Otherwise, the existing axis limits are retained.
- Parameters:
dataxlims – A tuple containing the (min, max) x data range.
dataylims – A tuple containing the (min, max) y data range.
axisxlims – A tuple containing the current (min, max) x axis limits.
axisylims – A tuple containing the current (min, max) y axis limits.
axWidth – Canvas width in pixels
axHeight – Canvas height in pixels