Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0e13c82

Browse filesBrowse files
committed
ENH: Allow to register standalone figures with pyplot
It may be fundamentally nice not to have to create the figure though pyplot to be able to use it in pyplot afterwards. You can now do ``` from matplotlib.figure import Figure import matplotlib.pyplot as plt fig = Figure() fig.subplots().plot([1, 3, 2]) plt.figure(fig) # fig is now tracked in pyplot plt.show() ``` This also opens up the possibility to more dynamically track and untrack figures in pyplot, which opens up the road to optimized figure tracking in pyplot (#29849)
1 parent 77993d5 commit 0e13c82
Copy full SHA for 0e13c82

File tree

2 files changed

+39
-4
lines changed
Filter options

2 files changed

+39
-4
lines changed

‎lib/matplotlib/pyplot.py

Copy file name to clipboardExpand all lines: lib/matplotlib/pyplot.py
+15-4Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -990,21 +990,32 @@ def figure(
990990
in the matplotlibrc file.
991991
"""
992992
allnums = get_fignums()
993+
next_num = max(allnums) + 1 if allnums else 1
993994

994995
if isinstance(num, FigureBase):
995996
# type narrowed to `Figure | SubFigure` by combination of input and isinstance
997+
has_figure_property_parameters = (
998+
any(param is not None for param in [figsize, dpi, facecolor, edgecolor])
999+
or not frameon or kwargs
1000+
)
1001+
9961002
root_fig = num.get_figure(root=True)
9971003
if root_fig.canvas.manager is None:
998-
raise ValueError("The passed figure is not managed by pyplot")
999-
elif (any(param is not None for param in [figsize, dpi, facecolor, edgecolor])
1000-
or not frameon or kwargs) and root_fig.canvas.manager.num in allnums:
1004+
if has_figure_property_parameters:
1005+
raise ValueError(
1006+
"You cannot pass figure properties when calling figure() with "
1007+
"an existing Figure instance")
1008+
backend = _get_backend_mod()
1009+
manager_ = backend.new_figure_manager_given_figure(next_num, root_fig)
1010+
_pylab_helpers.Gcf._set_new_active_manager(manager_)
1011+
return manager_.canvas.figure
1012+
elif has_figure_property_parameters and root_fig.canvas.manager.num in allnums:
10011013
_api.warn_external(
10021014
"Ignoring specified arguments in this call because figure "
10031015
f"with num: {root_fig.canvas.manager.num} already exists")
10041016
_pylab_helpers.Gcf.set_active(root_fig.canvas.manager)
10051017
return root_fig
10061018

1007-
next_num = max(allnums) + 1 if allnums else 1
10081019
fig_label = ''
10091020
if num is None:
10101021
num = next_num

‎lib/matplotlib/tests/test_pyplot.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_pyplot.py
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,30 @@ def test_multiple_same_figure_calls():
470470
assert fig is fig3
471471

472472

473+
def test_register_existing_figure_with_pyplot():
474+
from matplotlib.figure import Figure
475+
# start with a standalone figure
476+
fig = Figure()
477+
assert fig.canvas.manager is None
478+
with pytest.raises(AttributeError):
479+
# Heads-up: This will change to returning None in the future
480+
# See docstring for the Figure.number property
481+
fig.number
482+
# register the Figure with pyplot
483+
plt.figure(fig)
484+
assert fig.number == 1
485+
# the figure can now be used in pyplot
486+
plt.suptitle("my title")
487+
assert fig.get_suptitle() == "my title"
488+
# it also has a manager that is properly wired up in the pyplot state
489+
assert plt._pylab_helpers.Gcf.get_fig_manager(fig.number) is fig.canvas.manager
490+
# and we can regularly switch the pyplot state
491+
fig2 = plt.figure()
492+
assert fig2.number == 2
493+
assert plt.figure(1) is fig
494+
assert plt.gcf() is fig
495+
496+
473497
def test_close_all_warning():
474498
fig1 = plt.figure()
475499

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.