Skip to content

Commit

Permalink
Switch to using fig.show() from plt.show()
Browse files Browse the repository at this point in the history
Since upgrading to matplotlib 3.7, plt.show() has sometimes created freezing / hanging behaviour when run from within workbench
  • Loading branch information
jhaigh0 committed Apr 24, 2024
1 parent 3c30272 commit 5e15a6c
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 176 deletions.
2 changes: 1 addition & 1 deletion docs/source/algorithms/ConvertToReflectometryQ-v1.rst
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/algorithms/ConvertToReflectometryQ-v2.rst
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/algorithms/CopySample-v1.rst
Expand Up @@ -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()



Expand Down
12 changes: 6 additions & 6 deletions docs/source/algorithms/HB2AReduce-v1.rst
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
10 changes: 6 additions & 4 deletions docs/source/algorithms/LagrangeILLReduction-v1.rst
Expand Up @@ -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**
Expand Down
2 changes: 1 addition & 1 deletion docs/source/algorithms/SetSample-v1.rst
Expand Up @@ -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::

Expand Down
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/source/concepts/HowToDefineGeometricShape.rst
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions docs/source/fitting/fitfunctions/BivariateGaussian.rst
Expand Up @@ -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::
Expand Down
Expand Up @@ -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
Expand Down
67 changes: 7 additions & 60 deletions docs/source/plotting/1DPlotsHelp.rst
Expand Up @@ -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)
Expand All @@ -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`

Expand Down Expand Up @@ -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 *
Expand Down Expand Up @@ -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`

Expand Down
6 changes: 4 additions & 2 deletions docs/source/plotting/3DPlotsHelp.rst
Expand Up @@ -83,7 +83,8 @@ Basic example of plotting a `Surface <https://matplotlib.org/mpl_toolkits/mplot3

fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
ax.plot_surface(data, cmap='viridis')
plt.show()

fig.show()

For more advice: :ref:`02_scripting_plots`

Expand Down Expand Up @@ -150,7 +151,8 @@ Basic example of plotting a `Wireframe <https://matplotlib.org/mpl_toolkits/mplo

fig, ax = plt.subplots(subplot_kw={'projection':'mantid3d'})
ax.plot_wireframe(data, color='#1f77b4')
plt.show()

fig.show()

For more advice: :ref:`02_scripting_plots`

Expand Down
30 changes: 4 additions & 26 deletions docs/source/plotting/ColorfillPlotsHelp.rst
Expand Up @@ -68,31 +68,8 @@ Scripting

Click the generate a script button |GenerateAScript.png| on a `Colorfill Plot <https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.imshow.html>`_:

.. 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 *
Expand All @@ -116,7 +93,7 @@ Click the generate a script button |GenerateAScript.png| on a `Colorfill Plot <h
axes.set_xlim([5.0, 19992.0])
axes.set_ylim([0.5, 922.5])

plt.show()
fig.show()

For more advice:

Expand Down Expand Up @@ -200,7 +177,8 @@ Basic example of plotting a `Contour Plot <https://matplotlib.org/api/_as_gen/ma
cbar=fig.colorbar(c)
cbar.set_label('Counts ($\mu s$)$^{-1}$') #add text to colorbar
fig.tight_layout()
plt.show()

fig.show()

For more advice:

Expand Down
4 changes: 2 additions & 2 deletions docs/source/plotting/MeshPlotHelp.rst
Expand Up @@ -114,7 +114,7 @@ Note Component index 0 is usually the Container.
sample = ws.getInstrument().getSample().getPos() - source
arrow(axes, sample, origin=(0,0,-0.04))
axes.view_init(vertical_axis='y', elev=30, azim=-135)
plt.show()
fig.show()


Plot a cuboid sample shape, rotate it by the goniometer and add lattice vector arrows.
Expand Down Expand Up @@ -192,7 +192,7 @@ Plot a cuboid sample shape, rotate it by the goniometer and add lattice vector a
arrow(axes, reciprocal_lattice[:,i], color = colors[i], linestyle = '--')

axes.view_init(vertical_axis='y', elev=27, azim=50)
plt.show()
fig.show()


**Other Plotting Documentation**
Expand Down
2 changes: 1 addition & 1 deletion docs/source/plotting/WaterfallPlotsHelp.rst
Expand Up @@ -132,7 +132,7 @@ An example script for a Waterfall Plot:
# Update the offsets
ax.update_waterfall(x_offset=10, y_offset=30)

plt.show()
fig.show()

For more advice: :ref:`02_scripting_plots`

Expand Down
3 changes: 2 additions & 1 deletion docs/source/release/v5.1.0/mantidworkbench.rst
Expand Up @@ -92,7 +92,8 @@ Plotting
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()

- The Advanced Plotting menu is now in Workbench. This enables creating surface and contour plots of three or more workspaces, and choosing which log value to plot against.

Expand Down

0 comments on commit 5e15a6c

Please sign in to comment.