/qt/widgets/mplcpp/src/MantidAxes.cpp

https://github.com/mantidproject/mantid · C++ · 115 lines · 63 code · 16 blank · 36 comment · 3 complexity · 943485f11953de5577dd503f68634685 MD5 · raw file

  1. // Mantid Repository : https://github.com/mantidproject/mantid
  2. //
  3. // Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
  4. // NScD Oak Ridge National Laboratory, European Spallation Source,
  5. // Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
  6. // SPDX - License - Identifier: GPL - 3.0 +
  7. #include "MantidQtWidgets/MplCpp/MantidAxes.h"
  8. #include "MantidQtWidgets/Common/Python/QHashToDict.h"
  9. using Mantid::API::MatrixWorkspace_sptr;
  10. using Mantid::PythonInterface::GlobalInterpreterLock;
  11. namespace MantidQt::Widgets {
  12. namespace Python = Common::Python;
  13. namespace MplCpp {
  14. using MatrixWorkpaceToPython = Python::ToPythonValue<MatrixWorkspace_sptr>;
  15. /**
  16. * Construct an Axes wrapper around an existing Axes instance
  17. * @param obj A matplotlib.axes.Axes instance
  18. */
  19. MantidAxes::MantidAxes(Python::Object pyObj) : Axes{std::move(pyObj)} {}
  20. /**
  21. * Add a line on the current axes based on the workspace
  22. * @param workspace A pointer to a workspace containing the data
  23. * @param wkspIndex The workspace index to plot
  24. * @param lineColour Set the line colour to this string name
  25. * @param label A label for the curve
  26. * @param otherKwargs Other kwargs to use for the line
  27. * @return A new Line2D artist object
  28. */
  29. Line2D MantidAxes::plot(const Mantid::API::MatrixWorkspace_sptr &workspace, const size_t wkspIndex,
  30. const QString &lineColour, const QString &label,
  31. const boost::optional<QHash<QString, QVariant>> &otherKwargs) {
  32. GlobalInterpreterLock lock;
  33. const auto wksp = Python::NewRef(MatrixWorkpaceToPython()(workspace));
  34. const auto args = Python::NewRef(Py_BuildValue("(O)", wksp.ptr()));
  35. Python::Dict kwargs;
  36. if (otherKwargs)
  37. kwargs = Python::qHashToDict(otherKwargs.get());
  38. kwargs["wkspIndex"] = wkspIndex;
  39. kwargs["color"] = lineColour.toLatin1().constData();
  40. kwargs["label"] = label.toLatin1().constData();
  41. return Line2D{pyobj().attr("plot")(*args, **kwargs)[0]};
  42. }
  43. /**
  44. * Add an errorbar plot on the current axes based on the workspace
  45. * @param workspace A pointer to a workspace containing the data
  46. * @param wkspIndex The workspace index to plot
  47. * @param lineColour Set the line colour to this string name
  48. * @param label A label for the curve
  49. * @return A new ErrorbarContainer object
  50. */
  51. ErrorbarContainer MantidAxes::errorbar(const Mantid::API::MatrixWorkspace_sptr &workspace, const size_t wkspIndex,
  52. const QString &lineColour, const QString &label,
  53. const boost::optional<QHash<QString, QVariant>> &otherKwargs) {
  54. GlobalInterpreterLock lock;
  55. const auto wksp = Python::NewRef(MatrixWorkpaceToPython()(workspace));
  56. const auto args = Python::NewRef(Py_BuildValue("(O)", wksp.ptr()));
  57. Python::Dict kwargs;
  58. if (otherKwargs)
  59. kwargs = Python::qHashToDict(otherKwargs.get());
  60. kwargs["wkspIndex"] = wkspIndex;
  61. kwargs["color"] = lineColour.toLatin1().constData();
  62. kwargs["label"] = label.toLatin1().constData();
  63. return ErrorbarContainer{pyobj().attr("errorbar")(*args, **kwargs)};
  64. }
  65. void MantidAxes::pcolormesh(const Mantid::API::MatrixWorkspace_sptr &workspace,
  66. const boost::optional<QHash<QString, QVariant>> &otherKwargs) {
  67. GlobalInterpreterLock lock;
  68. const auto wksp = Python::NewRef(MatrixWorkpaceToPython()(workspace));
  69. const auto args = Python::NewRef(Py_BuildValue("(O)", wksp.ptr()));
  70. Python::Dict kwargs;
  71. if (otherKwargs)
  72. kwargs = Python::qHashToDict(otherKwargs.get());
  73. pyobj().attr("pcolormesh")(*args, **kwargs);
  74. }
  75. /**
  76. * Remove any artists plotted from the given workspace.
  77. * @param ws A reference to a workspace whose name is used to
  78. * lookup any artists for removal
  79. */
  80. bool MantidAxes::removeWorkspaceArtists(const Mantid::API::MatrixWorkspace_sptr &ws) {
  81. GlobalInterpreterLock lock;
  82. bool removed = false;
  83. try {
  84. removed = pyobj().attr("remove_workspace_artists")(Python::NewRef(MatrixWorkpaceToPython()(ws)));
  85. } catch (Python::ErrorAlreadySet &) {
  86. throw Mantid::PythonInterface::PythonException();
  87. }
  88. return removed;
  89. }
  90. /**
  91. * Replace the artists on this axes instance that are based off this workspace
  92. * @param newWS A reference to the new workspace containing the data
  93. */
  94. bool MantidAxes::replaceWorkspaceArtists(const Mantid::API::MatrixWorkspace_sptr &newWS) {
  95. GlobalInterpreterLock lock;
  96. return pyobj().attr("replace_workspace_artists")(Python::NewRef(MatrixWorkpaceToPython()(newWS)));
  97. }
  98. } // namespace MplCpp
  99. } // namespace MantidQt::Widgets