The functions in this module are intended to be used with matplotlib’s object oriented abstract program interface (API). matplotlib’s (stateful) functional interface is discouraged by matplotlib. The object oriented API allow for customization as well.
The plotting of a mantid.api.MatrixWorkspace or a mantid.api.IMDHistoWorkspace can happen in two different ways. The use of a mantid projection allows most matplotlib-like experience:
import matplotlib.pyplot as plt
from mantid import plots
#some code here to get a workspace, and x, y, yerr arrays
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.errorbar(workspace,'rs',specNum=1) #for workspaces
ax.errorbar(x,y,yerr,'bo') #for arrays
fig.show()
If the mantid projection is not used, the plotting functions take a matplotlib.axes.Axes and a mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace, with some keywords that are specific to Mantid an the type or workspace used. While there are defaults for the labels, you can easily override them after the initial plotting is called. A useful reference is matplotlib’s anatomy of a figure.
Note
To run these usage examples please first download the usage data, and add these to your path. In MantidPlot this is done using Manage User Directories.
All of the examples below can be run with the following imports, but not all are used in all places.
from mantid.simpleapi import mtd, Load, LoadEventNexus, Rebin, ConvertUnits, SofQW, Transpose
from mantid import plots
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
First, load some diffraction data and see what the automatic axes will be using get_axes_labels().
Load(Filename="PG3_733", OutputWorkspace="PG3_733")
print(plots.helperfunctions.get_axes_labels(mtd['PG3_733']))
Which will print the y-label then the labels for all the other axes as properly escaped for use directly in matplotlib.axes.Axes.set_xlabel().
('Counts', 'd-Spacing ($\\AA$)', 'Spectrum')
To generate a 1D plots of some spectra with mantid projection:
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.plot(mtd['PG3_733'], 'go-', specNum=1, label='user label')
ax.errorbar(mtd['PG3_733'], wkspIndex=2)
ax.legend()
fig.show()
or without:
fig, ax = plt.subplots()
plots.plotfunctions.plot(ax, mtd['PG3_733'], 'go-', specNum=1, label='user label')
plots.plotfunctions.errorbar(ax, mtd['PG3_733'], wkspIndex=2)
ax.legend()
fig.show()
This example demonstrates adding multiple spectra onto a single 1D plot and overriding some of the default behavior. plot() is a normal line plot, while errorbar() adds the uncertainties. It should be warned that every call to one of the plot functions will automatically annotate the axes with the last one called being the one that takes effect.
The plot() function also allows plotting sample logs.
from mantid import plots
import matplotlib.pyplot as plt
w = LoadEventNexus(Filename='CNCS_7860_event.nxs')
fig = plt.figure()
ax1 = fig.add_subplot(211, projection = 'mantid')
ax2 = fig.add_subplot(212, projection = 'mantid')
ax1.plot(w, LogName = 'ChopperStatus5')
ax1.set_title('From run start')
ax2.plot(w, LogName = 'ChopperStatus5', FullTime = True)
ax2.set_title('Absolute time')
fig.tight_layout()
fig.show()
Two common ways to look at 2D plots are contourf() and pcolormesh(). The difference between these is the contourf() calculates smooth lines of constant value, where the pcolormesh() is the actual data values. pcolormesh() is similar to pcolor(), but uses a different mechanism and returns a different object; pcolor returns a PolyCollection but pcolormesh returns a QuadMesh. It is much faster, so it is almost always preferred for large arrays.
LoadEventNexus(Filename='CNCS_7860_event.nxs', OutputWorkspace='CNCS_7860_event')
ConvertUnits(InputWorkspace='CNCS_7860_event', OutputWorkspace='CNCS_7860_event', Target='DeltaE', EMode='Direct', EFixed=3)
Rebin(InputWorkspace='CNCS_7860_event', OutputWorkspace='CNCS_7860_event', Params='-3,0.05,3')
SofQW(InputWorkspace='CNCS_7860_event', OutputWorkspace='CNCS_7860_sqw', QAxisBinning='0,0.05,3', EMode='Direct', EFixed=3)
Transpose(InputWorkspace='CNCS_7860_sqw', OutputWorkspace='CNCS_7860_sqw')
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
c = ax.contourf(mtd['CNCS_7860_sqw'], norm=LogNorm())
ax.set_xlabel('awesome label')
fig.colorbar(c)
fig.show()
Similarly, showing the actual values with pcolormesh()
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
c = ax.pcolormesh(mtd['CNCS_7860_sqw'], norm=LogNorm())
fig.colorbar(c)
fig.show()
A couple of notes about pcolor(), pcolormesh(), and pcolorfast():
In addition to the mantid projection, there is also the mantid3d projection for 3d plots. Can be used much the same as the mantid projection, but by instead specifying mantid3d when giving the projection:
import matplotlib.pyplot as plt
from mantid import plots
#some code here to get a workspace, and x, y, yerr arrays
fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
ax.plot_wireframe(workspace) #for workspaces
ax.plot_wireframe(x,y,z) #for arrays
fig.show()
Informational
1D Plotting
2D Plotting
3D Plotting
matplotlib demonstrates the difference between uniform and nonuniform grids well in this example
This class defines the mantid projection for 2d plotting. One chooses this projection using:
import matplotlib.pyplot as plt
from mantid import plots
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
or:
import matplotlib.pyplot as plt
from mantid import plots
fig = plt.figure()
ax = fig.add_subplot(111,projection='mantid')
The mantid projection allows replacing the array objects with mantid workspaces.
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.contour() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.contour(workspace) #for workspaces
ax.contour(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.contour()
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.contourf() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.contourf(workspace) #for workspaces
ax.contourf(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.contourf()
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.errorbar() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.errorbar(workspace,'rs',specNum=1) #for workspaces
ax.errorbar(x,y,yerr,'bo') #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.errorbar()
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.pcolor() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.pcolor(workspace) #for workspaces
ax.pcolor(x,y,C) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.pcolor()
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.pcolorfast() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.pcolorfast(workspace) #for workspaces
ax.pcolorfast(x,y,C) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.pcolorfast()
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.pcolormesh() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.pcolormesh(workspace) #for workspaces
ax.pcolormesh(x,y,C) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.pcolormesh()
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.plot() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.plot(workspace,'rs',specNum=1) #for workspaces
ax.plot(x,y,'bo') #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.plot().
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.scatter() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.scatter(workspace,'rs',specNum=1) #for workspaces
ax.scatter(x,y,'bo') #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.scatter()
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.tricontour() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.tricontour(workspace) #for workspaces
ax.tricontour(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.tricontour()
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.tricontourf() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.tricontourf(workspace) #for workspaces
ax.tricontourf(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.tricontourf()
If the mantid projection is chosen, it can be used the same as matplotlib.axes.Axes.tripcolor() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid'})
ax.tripcolor(workspace) #for workspaces
ax.tripcolor(x,y,C) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions.tripcolor()
This class defines the mantid3d projection for 3d plotting. One chooses this projection using:
import matplotlib.pyplot as plt
from mantid import plots
fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
or:
import matplotlib.pyplot as plt
from mantid import plots
fig = plt.figure()
ax = fig.add_subplot(111,projection='mantid3d')
The mantid3d projection allows replacing the array objects with mantid workspaces.
If the mantid3d projection is chosen, it can be used the same as matplotlib.axes.Axes3D.contour() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
ax.contour(workspace) #for workspaces
ax.contour(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions3D.contour()
If the mantid3d projection is chosen, it can be used the same as matplotlib.axes.Axes3D.contourf() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
ax.contourf(workspace) #for workspaces
ax.contourf(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions3D.contourf()
If the mantid3d projection is chosen, it can be used the same as matplotlib.axes.Axes3D.plot() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
ax.plot(workspace) #for workspaces
ax.plot(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions3D.plot3D()
If the mantid3d projection is chosen, it can be used the same as matplotlib.axes.Axes3D.plot_surface() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
ax.plot_surface(workspace) #for workspaces
ax.plot_surface(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions3D.plot_surface()
If the mantid3d projection is chosen, it can be used the same as matplotlib.axes.Axes3D.plot_wireframe() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
ax.plot_wireframe(workspace) #for workspaces
ax.plot_wireframe(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions3D.wireframe()
If the mantid3d projection is chosen, it can be used the same as matplotlib.axes.Axes3D.scatter() for arrays, or it can be used to plot mantid.api.MatrixWorkspace or mantid.api.IMDHistoWorkspace. You can have something like:
import matplotlib.pyplot as plt
from mantid import plots
...
fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
ax.scatter(workspace) #for workspaces
ax.scatter(x,y,z) #for arrays
fig.show()
For keywords related to workspaces, see mantid.plots.plotfunctions3D.scatter()
Unpack mantid workspace and render it with matplotlib. args and kwargs are passed to matplotlib.axes.Axes.plot() after special keyword arguments are removed. This will automatically label the line according to the spectrum number unless specified otherwise.
Parameters: |
|
---|
For matrix workspaces with more than one spectra, either specNum or wkspIndex needs to be specified. Giving both will generate a RuntimeError. There is no similar keyword for MDHistoWorkspaces. These type of workspaces have to have exactly one non integrated dimension
Unpack mantid workspace and render it with matplotlib. args and kwargs are passed to matplotlib.axes.Axes.errorbar() after special keyword arguments are removed. This will automatically label the line according to the spectrum number unless specified otherwise.
Parameters: |
|
---|
For matrix workspaces with more than one spectra, either specNum or wkspIndex needs to be specified. Giving both will generate a RuntimeError. There is no similar keyword for MDHistoWorkspaces. These type of workspaces have to have exactly one non integrated dimension
Unpack mantid workspace and render it with matplotlib. args and kwargs are passed to matplotlib.axes.Axes.scatter() after special keyword arguments are removed. This will automatically label the line according to the spectrum number unless specified otherwise.
Parameters: |
|
---|
For matrix workspaces with more than one spectra, either specNum or wkspIndex needs to be specified. Giving both will generate a RuntimeError. There is no similar keyword for MDHistoWorkspaces. These type of workspaces have to have exactly one non integrated dimension
Essentially the same as matplotlib.axes.Axes.contour() but calculates the countour levels. Currently this only works with workspaces that have a constant number of bins between spectra.
Parameters: |
|
---|
Essentially the same as matplotlib.axes.Axes.contourf() but calculates the countour levels. Currently this only works with workspaces that have a constant number of bins between spectra.
Parameters: |
|
---|
Essentially the same as matplotlib.axes.Axes.pcolor()
Parameters: |
|
---|
Essentially the same as matplotlib.axes.Axes.pcolorfast()
Parameters: |
|
---|
Essentially the same as matplotlib.axes.Axes.pcolormesh().
Parameters: |
|
---|
To be used with non-uniform grids. Currently this only works with workspaces that have a constant number of bins between spectra or with MDHistoWorkspaces.
Parameters: |
|
---|
See matplotlib.axes.Axes.tripcolor() for more information.
Essentially the same as mantid.plots.contour(), but works for non-uniform grids. Currently this only works with workspaces that have a constant number of bins between spectra or with MDHistoWorkspaces.
Parameters: |
|
---|
See matplotlib.axes.Axes.tricontour() for more information.
Essentially the same as mantid.plots.contourf(), but works for non-uniform grids. Currently this only works with workspaces that have a constant number of bins between spectra or with MDHistoWorkspaces.
Parameters: |
|
---|
See matplotlib.axes.Axes.tricontourf() for more information.
3D plots - line plots
Parameters: |
|
---|
Scatter plots
Parameters: |
|
---|
Wire-frame plot
Parameters: |
|
---|
Surface plots
Parameters: |
|
---|
Contour plots
Parameters: |
|
---|
Filled Contour plots
Parameters: |
|
---|