diff --git a/doc/users/whats_new/updated_pyplot.rst b/doc/users/whats_new/updated_pyplot.rst new file mode 100644 index 000000000000..7e525c9f761a --- /dev/null +++ b/doc/users/whats_new/updated_pyplot.rst @@ -0,0 +1,7 @@ +Updated fignum_exists to take figure name +------------------------------------------- +Added the ability to check the existence of a figure using it's name +instead of just the figure number. +Example: + figure('figure') + fignum_exists('figure') #true \ No newline at end of file diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index a13d87ee2ba0..03e3b05e927a 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -460,7 +460,9 @@ def gcf(): else: return figure() -fignum_exists = _pylab_helpers.Gcf.has_fignum +def fignum_exists(num): + allLabels = get_figlabels() + return _pylab_helpers.Gcf.has_fignum(num) or num in allLabels def get_fignums(): diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 0731205ed051..2f5fb4d68e2b 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -32,6 +32,23 @@ def test_figure_label(): assert_equal(plt.get_figlabels(), ['', 'today']) +@cleanup +def test_fignum_exists(): + # pyplot figure creation, selection and closing with fignum_exists + plt.figure('one') + plt.figure(2) + plt.figure('three') + plt.figure() + assert_equal(plt.fignum_exists('one'), True) + assert_equal(plt.fignum_exists(2), True) + assert_equal(plt.fignum_exists('three'), True) + assert_equal(plt.fignum_exists(4), True) + plt.close('one') + plt.close(4) + assert_equal(plt.fignum_exists('one'), False) + assert_equal(plt.fignum_exists(4), False) + + @image_comparison(baseline_images=['figure_today']) def test_figure(): # named figure support