FSRSPlot

FSRSPlot is a lightweight plotting widget for wxPython for displaying pyFSRS measurement results. It supports multiple line and scatter plots, contour plots and image plots. While contour plots work on non-equidistantly sampled data and produce nice interpolated plots, image plots operate on 2d arrays and provide an enourmous advantage in speed.

Further features include lin / log scale for x, y, and z axes (individually) as well as reading data coordinates with mouse cursor, two independent data cursors for exploring line/scatter data values and displaying the difference value.

Example usage:

import wx
import numpy as np
from FSRSPlot import *

# setup a wxPython app
app = wx.App()

# get an instance of a plot frame
frame = PlotFrame(None, title="pyFSRS - Test plot panel", size=(640, 480))
frame.plotCanvas.setColormap("blue")

# get some nice data
x = np.linspace(-10, 10, 64)
y = np.linspace(-10, 10, 64)

X, Y = np.meshgrid(x, y)
z = np.exp(-(X**2 +Y**2) / 5.0**2) * np.sin(X)
frame.plotCanvas.addImage(x, y, z)
frame.plotCanvas.addLine(x, -5 + 10.0 * np.exp(-x**2 / 5.0**2))

# show frame and start app
frame.Show()
app.MainLoop()
class core.FSRSPlot.DualPlotFrame(*args, **kwargs)[source]

A simple plot window containing a two vertically stacked plot canvases. The upper / lower one can be accessed as upperPlotCanvas and lowerPlotCanvas.

class core.FSRSPlot.FSRSPlot(parent, id=-1, *args, **kwargs)[source]

The FSRSPlot class is derived from wxPanel and provides a lightweight plotting interface for wxPython that not only supports line/scatter plots but also image and contour plots.

I have kept the number of functions to control the layout to a minimum. Rather, the user is encouraged to directly mess with the class member variables. A list of variables and their default values are given in the following:

# display the cross cursor when mouse is present in plot area
self.showCross = True

# display a grid
self.showGrid = True

# linewidth, markerstyle and colors for scatter / line plot
self.markersize = 2
self.linewidth = 2

# background color
self.bgcolor = wx.Colour(255, 255, 255)

# margin of plotting area to boundary of panel in pixels
self.leftmargin = 60
self.bottommargin = 30

# axis and ticks; font and colors
self.ticklength = 5
self.axisPen = wx.Pen(wx.Colour(0, 0, 0), width=2, style=wx.PENSTYLE_SOLID)
self.font = wx.Font(12, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)

# display Delta-value, i.e. difference between cursors A and B
self.deltafont = wx.Font(20, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
self.deltafontPen = wx.Pen(wx.Colour(30, 0, 250), width=1, style=wx.PENSTYLE_SOLID)

# grid
self.gridPen = wx.Pen(wx.Colour(128, 128, 128), width=1, style=wx.PENSTYLE_DOT)

# mouse crosshair
self.crossPenDark = wx.Pen(wx.Colour(0, 0, 0), width=1, style=wx.PENSTYLE_SOLID)
self.crossPenLight = wx.Pen(wx.Colour(255, 255, 255), width=1, style=wx.PENSTYLE_SOLID)

# list of line colors; the color is cycled through the array for each new line or point
self.linescolor = [wx.Colour(255, 0, 0), wx.Colour(255, 128, 128), wx.Colour(0, 0, 255),
                   wx.Colour(128, 128, 255) , wx.Colour(0, 255, 0), wx.Colour(128, 255, 128)]
Draw(dc)[source]

Internally used to draw the plots to a device context (dc). Plotting order is contour plots first, followed by image plots and line / scatter plots and axes, labels and cursors.

OnEraseBackground(event)[source]

Event handler for background erase.

OnKeyDown(event)[source]

Event handler for key-press event. Handles the following key strokes:

  • C: Clear plot.
  • X: Switch between linear and logarithmic scaling on x-axis.
  • Y: Switch between linear and logarithmic scaling on y-axis.
  • Z: Switch between linear and logarithmic scaling on z-axis. Affects only contour and image plots.
  • M: Center cursor A on maxium of active line/scatter plot.
OnLMouseDown(event)[source]

Event handler for left mouse button down. Positions cursor A on the data if the plot is not empty.

OnMouseMove(event)[source]

Event handler for mouse move event. Updates current position of cursor in data coordinates.

OnMouseWheel(event)[source]

Event handler for mouse wheel event. This event is used to switch the active line / scatter plot for the cursors.

OnPaint(event)[source]

Event handler for paint event.

OnRMouseDown(event)[source]

Event hanlder for right mouse button down. Positions cursor B on the data if the plot is not empty.

OnResize(event)[source]

Event handler for resize event.

addContour(x, y, z)[source]

Add contour plot.

Parameters:
  • x (array) – x-axis of length N. Does not have to be uniform.
  • y (array) – y-axis of length M. Does not have to be uniform.
  • z (array) – z-data, MxN array.
Returns:

Plot id.

addImage(x, y, z)[source]

Add an image plot.

Parameters:
  • x (array) – x-axis. Only the min/max values are used and the axis is linearly interpolated.
  • y (array) – y-axis. Only the min/max values are used and the axis is linearly interpolated.
  • z (array) – Image data, 2d-array.
Returns:

Plot id.

addLine(x, y)[source]

Add a line plot or single point.

Parameters:
  • x (array) – x-axis. Can also be a single integer or floating point number.
  • y (array) – y-axis data with same shape as x.
Returns:

Plot id.

addScatter(x, y)[source]

Add a scatter plot.

Parameters:
  • x (array) – x-axis. Can also be a single integer or floating point number.
  • y (array) – y-axis data with same shape as x.
Returns:

Plot id.

adjustAxis(_min, _max, logscale=False)[source]

Internally used to adjust min, max and number of ticks to get nice looking axes.

Parameters:
  • _min (float) – Minimum data value.
  • _max (float) – Maximum data value.
  • logscale (bool) – Set to True when axis is using logscale, to adapt the number of ticks (default=False).
Returns:

Suggested min, max and number of ticks.

data2screen(x, y)[source]

Convert data coordinates x, y to screen coordinates.

Parameters:
  • x (mixed) – Single x value or x-axis.
  • y (mixed) – Single y value or y-axis with same shape as x.
Returns:

i, j pixel coordinates (integers) with same shapes as x, y.

drawAxes(dc)[source]

Internally used to draw the axes plus labels. Takes also care of crosshair, mouse coordinates and delta.

drawContour(dc, cont)[source]

Used internally to draw a contour plot (cont) to a device context (dc). cont is a tuple consisting of x-axis, y-axis and z-data (2d).

drawCursor(dc, data, x, label)[source]

Used internally to draw cursors.

drawImage(dc, img)[source]

Internally used to draw an image (img) to a device context (dc).

drawLine(dc, data, type='line', cursors=False)[source]

Used internally to draw a line/scatter plot (data, type) to a device context (dc). If cursors is True, draw cursors accordingly.

formatLabels(values, logscale=False)[source]

Internally used to get nice, consistent formatting for the tick labels. Automatically decides for scientific or floating point notation and uses scientific prefixes to keep number of decimals below 3.

Parameters:
  • values (array) – Array of numeric values for which labels are to be generated.
  • logscale (bool) – Set to True, when axis uses logscale. This changes the appearance of the labels.
Returns:

An array of strings with label texts.

formatNumber(x)[source]

Get a formatted number according to the formatting rules that also apply to the labels. Use scientific prefixes and limit the number of decimals below 3.

Parameters:x (float) – Number.
Returns:Formatted string.
getDataMinMax()[source]

Internally used to get the extent of data along x, y and z-axes.

getNumberOfPlots()[source]

Returns total number of plots in this plot window.

getZColor(z, min=-1, max=-1)[source]

Get RGB-tuples for z-values according to current colormap.

When min == max, the full z-extent of the data is used for mapping.

Parameters:
  • z (mixed) – Single z-value, 1d or 2d-array of z-values.
  • min (float) – Minimum z value that gets mapped onto 0.
  • max (float) – Maximum z value that gets mapped onto 1.
Returns:

RGB-tuples with same shape as z.

initVariables()[source]

Init internally used variables and parameters.

interpolateColor(alpha)[source]

Used internally to calculate the rgb-values for a scalar alpha (0..1) and the currently set colormap.

Parameters:alpha (float) – Scalar between 0 and 1.
Returns:RGB-tuple.
prepareMapping()[source]

Internally used to prepare the mapping from data to screen coordinates and vice-versa.

removeLastPlot()[source]

Remove last added plot from the stack.

screen2data(i, j)[source]

Convert screen coordinates i, j to data coordinates.

Parameters:
  • i (mixed) – Single i value or i-axis.
  • j (mixed) – Single j value or j-axis with same shape as i.
Returns:

x, y data coordinates (float) with same shapes as i, j.

setColormap(cmap='rainbow')[source]

Activates the colormap with the given label for plotting.

A colormap consists of a tuple of lists, the first containing the supporting points in the interval 0 to 1 and the second containing the respective r,g,b values as lists. For example, the ‘rgb’ colormap is defined as:

cmap = ([0,            0.5,         1],
        [[0, 0, 255], [0, 255, 0], [255, 0, 0]])
Parameters:cmap (mixed) – Either name of the colormap or a colormap-defining tuple (default=’rgb’). Valid colormap names are ‘hot’, ‘rainbow’, ‘rgb’, ‘blue’, ‘red’, ‘green’, ‘rwb’ (red-white-blue).
setContour(id, x, y, z)[source]

Overwrite an existing contour plot.

Parameters:
  • id (int) – Id or index of contour plot to overwrite. The plot at the given id must already be a contour plot.
  • x (array) – x-axis of length N. Does not have to be uniform.
  • y (array) – y-axis of length M. Does not have to be uniform.
  • z (array) – z-data, MxN array.
setImage(id, x, y, z)[source]

Overwrite an existing image plot.

Parameters:
  • id (int) – Id or index of image plot to overwrite. The plot at the given id must already be an image plot.
  • x (array) – x-axis. Only the min/max values are used and the axis is linearly interpolated.
  • y (array) – y-axis. Only the min/max values are used and the axis is linearly interpolated.
  • z (array) – Image data, 2d-array.
setLine(id, x, y)[source]

Overwrite an existing line / scatter plot.

Parameters:
  • id (int) – Id or index of line / scatter plot to overwrite. The plot at the given id must already be a line / scatter plot.
  • x (array) – x-axis. Can also be a single integer or floating point number.
  • y (array) – y-axis data with same shape as x.
setScatter(id, x, y)[source]

Same as setLine.

setXLabel(label)[source]

Set x-axis label.

setYLabel(label)[source]

Set y-axis label.

class core.FSRSPlot.PlotFrame(*args, **kwargs)[source]

A simple plot window containing a single plot canvas which can be accessed as plotCanvas.