Plotting (Elemental Analysis)

Plotting without errors:
Plotting with errors:


The plotting submodule of Elemental Analysis (/scripts/Muon/ElementalAnalysis/Plotting) is a PyQt4/5 compatible interactive interface of up to four subplots, each with separate, changeable axes.

The x and y limits of each plot (including all plots simultaneously) can be changed using the AxisChanger widgets, and errors can be plotted by using the Errors checkbox. AxisChanger input boxes are automatically updated when the selected plot changes.

The design is such that the user has the ability to handle all matplotlib objects, whilst the plotting and axis changing is handled internally. For this reason, only functions that are absolutely necessary should be exposed, as this maintains simplicity for the user of the wrapper.


The four subplot arrangements are:

  • One subplot, centered
  • Two subplots, side-by-side
  • Three subplots, stacked on top of each other
  • Four subplots, in a two-by-two grid

This layout can be changed by modifying ````.

The python toolbar is available for use. Two custom buttons have been added for adding and removing lines from a subplot.


(importing plotting as plotting)
subplot_name = "subplot name"
subplot = plotting.add_subplot(subplot_name) # creates subplot (& updates the grid)
# this is a standard matplotlib subplot object, so has all the usual methods
# alternatively
#later on...
subplot = plotting.get_subplot(subplot_name)
plotting.plot(subplot_name, mtd["test workspace"]) # plots the workspace "test workspace" to subplot
plotting.remove_subplot(subplot_name) # removes the subplot (& updates the grid)

(... add then delete lines, or text/annotations etc. ...)
# lines can be added as normal in matplotlib
# for example:
line = subplot.axvline(x_axis, y_min, y_max, **kwargs)
line = plotting.get_subplot(subplot_name).axvline(x_axis, y_min, y_max, **kwargs)
plotting.update_canvas() # will update the canvas (generally only required for deleting things)

File and Code Layout

The Plotting files are arranged in the following format:


Both the Plotting and AxisChanger modules are in MVP (Model, View, Presenter) format, as this benefits maintainability.

Plotting Presenter

Exposes required functions from

def get_subplot(self, name):
    Returns the subplot with the given name.

    :param name: the name of the subplot
    :returns: a matplotlib subplot object
    :raises KeyError: if the subplot name doesn't exist
    return self.view.get_subplot(name)

Functions should only be exposed if they are necessary for use with the plotting interface.

Plotting View

Handles all the matplotlib and Qt. Currently, is arguably too large (due to the requirement of isolating matplotlib and Qt), so this could be split up in a future version.

Plotting Utils

Handles the gridspec layouts: further gridspecs can be defined in, but self.gridspecs must be updated in


  • Move plotting to a more central, accessible location in the mantid codebase
  • Moveable lines with the related events (i.e. can check whether a line is moved within tolerance of a given x value)
  • Addition of more subplots
  • Allow any spectrum number to be plotted


  • Options for changing line colours, fonts etc. This is done by python toolbar.
  • Exporting subplots as PNGs etc. This is done by python toolbar.
  • Removal of individual plots from a subplot (the way plotted lines are stored would have to be changed). Needed some rewritting but done now.

Known Issues

  • Axis change goes blank after removing subplot.


AxisChanger is a small widget comprising of two QLineEdit widgets in a neat format, with events exposed for x/y limit changing. This functionality is inherently separate to that of the plotting, so the AxisChanger module is separate (albeit enclosed within) the plotting.