Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a53074

Browse files
committed
Added figure as a property and deprecated setters and getters
1 parent fbbbc51 commit 1a53074

25 files changed

+129
-74
lines changed

lib/matplotlib/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,7 @@ def test(verbosity=1):
15001500
finally:
15011501
if old_backend.lower() != 'agg':
15021502
use(old_backend)
1503+
return success
15031504

15041505
def deprecated_get_set(function, to_use):
15051506
"""Fuction to deprecate the getters and setter for a class
@@ -1512,10 +1513,9 @@ def deprecated_get_set(function, to_use):
15121513
- to_use: string
15131514
The argument to use instead of the deprecated function
15141515
"""
1515-
msg = "{} is deprecated, please use the {} argument"
1516+
msg = "{} is deprecated, please use the `{}` argument"
15161517
msg = msg.format(function.__name__, to_use)
15171518
warnings.warn(msg, mplDeprecation, stacklevel=1)
1518-
return success
15191519

15201520
test.__test__ = False # nose: this function is not a test
15211521

lib/matplotlib/artist.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import matplotlib
1010
import matplotlib.cbook as cbook
1111
from matplotlib.cbook import mplDeprecation
12-
from matplotlib import docstring, rcParams
12+
from matplotlib import docstring, rcParams, deprecated_get_set
1313
from .transforms import (Bbox, IdentityTransform, TransformedBbox,
1414
TransformedPath, Transform)
1515
from .path import Path
@@ -88,7 +88,7 @@ class Artist(object):
8888
def __init__(self):
8989
self._stale = True
9090
self._axes = None
91-
self.figure = None
91+
self._figure = None
9292

9393
self._transform = None
9494
self._transformSet = False
@@ -594,11 +594,27 @@ def set_path_effects(self, path_effects):
594594
def get_path_effects(self):
595595
return self._path_effects
596596

597+
@property
598+
def figure(self):
599+
""":class:`~matplotlib.figure.Figure` instance the artist
600+
belongs to"""
601+
return self._figure
602+
603+
@figure.setter
604+
def figure(self, fig):
605+
self._figure = fig
606+
if self._figure and self._figure is not self:
607+
self.add_callback(_stale_figure_callback)
608+
self.pchanged()
609+
self.stale = True
610+
611+
597612
def get_figure(self):
598613
"""
599614
Return the :class:`~matplotlib.figure.Figure` instance the
600615
artist belongs to.
601616
"""
617+
deprecated_get_set(self.get_figure, "figure")
602618
return self.figure
603619

604620
def set_figure(self, fig):
@@ -608,11 +624,8 @@ def set_figure(self, fig):
608624
609625
ACCEPTS: a :class:`matplotlib.figure.Figure` instance
610626
"""
627+
deprecated_get_set(self.set_figure, "figure")
611628
self.figure = fig
612-
if self.figure and self.figure is not self:
613-
self.add_callback(_stale_figure_callback)
614-
self.pchanged()
615-
self.stale = True
616629

617630
def set_clip_box(self, clipbox):
618631
"""

lib/matplotlib/axes/_base.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def __init__(self, fig, rect,
421421
# 'shared axes: "adjustable" is being changed to "datalim"')
422422
self._adjustable = 'datalim'
423423
self.set_label(label)
424-
self.set_figure(fig)
424+
self.figure = fig
425425

426426
self.set_axes_locator(kwargs.get("axes_locator", None))
427427

@@ -500,16 +500,23 @@ def set_figure(self, fig):
500500
501501
accepts a class:`~matplotlib.figure.Figure` instance
502502
"""
503-
martist.Artist.set_figure(self, fig)
503+
# import ipdb; ipdb.set_trace()
504+
self.figure = fig
504505

506+
@martist.Artist.figure.getter
507+
def figure(self):
508+
return self._figure
509+
510+
@martist.Artist.figure.setter
511+
def figure(self, fig):
512+
martist.Artist.figure.__set__(self, fig)
505513
self.bbox = mtransforms.TransformedBbox(self._position,
506514
fig.transFigure)
507515
# these will be updated later as data is added
508516
self.dataLim = mtransforms.Bbox.null()
509517
self.viewLim = mtransforms.Bbox.unit()
510518
self.transScale = mtransforms.TransformWrapper(
511519
mtransforms.IdentityTransform())
512-
513520
self._set_lim_and_transforms()
514521

515522
def _set_lim_and_transforms(self):
@@ -781,7 +788,7 @@ def get_axes_locator(self):
781788

782789
def _set_artist_props(self, a):
783790
"""set the boilerplate props for artists added to axes"""
784-
a.set_figure(self.figure)
791+
a.figure = self.figure
785792
if not a.is_transform_set():
786793
a.set_transform(self.transData)
787794

@@ -959,7 +966,7 @@ def cla(self):
959966
# deprecated. We use the frame to draw the edges so we are
960967
# setting the edgecolor to None
961968
self.patch = self.axesPatch = self._gen_axes_patch()
962-
self.patch.set_figure(self.figure)
969+
self.patch.figure = self.figure
963970
self.patch.set_facecolor(self._axisbg)
964971
self.patch.set_edgecolor('None')
965972
self.patch.set_linewidth(0)
@@ -1211,7 +1218,7 @@ def apply_aspect(self, position=None):
12111218
warnings.warn(
12121219
'shared axes: "adjustable" is being changed to "datalim"')
12131220

1214-
figW, figH = self.get_figure().get_size_inches()
1221+
figW, figH = self.figure.get_size_inches()
12151222
fig_aspect = figH / figW
12161223
if self._adjustable in ['box', 'box-forced']:
12171224
if aspect_scale_mode == "log":

lib/matplotlib/axes/_subplots.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def __init__(self, fig, *args, **kwargs):
3535
decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*.
3636
"""
3737

38-
self.figure = fig
38+
self._figure = fig
39+
# self._figure = None
40+
# self.figure = fig
3941

4042
if len(args) == 1:
4143
if isinstance(args[0], SubplotSpec):
@@ -86,6 +88,15 @@ def not_subplotbase(c):
8688
self.__getstate__()]
8789
return tuple(r)
8890

91+
# @property
92+
# def figure(self):
93+
# """:class:`~matplotlib.figure.Figure` the subplot is set to"""
94+
# return self._figure
95+
96+
# @figure.setter
97+
# def figure(self, fig):
98+
# self._figure = fig
99+
89100
def get_geometry(self):
90101
"""get the subplot geometry, e.g., 2,2,3"""
91102
rows, cols, num1, num2 = self.get_subplotspec().get_geometry()

lib/matplotlib/axis.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def __init__(self, axes, loc, label,
9797
else:
9898
gridOn = False
9999

100-
self.set_figure(axes.figure)
100+
self.figure = axes.figure
101101
self.axes = axes
102102

103103
name = self.__name__.lower()
@@ -277,7 +277,7 @@ def set_label2(self, s):
277277
self.stale = True
278278

279279
def _set_artist_props(self, a):
280-
a.set_figure(self.figure)
280+
a.figure = self.figure
281281

282282
def get_view_interval(self):
283283
'return the view Interval instance for the axis this tick is ticking'
@@ -625,7 +625,7 @@ def __init__(self, axes, pickradius=15):
625625
Init the axis with the parent Axes instance
626626
"""
627627
artist.Artist.__init__(self)
628-
self.set_figure(axes.figure)
628+
self.figure = axes.figure
629629

630630
# Keep track of setting to the default value, this allows use to know
631631
# if any of the following values is explicitly set by the user, so as
@@ -883,7 +883,7 @@ def set_default_intervals(self):
883883
def _set_artist_props(self, a):
884884
if a is None:
885885
return
886-
a.set_figure(self.figure)
886+
a.figure = self.figure
887887

888888
def iter_ticks(self):
889889
"""

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def apply_callback(data):
154154
new_legend.draggable(draggable)
155155

156156
# Redraw
157-
figure = axes.get_figure()
157+
figure = axes.figure
158158
figure.canvas.draw()
159159

160160
data = formlayout.fedit(datalist, title="Figure options", parent=parent,

lib/matplotlib/colorbar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,8 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
10951095
if not isinstance(parents, (list, tuple)):
10961096
parents = [parents]
10971097

1098-
fig = parents[0].get_figure()
1099-
if not all(fig is ax.get_figure() for ax in parents):
1098+
fig = parents[0].figure
1099+
if not all(fig is ax.figure for ax in parents):
11001100
raise ValueError('Unable to create a colorbar axes as not all '
11011101
'parents share the same figure.')
11021102

@@ -1232,7 +1232,7 @@ def make_axes_gridspec(parent, **kw):
12321232
parent.set_position(parent.figbox)
12331233
parent.set_anchor(panchor)
12341234

1235-
fig = parent.get_figure()
1235+
fig = parent.figure
12361236
cax = fig.add_subplot(gs2[1])
12371237
cax.set_aspect(aspect, anchor=anchor, adjustable='box')
12381238
return cax, kw

lib/matplotlib/figure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ def add_axes(self, *args, **kwargs):
888888

889889
if isinstance(args[0], Axes):
890890
a = args[0]
891-
if a.get_figure() is not self:
891+
if a.figure is not self:
892892
msg = "The Axes must have been created in the present figure"
893893
raise ValueError(msg)
894894
else:
@@ -965,7 +965,7 @@ def add_subplot(self, *args, **kwargs):
965965
if isinstance(args[0], SubplotBase):
966966

967967
a = args[0]
968-
if a.get_figure() is not self:
968+
if a.figure is not self:
969969
msg = ("The Subplot must have been created in the present"
970970
" figure")
971971
raise ValueError(msg)
@@ -1271,7 +1271,7 @@ def text(self, x, y, s, *args, **kwargs):
12711271

12721272
def _set_artist_props(self, a):
12731273
if a != self:
1274-
a.set_figure(self)
1274+
a.figure = self
12751275
a.set_transform(self.transFigure)
12761276

12771277
@docstring.dedent_interpd

lib/matplotlib/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ def __init__(self, bbox,
11271127

11281128
def get_window_extent(self, renderer=None):
11291129
if renderer is None:
1130-
renderer = self.get_figure()._cachedRenderer
1130+
renderer = self.figure._cachedRenderer
11311131

11321132
if isinstance(self.bbox, BboxBase):
11331133
return self.bbox
@@ -1141,7 +1141,7 @@ def contains(self, mouseevent):
11411141
if six.callable(self._contains):
11421142
return self._contains(self, mouseevent)
11431143

1144-
if not self.get_visible(): # or self.get_figure()._renderer is None:
1144+
if not self.get_visible(): # or self.figure._renderer is None:
11451145
return False, {}
11461146

11471147
x, y = mouseevent.x, mouseevent.y

lib/matplotlib/legend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ def __init__(self, parent, handles, labels,
304304
if isinstance(parent, Axes):
305305
self.isaxes = True
306306
self.axes = parent
307-
self.set_figure(parent.figure)
307+
self.figure = parent.figure
308308
elif isinstance(parent, Figure):
309309
self.isaxes = False
310-
self.set_figure(parent)
310+
self.figure = parent
311311
else:
312312
raise TypeError("Legend needs either Axes or Figure as parent")
313313
self.parent = parent
@@ -398,7 +398,7 @@ def _set_artist_props(self, a):
398398
"""
399399
set the boilerplate props for artists added to axes
400400
"""
401-
a.set_figure(self.figure)
401+
a.figure = self.figure
402402
if self.isaxes:
403403
# a.set_axes(self.axes)
404404
a.axes = self.axes
@@ -714,7 +714,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
714714
align="center",
715715
children=[self._legend_title_box,
716716
self._legend_handle_box])
717-
self._legend_box.set_figure(self.figure)
717+
self._legend_box.figure = self.figure
718718
self.texts = text_list
719719
self.legendHandles = handle_list
720720

lib/matplotlib/legend_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def update_prop(self, legend_handle, orig_handle, legend):
327327

328328
self._update_prop(legend_handle, orig_handle)
329329

330-
legend_handle.set_figure(legend.figure)
330+
legend_handle.figure = legend.figure
331331
#legend._set_artist_props(legend_handle)
332332
legend_handle.set_clip_box(None)
333333
legend_handle.set_clip_path(None)
@@ -610,7 +610,7 @@ def get_first(prop_array):
610610
legend_handle.set_linewidth(get_first(orig_handle.get_linewidths()))
611611
legend_handle.set_linestyle(get_first(orig_handle.get_linestyles()))
612612
legend_handle.set_transform(get_first(orig_handle.get_transforms()))
613-
legend_handle.set_figure(orig_handle.get_figure())
613+
legend_handle.figure = orig_handle.figure
614614
legend_handle.set_alpha(orig_handle.get_alpha())
615615

616616
def create_artists(self, legend, orig_handle,

lib/matplotlib/offsetbox.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ def set_figure(self, fig):
183183
184184
accepts a class:`~matplotlib.figure.Figure` instance
185185
"""
186-
martist.Artist.set_figure(self, fig)
186+
self.figure = fig
187+
188+
@martist.Artist.figure.setter
189+
def figure(self, fig):
190+
martist.Artist.figure.__set__(self, fig)
187191
for c in self.get_children():
188-
c.set_figure(fig)
192+
c.figure = fig
189193

190194
def contains(self, mouseevent):
191195
for c in self.get_children():
@@ -1460,11 +1464,15 @@ def get_children(self):
14601464
return children
14611465

14621466
def set_figure(self, fig):
1467+
self.figure = fig
14631468

1469+
@martist.Artist.figure.setter
1470+
def figure(self, fig):
14641471
if self.arrow_patch is not None:
1465-
self.arrow_patch.set_figure(fig)
1466-
self.offsetbox.set_figure(fig)
1467-
martist.Artist.set_figure(self, fig)
1472+
self.arrow_patch.figure = fig
1473+
self.offsetbox.figure = fig
1474+
martist.Artist.figure.__set__(self, fig)
1475+
14681476

14691477
def set_fontsize(self, s=None):
14701478
"""

lib/matplotlib/patches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def update_from(self, other):
177177
self.set_linewidth(other.get_linewidth())
178178
self.set_linestyle(other.get_linestyle())
179179
self.set_transform(other.get_data_transform())
180-
self.set_figure(other.get_figure())
180+
self.figure = other.figure
181181
self.set_alpha(other.get_alpha())
182182

183183
def get_extents(self):

lib/matplotlib/quiver.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def _init(self):
309309
if self.color is not None:
310310
self.vector.set_color(self.color)
311311
self.vector.set_transform(self.Q.get_transform())
312-
self.vector.set_figure(self.get_figure())
312+
self.vector.figure = self.figure
313313
self._initialized = True
314314

315315
def _text_x(self, x):
@@ -351,8 +351,12 @@ def _set_transform(self):
351351
raise ValueError('unrecognized coordinates')
352352

353353
def set_figure(self, fig):
354-
martist.Artist.set_figure(self, fig)
355-
self.text.set_figure(fig)
354+
self.figure = fig
355+
356+
@martist.Artist.figure.setter
357+
def figure(self, fig):
358+
martist.Artist.figure.__set__(self, fig)
359+
self.text.figure = fig
356360

357361
def contains(self, mouseevent):
358362
# Maybe the dictionary should allow one to

0 commit comments

Comments
 (0)