Skip to content

Commit

Permalink
Merge pull request #38801 from mantidproject/EWM8982-throw-exception
Browse files Browse the repository at this point in the history
Matrix Workspace throw exception for invalid plot argument
  • Loading branch information
AndreiSavici authored Feb 12, 2025
2 parents 0a8c358 + e905493 commit aab27b7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
9 changes: 3 additions & 6 deletions Framework/API/src/MatrixWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,12 @@ void MatrixWorkspace::setPlotType(const std::string &t) {
StringListValidator v(validPlotTypes);

if (v.isValid(t) == "") {
if (run.hasProperty("plot_type"))
run.addProperty("plot_type", t, true);
else
run.addProperty("plot_type", t, false);
run.addProperty("plot_type", t, true);
} else {
std::string validValues = std::accumulate(
validPlotTypes.begin() + 1, validPlotTypes.end(), validPlotTypes.front(),
[](const std::string &valuesString, const std::string &plotType) { return valuesString + ", " + plotType; });
g_log.warning("Invalid plot type '" + t + "'. Must be one of: " + validValues);
throw std::invalid_argument("Invalid plot type '" + t + "'. Must be one of: " + validValues);
}
}

Expand Down Expand Up @@ -375,7 +372,7 @@ void MatrixWorkspace::setMarkerStyle(const std::string &markerType) {
[](const std::string &valuesString, const std::string &markerStyle) {
return valuesString + ", " + markerStyle;
});
g_log.warning("Invalid marker type '" + markerType + "'. Must be one of: " + validValues);
throw std::invalid_argument("Invalid marker type '" + markerType + "'. Must be one of: " + validValues);
}
}

Expand Down
5 changes: 4 additions & 1 deletion Framework/API/test/MatrixWorkspaceTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(ws->getPlotType(), "plot");

// test invalid is rejected
ws->setPlotType("invalid");
TS_ASSERT_THROWS(ws->setPlotType("invalid"), const std::invalid_argument &);
TS_ASSERT_EQUALS(ws->getPlotType(), "plot");

// test valid is accepted
Expand All @@ -419,6 +419,9 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite {
// test default
TS_ASSERT_EQUALS(ws->getMarkerStyle(), "vline");

TS_ASSERT_THROWS(ws->setMarkerStyle("invalid"), const std::invalid_argument &);
TS_ASSERT_EQUALS(ws->getMarkerStyle(), "vline");

// test set
ws->setMarkerStyle("square");
TS_ASSERT_EQUALS(ws->getMarkerStyle(), "square");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def test_setPlotType(self):
self.assertEqual("plot", ws1.getPlotType())

# test invalid doesn't take
ws1.setPlotType("invalid")
self.assertRaisesRegex(ValueError, "Invalid plot type", ws1.setPlotType, "invalid")
self.assertEqual("plot", ws1.getPlotType())

# test valid takes
Expand All @@ -419,7 +419,7 @@ def test_setMarkerStyle(self):
self.assertEqual("vline", ws1.getMarkerStyle())

# test invalid doesn't take
ws1.setMarkerStyle("invalid")
self.assertRaisesRegex(ValueError, "Invalid marker type", ws1.setMarkerStyle, "invalid")
self.assertEqual("vline", ws1.getMarkerStyle())

# test valid takes
Expand Down
1 change: 1 addition & 0 deletions docs/source/api/python/mantid/plots/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ when giving the projection:
Matrix Workspace Plotting
=========================

The :ref:`Matrix Workspace <MatrixWorkspace_Plotting>` has some additional features that help simplify plotting data.
The `setPlotType()` method allows you to define how a workspace should be visualized when plotting. The following plot types are available:

- **marker**:
Expand Down
9 changes: 6 additions & 3 deletions docs/source/concepts/MatrixWorkspace.rst
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,16 @@ Output:
Counts
Elephants

.. _MatrixWorkspace_Plotting:

Plotting
^^^^^^^^
.. _MatrixWorkspace_Plotting:

You can specify the type of plot to be used when plotting a Matrix Workspace. The following plot types are available:
``plot``, ``marker``, ``errorbar_x``, ``errorbar_y``, ``errorbar_xy``, with ``plot`` as the default.
Changing the plot type will affect the way the data is displayed and can be changed in the following way:
``plot``, ``marker``, ``errorbar_x``, ``errorbar_y``, ``errorbar_xy``. By default all Matrix Workspaces will be
treated as a ``plot`` type and this value will not auto-update or affect existing workspace plots. Changing the plot
type will affect the way the data is displayed. An example of the different plot types can be seen in :ref:`mantid.plots`.
Below is a simple example changing the plot type, a full example is available in the :ref:`plotting gallery<MarkerWS_Example>` page.

.. testsetup:: MatrixWorkspacePlotType

Expand Down
39 changes: 39 additions & 0 deletions docs/source/plotting/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,45 @@ Some data should be visualized as two dimensional colormaps
cbar.set_label('Intensity (arb. units)') #add text to colorbar
#fig.show()

.. _MarkerWS_Example:

Some data is best visualized as a Marker type workspace with no lines connecting the points.
:ref:`Matrix workspace<MatrixWorkspace_Plotting>` has some built-in functions to simplify plotting Marker type workspaces.

.. plot::
:include-source:

import numpy as np
import matplotlib.pyplot as plt
from mantid import plots
from mantid.simpleapi import CreateWorkspace

# Generate random x and y values for two workspaces
x1 = np.arange(20)
y1 = np.random.uniform(5, 50, len(x1)) # Random values between 5 and 50

x2 = np.arange(20)
y2 = np.random.uniform(10, 55, len(x2)) # Different random values

# Create a marker workspace
w1 = CreateWorkspace(DataX=x1, DataY=y1, NSpec=1)
w1.setPlotType('marker')

# Create a second marker workspace
w2 = CreateWorkspace(DataX=x2, DataY=y2, NSpec=1)
w2.setPlotType('marker')
w2.setMarkerStyle('circle')
w2.setMarkerSize(4)

# Plot using the mantid projection
fig, ax = plt.subplots(subplot_kw={'projection': 'mantid'})
ax.plot(w1) # First marker workspace
ax.plot(w2) # Second marker workspace with different style marker

ax.set_title("Marker Workspace Example") # Set plot title
ax.legend() # Show the legend
plt.show()

One can then change properties of the plot. Here is an example that
changes the label of the data, changes the label of the x and y axis,
changes the limits for the y axis, adds a title, change tick orientations,
Expand Down

0 comments on commit aab27b7

Please sign in to comment.