From 5e15a6c33d3c4cbe8a74dc8daee350c34f732989 Mon Sep 17 00:00:00 2001 From: Jonathan Haigh Date: Thu, 4 Apr 2024 15:38:31 +0100 Subject: [PATCH 1/2] Switch to using fig.show() from plt.show() Since upgrading to matplotlib 3.7, plt.show() has sometimes created freezing / hanging behaviour when run from within workbench --- .../algorithms/ConvertToReflectometryQ-v1.rst | 2 +- .../algorithms/ConvertToReflectometryQ-v2.rst | 2 +- docs/source/algorithms/CopySample-v1.rst | 2 +- docs/source/algorithms/HB2AReduce-v1.rst | 12 ++-- .../algorithms/LagrangeILLReduction-v1.rst | 10 +-- docs/source/algorithms/SetSample-v1.rst | 2 +- .../SingleCrystalDiffuseReduction-v1.rst | 2 +- .../concepts/HowToDefineGeometricShape.rst | 4 +- .../fitfunctions/BivariateGaussian.rst | 16 ++--- .../direct/Crystal Field Python Interface.rst | 5 +- docs/source/plotting/1DPlotsHelp.rst | 67 ++----------------- docs/source/plotting/3DPlotsHelp.rst | 6 +- docs/source/plotting/ColorfillPlotsHelp.rst | 30 ++------- docs/source/plotting/MeshPlotHelp.rst | 4 +- docs/source/plotting/WaterfallPlotsHelp.rst | 2 +- .../source/release/v5.1.0/mantidworkbench.rst | 3 +- docs/source/techniques/CrystalField.rst | 21 +++--- .../06_sample_logs.rst | 4 +- .../getting_started/main_window.rst | 18 ++--- .../plotting/01_basic_plot_scripting.rst | 10 +-- .../python_in_mantid/solutions/02_pim_sol.rst | 32 ++------- .../python_in_mantid/solutions/03_pim_sol.rst | 5 +- 22 files changed, 83 insertions(+), 176 deletions(-) diff --git a/docs/source/algorithms/ConvertToReflectometryQ-v1.rst b/docs/source/algorithms/ConvertToReflectometryQ-v1.rst index 32141ef2b46f..dbebb7f0031e 100644 --- a/docs/source/algorithms/ConvertToReflectometryQ-v1.rst +++ b/docs/source/algorithms/ConvertToReflectometryQ-v1.rst @@ -205,7 +205,7 @@ achieved by running the algorithm below. axes.set_xlim([-0.0004,0.0004]) axes.set_ylim([0,0.2]) - plt.show() + fig.show() threadsafe_call(patch_plot, dump_vertexes) diff --git a/docs/source/algorithms/ConvertToReflectometryQ-v2.rst b/docs/source/algorithms/ConvertToReflectometryQ-v2.rst index 6882f67f61fa..350fb7372901 100644 --- a/docs/source/algorithms/ConvertToReflectometryQ-v2.rst +++ b/docs/source/algorithms/ConvertToReflectometryQ-v2.rst @@ -203,7 +203,7 @@ achieved by running the algorithm below. axes.set_xlim([-0.0004,0.0004]) axes.set_ylim([0,0.2]) - plt.show() + fig.show() threadsafe_call(patch_plot, dump_vertexes) diff --git a/docs/source/algorithms/CopySample-v1.rst b/docs/source/algorithms/CopySample-v1.rst index 5327b1d57276..908b43792157 100644 --- a/docs/source/algorithms/CopySample-v1.rst +++ b/docs/source/algorithms/CopySample-v1.rst @@ -130,7 +130,7 @@ After running this example code, the sample shapes can be plotted (see :ref:`Mes axes.set_mesh_axes_equal(mesh) axes.view_init(elev=20, azim=80) - plt.show() + fig.show() diff --git a/docs/source/algorithms/HB2AReduce-v1.rst b/docs/source/algorithms/HB2AReduce-v1.rst index 3269bd9dc739..e90b969ee8a3 100644 --- a/docs/source/algorithms/HB2AReduce-v1.rst +++ b/docs/source/algorithms/HB2AReduce-v1.rst @@ -100,7 +100,7 @@ Usage ax.plot(ws, specNum=num) plt.legend() #fig.savefig('HB2AReduce_1.png') - plt.show() + fig.show() .. figure:: /images/HB2AReduce_1.png @@ -117,7 +117,7 @@ Usage fig, ax = plt.subplots(subplot_kw={'projection':'mantid'}) ax.plot(ws) #fig.savefig('HB2AReduce_2.png') - plt.show() + fig.show() .. figure:: /images/HB2AReduce_2.png @@ -134,7 +134,7 @@ Usage fig, ax = plt.subplots(subplot_kw={'projection':'mantid'}) ax.plot(ws) #fig.savefig('HB2AReduce_3.png') - plt.show() + fig.show() .. figure:: /images/HB2AReduce_3.png @@ -151,7 +151,7 @@ Usage fig, ax = plt.subplots(subplot_kw={'projection':'mantid'}) ax.plot(ws) #fig.savefig('HB2AReduce_4.png') - plt.show() + fig.show() .. figure:: /images/HB2AReduce_4.png @@ -168,7 +168,7 @@ Usage fig, ax = plt.subplots(subplot_kw={'projection':'mantid'}) ax.plot(ws) #fig.savefig('HB2AReduce_5.png') - plt.show() + fig.show() .. figure:: /images/HB2AReduce_5.png @@ -191,7 +191,7 @@ single anode *vs* temperature. fig, ax = plt.subplots(subplot_kw={'projection':'mantid'}) ax.plot(ws, specNum=8) # anode8 #fig.savefig('HB2AReduce_6.png') - plt.show() + fig.show() .. figure:: /images/HB2AReduce_6.png diff --git a/docs/source/algorithms/LagrangeILLReduction-v1.rst b/docs/source/algorithms/LagrangeILLReduction-v1.rst index 84a4af42ae84..97b5bad97c5c 100644 --- a/docs/source/algorithms/LagrangeILLReduction-v1.rst +++ b/docs/source/algorithms/LagrangeILLReduction-v1.rst @@ -51,10 +51,12 @@ Usage eis = result.readX(0) temperatures = run.getLogData('temperature').value - plt.plot(eis, temperatures) - plt.xlabel("Ei (meV)") - plt.ylabel("temperature (K)") - plt.show() + fig, ax = plt.subplots(subplot_kw={'projection': 'mantid'}) + + ax.plot(eis, temperatures) + ax.set_xlabel("Ei (meV)") + ax.set_ylabel("temperature (K)") + fig.show() **Multiple monochromators example** diff --git a/docs/source/algorithms/SetSample-v1.rst b/docs/source/algorithms/SetSample-v1.rst index ba75f938b3bf..39d273a9452c 100644 --- a/docs/source/algorithms/SetSample-v1.rst +++ b/docs/source/algorithms/SetSample-v1.rst @@ -334,7 +334,7 @@ After running this example code to rotate a cuboid by 30° anti-clockwise around axes.set_mesh_axes_equal(mesh) axes.view_init(elev=20, azim=80) - plt.show() + fig.show() .. categories:: diff --git a/docs/source/algorithms/SingleCrystalDiffuseReduction-v1.rst b/docs/source/algorithms/SingleCrystalDiffuseReduction-v1.rst index 3ea2fbe65636..795fc6932724 100644 --- a/docs/source/algorithms/SingleCrystalDiffuseReduction-v1.rst +++ b/docs/source/algorithms/SingleCrystalDiffuseReduction-v1.rst @@ -240,7 +240,7 @@ Usage fig, ax = plt.subplots(subplot_kw={'projection':'mantid'}) c = ax.pcolormesh(mtd['output'],vmin=0, vmax=1e-5) fig.colorbar(c) - plt.show() + fig.show() .. figure:: /images/SingleCrystalDiffuseReduction_corelli_multiple_sym_bkg_HH0.png diff --git a/docs/source/concepts/HowToDefineGeometricShape.rst b/docs/source/concepts/HowToDefineGeometricShape.rst index 690dfd74434f..927058bf9242 100644 --- a/docs/source/concepts/HowToDefineGeometricShape.rst +++ b/docs/source/concepts/HowToDefineGeometricShape.rst @@ -332,7 +332,7 @@ Here the dimensions are used to define a 2m x 4m x 0.2m cuboid with its centre a axes.text(10.5,11.5,11, "WIDTH", color='b', fontsize=12) axes.text(11,9.5,9, "HEIGHT", color='purple', fontsize=12) - plt.show() + fig.show() In the next example, four points are used to describe a 2m x 0.8m x 0.4m cuboid with the its centre at the origin. @@ -406,7 +406,7 @@ In the next example, four points are used to describe a 2m x 0.8m x 0.4m cuboid axes.scatter(0,0,0, color='b') axes.text(0,0.1,-0.15, "ORIGIN", color='b', fontsize=12) - plt.show() + fig.show() Hexahedron diff --git a/docs/source/fitting/fitfunctions/BivariateGaussian.rst b/docs/source/fitting/fitfunctions/BivariateGaussian.rst index 2a322ee506c2..4497445ac15d 100644 --- a/docs/source/fitting/fitfunctions/BivariateGaussian.rst +++ b/docs/source/fitting/fitfunctions/BivariateGaussian.rst @@ -86,16 +86,12 @@ Here is an example of fitting a 2D histogram: ZFit = bvg.function2D(pos) #Plot the results - plt.figure(1) - plt.clf() - plt.subplot(1,2,1) - plt.imshow(Z, origin='lower') - plt.title('Data') - plt.subplot(1,2,2) - plt.imshow(ZFit, origin='lower') - plt.title('Fit') - plt.show() - + fig, axes = plt.subplots(nrows=1, ncols=2, subplot_kw={'projection': 'mantid'}) + axes[0].imshow(Z, origin='lower') + axes[0].set_title('Data') + axes[1].imshow(ZFit, origin='lower') + axes[1].set_title('Fit') + fig.show() .. categories:: diff --git a/docs/source/interfaces/direct/Crystal Field Python Interface.rst b/docs/source/interfaces/direct/Crystal Field Python Interface.rst index e215904dfe36..e866875a2b3c 100644 --- a/docs/source/interfaces/direct/Crystal Field Python Interface.rst +++ b/docs/source/interfaces/direct/Crystal Field Python Interface.rst @@ -142,8 +142,9 @@ After the peak shape is defined a spectrum can be calculated:: The output is a tuple of two 1d numpy arrays (x, y) that can be used with `matplotlib` to plot:: import matplotlib.pyplot as plt - plt.plot(*sp) - plt.show() + fig, ax = plt.subplots(subplot_kw={'projection': 'mantid'}) + ax.plot(*sp) + fig.show() .. image:: /images/CrystalFieldSpectrum1.png :height: 300 diff --git a/docs/source/plotting/1DPlotsHelp.rst b/docs/source/plotting/1DPlotsHelp.rst index 6ef597edc47e..b758d0594e00 100644 --- a/docs/source/plotting/1DPlotsHelp.rst +++ b/docs/source/plotting/1DPlotsHelp.rst @@ -85,14 +85,15 @@ Scripting Click the generate a script button |GenerateAScript.png| on a 1D Plot: -.. code-block:: python +.. plot:: + :include-source: # import mantid algorithms, numpy and matplotlib + from mantid.simpleapi import * import matplotlib.pyplot as plt from mantid.plots.utility import MantidAxType - from mantid.api import AnalysisDataService as ADS - MAR11060 = ADS.retrieve('MAR11060') + MAR11060 = Load('MAR11060') fig, axes = plt.subplots(edgecolor='#ffffff', num='MAR11060-1', subplot_kw={'projection': 'mantid'}) axes.plot(MAR11060, color='#1f77b4', label='MAR11060: spec 1', wkspIndex=0) @@ -105,29 +106,7 @@ Click the generate a script button |GenerateAScript.png| on a 1D Plot: axes.set_ylabel('Counts ($\\mu s$)$^{-1}$') legend = axes.legend(fontsize=8.0).set_draggable(True).legend - plt.show() - -.. plot:: - - # import mantid algorithms, numpy and matplotlib - from mantid.simpleapi import * - import matplotlib.pyplot as plt - from mantid.plots.utility import MantidAxType - - MAR11060 = Load('MAR11060') - - fig, axes = plt.subplots(edgecolor='#ffffff', num='MAR11060-1', subplot_kw={'projection': 'mantid'}) - axes.plot(MAR11060, color='#1f77b4', label='MAR11060: spec 1', specNum=1) - axes.plot(MAR11060, color='#ff7f0e', label='MAR11060: spec 2', specNum=2) - axes.plot(MAR11060, color='#2ca02c', label='MAR11060: spec 3', specNum=3) - axes.tick_params(axis='x', which='major', **{'gridOn': False, 'tick1On': True, 'tick2On': False, 'label1On': True, 'label2On': False, 'size': 6, 'tickdir': 'out', 'width': 1}) - axes.tick_params(axis='y', which='major', **{'gridOn': False, 'tick1On': True, 'tick2On': False, 'label1On': True, 'label2On': False, 'size': 6, 'tickdir': 'out', 'width': 1}) - axes.set_title('MAR11060') - axes.set_xlabel('Time-of-flight ($\\mu s$)') - axes.set_ylabel('Counts ($\\mu s$)$^{-1}$') - legend = axes.legend(fontsize=8.0) # .set_draggable(True).legend # uncomment to set the legend draggable - - plt.show() + fig.show() For more advice: :ref:`02_scripting_plots` @@ -199,40 +178,8 @@ Scripting An example script for a Tiled Plot: -.. code-block:: python - - # import mantid algorithms, numpy and matplotlib - from mantid.simpleapi import * - import matplotlib.pyplot as plt - from mantid.plots.utility import MantidAxType - from mantid.api import AnalysisDataService as ADS - - MAR11060 = ADS.retrieve('MAR11060') #May replace with Load('MAR11060') - - fig, axes = plt.subplots(edgecolor='#ffffff', ncols=2, nrows=2, num='MAR11060-1', subplot_kw={'projection': 'mantid'}) - axes[0][0].plot(MAR11060, color='#1f77b4', label='MAR11060: spec 1', wkspIndex=0) - axes[0][0].set_xlabel('Time-of-flight ($\\mu s$)') - axes[0][0].set_ylabel('Counts ($\\mu s$)$^{-1}$') - legend = axes[0][0].legend(fontsize=8.0).set_draggable(True).legend - - axes[0][1].plot(MAR11060, color='#1f77b4', label='MAR11060: spec 2', wkspIndex=1) - axes[0][1].set_xlabel('Time-of-flight ($\\mu s$)') - axes[0][1].set_ylabel('Counts ($\\mu s$)$^{-1}$') - legend = axes[0][1].legend(fontsize=8.0).set_draggable(True).legend - - axes[1][0].plot(MAR11060, color='#1f77b4', label='MAR11060: spec 3', wkspIndex=2) - axes[1][0].set_xlabel('Time-of-flight ($\\mu s$)') - axes[1][0].set_ylabel('Counts ($\\mu s$)$^{-1}$') - legend = axes[1][0].legend(fontsize=8.0).set_draggable(True).legend - - axes[1][1].plot(MAR11060, color='#1f77b4', label='MAR11060: spec 4', wkspIndex=3) - axes[1][1].set_xlabel('Time-of-flight ($\\mu s$)') - axes[1][1].set_ylabel('Counts ($\\mu s$)$^{-1}$') - legend = axes[1][1].legend(fontsize=8.0).set_draggable(True).legend - - plt.show() - .. plot:: + :include-source: # import mantid algorithms, numpy and matplotlib from mantid.simpleapi import * @@ -262,7 +209,7 @@ An example script for a Tiled Plot: axes[1][1].set_ylabel('Counts ($\\mu s$)$^{-1}$') legend = axes[1][1].legend(fontsize=8.0) #.set_draggable(True).legend # uncomment to set the legend draggable - plt.show() + fig.show() For more advice: :ref:`02_scripting_plots` diff --git a/docs/source/plotting/3DPlotsHelp.rst b/docs/source/plotting/3DPlotsHelp.rst index dbdea3910b77..5b010ceef89c 100644 --- a/docs/source/plotting/3DPlotsHelp.rst +++ b/docs/source/plotting/3DPlotsHelp.rst @@ -83,7 +83,8 @@ Basic example of plotting a `Surface `_: -.. code-block:: python - - import matplotlib.pyplot as plt - import numpy as np - from matplotlib.colors import LogNorm - from matplotlib.ticker import LogLocator - from mantid.api import AnalysisDataService as ADS - - MAR11060 = ADS.retrieve('MAR11060') - - fig, axes = plt.subplots(figsize=[8.0, 7.0], num='MAR11060-1', subplot_kw={'projection': 'mantid'}) - cfill = axes.imshow(MAR11060, aspect='auto', cmap='viridis', distribution=False, origin='lower') - cfill.set_norm(LogNorm(vmin=0.0001, vmax=3792.3352)) - # If no ticks appear on the color bar remove the subs argument inside the LogLocator below - cbar = fig.colorbar(cfill, ax=[axes], ticks=LogLocator(subs=np.arange(1, 10)), pad=0.06) - cbar.set_label('Counts ($\\mu s$)$^{-1}$') - axes.set_title('MAR11060') - axes.set_xlabel('Time-of-flight ($\\mu s$)') - axes.set_ylabel('Spectrum') - axes.set_xlim([5.0, 19992.0]) - axes.set_ylim([0.5, 922.5]) - - plt.show() - .. plot:: + :include-source: # import mantid algorithms, numpy and matplotlib from mantid.simpleapi import * @@ -116,7 +93,7 @@ Click the generate a script button |GenerateAScript.png| on a `Colorfill Plot `_ can be overlayed on a 2D colorfill: @@ -154,7 +154,7 @@ To overplot on the same window: cbar=fig.colorbar(c) cbar.set_label('Counts ($\mu s$)$^{-1}$') #add text to colorbar - #plt.show() + #fig.show() 3D Surface and Wireframe Plots @@ -175,7 +175,7 @@ To overplot on the same window: fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'}) ax.plot_surface(data, cmap='viridis') - #plt.show() + #fig.show() .. plot:: :include-source: @@ -189,7 +189,7 @@ To overplot on the same window: fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'}) ax.plot_wireframe(data, color='green') - #plt.show() + #fig.show() * See :ref:`here ` for custom color cycles and colormaps diff --git a/docs/source/tutorials/python_in_mantid/solutions/02_pim_sol.rst b/docs/source/tutorials/python_in_mantid/solutions/02_pim_sol.rst index 3ec72383f25a..6ddd4b8c476c 100644 --- a/docs/source/tutorials/python_in_mantid/solutions/02_pim_sol.rst +++ b/docs/source/tutorials/python_in_mantid/solutions/02_pim_sol.rst @@ -48,32 +48,8 @@ A - Processing ISIS Data B - Plotting ILL Data ===================== -.. code-block:: python - - # import mantid algorithms, numpy and matplotlib - from mantid.simpleapi import * - import matplotlib.pyplot as plt - import numpy as np - from mantid.api import AnalysisDataService as ADS - - _164198 = ADS.retrieve('164198') - - fig, axes = plt.subplots(edgecolor='#ffffff', num='164198-1', subplot_kw={'projection': 'mantid'}) - axes.plot(_164198, color='#2ca02c', label='164198: spec 100', linewidth=1.0, specNum=100, zorder=2.1) - axes.plot(_164198, color='#1f77b4', label='164198: spec 200', linewidth=1.0, specNum=200, zorder=2.1) - axes.errorbar(_164198, capsize=1.0, color='#ff7f0e', label='A funky label', linewidth=1.0, specNum=50) - axes.plot(_164198, color='#000000', label='164198: spec 300', linewidth=1.0, markeredgecolor='#d62728', markerfacecolor='#d62728', specNum=300, zorder=2.1) - axes.set_title('My Beautiful Plot') - axes.set_xlabel(r'Time-of-flight ($\mu s$)') - axes.set_ylabel('Counts') - axes.set_xlim([460.0, 600.0]) - axes.set_ylim([1.0, 2000.0]) - axes.set_yscale('log') - axes.legend().draggable() - - plt.show() - .. plot:: + :include-source: # import mantid algorithms, numpy and matplotlib from mantid.simpleapi import * @@ -88,14 +64,14 @@ B - Plotting ILL Data axes.errorbar(_164198, capsize=1.0, color='#ff7f0e', label='A funky label', linewidth=1.0, specNum=50) axes.plot(_164198, color='#000000', label='164198: spec 300', linewidth=1.0, markeredgecolor='#d62728', markerfacecolor='#d62728', specNum=300, zorder=2.1) axes.set_title('My Beautiful Plot') - axes.set_xlabel('Time-of-flight ($\mu s$)') + axes.set_xlabel(r'Time-of-flight ($\mu s$)') axes.set_ylabel('Counts') axes.set_xlim([460.0, 600.0]) axes.set_ylim([1.0, 2000.0]) axes.set_yscale('log') axes.legend() #.draggable() - #plt.show() + #fig.show() C - Processing and Plotting SNS Data @@ -134,7 +110,7 @@ C - Processing and Plotting SNS Data axes.set_yscale('log') axes.legend() #.draggable() - #plt.show() + #fig.show() # NOTE: This script could be improved further with adding comments, # and extracting and logging values for filename and binning params, diff --git a/docs/source/tutorials/python_in_mantid/solutions/03_pim_sol.rst b/docs/source/tutorials/python_in_mantid/solutions/03_pim_sol.rst index 3fb1f6ab65f7..da620ec184e9 100644 --- a/docs/source/tutorials/python_in_mantid/solutions/03_pim_sol.rst +++ b/docs/source/tutorials/python_in_mantid/solutions/03_pim_sol.rst @@ -42,7 +42,7 @@ A - Direct Matplotlib with SNS Data # Add a legend with the chosen labels and show the plot axes.legend() - #plt.show() #uncomment to show the plot + #fig.show() #uncomment to show the plot # Note with the Direct Matplotlib method, # there are many more options for formatting the plot @@ -148,4 +148,5 @@ C - 2D and 3D Plot ILL Data ax3d[0].set_title("Surface") ax3d[1].set_title("Wireframe") - #plt.show()# uncomment to show the plots + #figC.show()# uncomment to show the plots + #fig3d.show() From 56a3cabfae9be1d8a4a743e90697c377392cf4a2 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Tue, 7 May 2024 14:37:10 +0100 Subject: [PATCH 2/2] Increased noise in data for clearer comparison with fit - Previous noise was so small that Z and ZFit looked identical - With increased noise it's clear that one is a smooth fit to the noisy data --- docs/source/fitting/fitfunctions/BivariateGaussian.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/fitting/fitfunctions/BivariateGaussian.rst b/docs/source/fitting/fitfunctions/BivariateGaussian.rst index 4497445ac15d..ba190ccce9e0 100644 --- a/docs/source/fitting/fitfunctions/BivariateGaussian.rst +++ b/docs/source/fitting/fitfunctions/BivariateGaussian.rst @@ -53,7 +53,7 @@ Here is an example of fitting a 2D histogram: x, y = np.mgrid[-0.5:0.5:.01, -0.5:0.5:.01] pos = np.dstack((x, y)) Z = rv.pdf(pos) - Z += 0.1*(np.random.random(x.shape) - 0.5) # Noise + Z += 1.5*(np.random.random(x.shape) - 0.5) # Noise # Here we'll format it so we can fit this as a 1D function: ZForFitting = np.empty(Z.shape + (2,))