File tree Expand file tree Collapse file tree 2 files changed +14
-7
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +14
-7
lines changed
Original file line number Diff line number Diff line change @@ -657,7 +657,7 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
657
657
658
658
Parameters
659
659
----------
660
- num : int or str, optional
660
+ num : int or str or `.Figure` , optional
661
661
A unique identifier for the figure.
662
662
663
663
If a figure with that identifier already exists, this figure is made
@@ -728,6 +728,11 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
728
728
`~matplotlib.rcParams` defines the default values, which can be modified
729
729
in the matplotlibrc file.
730
730
"""
731
+ if isinstance (num , Figure ):
732
+ if num .canvas .manager is None :
733
+ raise ValueError ("The passed figure is not managed by pyplot" )
734
+ _pylab_helpers .Gcf .set_active (num .canvas .manager )
735
+ return num
731
736
732
737
allnums = get_fignums ()
733
738
next_num = max (allnums ) + 1 if allnums else 1
@@ -1055,9 +1060,7 @@ def sca(ax):
1055
1060
"""
1056
1061
Set the current Axes to *ax* and the current Figure to the parent of *ax*.
1057
1062
"""
1058
- if not hasattr (ax .figure .canvas , "manager" ):
1059
- raise ValueError ("Axes parent figure is not managed by pyplot" )
1060
- _pylab_helpers .Gcf .set_active (ax .figure .canvas .manager )
1063
+ figure (ax .figure )
1061
1064
ax .figure .sca (ax )
1062
1065
1063
1066
Original file line number Diff line number Diff line change 10
10
from matplotlib import cbook , rcParams
11
11
from matplotlib .testing .decorators import image_comparison , check_figures_equal
12
12
from matplotlib .axes import Axes
13
+ from matplotlib .figure import Figure
13
14
from matplotlib .ticker import AutoMinorLocator , FixedFormatter , ScalarFormatter
14
15
import matplotlib .pyplot as plt
15
16
import matplotlib .dates as mdates
@@ -60,10 +61,9 @@ def test_align_labels():
60
61
61
62
62
63
def test_figure_label ():
63
- # pyplot figure creation, selection and closing with figure label and
64
- # number
64
+ # pyplot figure creation, selection, and closing with label/number/instance
65
65
plt .close ('all' )
66
- plt .figure ('today' )
66
+ fig_today = plt .figure ('today' )
67
67
plt .figure (3 )
68
68
plt .figure ('tomorrow' )
69
69
plt .figure ()
@@ -78,6 +78,10 @@ def test_figure_label():
78
78
plt .close ('tomorrow' )
79
79
assert plt .get_fignums () == [0 , 1 ]
80
80
assert plt .get_figlabels () == ['' , 'today' ]
81
+ plt .figure (fig_today )
82
+ assert plt .gcf () == fig_today
83
+ with pytest .raises (ValueError ):
84
+ plt .figure (Figure ())
81
85
82
86
83
87
def test_fignum_exists ():
You can’t perform that action at this time.
0 commit comments