Skip to content

Figure property #4842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,25 @@ def test(verbosity=1):

return success


def deprecated_get_set(fclass, function, to_use):
"""Fuction to deprecate the getters and setter for a class
argument.

Parameter
---------
- fclass: class
The class of the function to deprecate
- function: function
The function to deprecate.
- to_use: string
The argument to use instead of the deprecated function
"""
msg = "{}.{} is deprecated, please use the `{}` argument"
msg = msg.format(fclass.__name__, function.__name__, to_use)
warnings.warn(msg, mplDeprecation, stacklevel=1)


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

verbose.report('matplotlib version %s' % __version__)
Expand Down
24 changes: 18 additions & 6 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import matplotlib
import matplotlib.cbook as cbook
from matplotlib.cbook import mplDeprecation
from matplotlib import docstring, rcParams
from matplotlib import docstring, rcParams, deprecated_get_set
from .transforms import (Bbox, IdentityTransform, TransformedBbox,
TransformedPath, Transform)
from .path import Path
Expand Down Expand Up @@ -88,7 +88,7 @@ class Artist(object):
def __init__(self):
self._stale = True
self._axes = None
self.figure = None
self._figure = None

self._transform = None
self._transformSet = False
Expand Down Expand Up @@ -594,11 +594,26 @@ def set_path_effects(self, path_effects):
def get_path_effects(self):
return self._path_effects

@property
def figure(self):
""":class:`~matplotlib.figure.Figure` instance the artist
belongs to"""
return self._figure

@figure.setter
def figure(self, fig):
self._figure = fig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are going to do this you should add validation that artists can not be moved between figures.

if self._figure and self._figure is not self:
self.add_callback(_stale_figure_callback)
self.pchanged()
self.stale = True

def get_figure(self):
"""
Return the :class:`~matplotlib.figure.Figure` instance the
artist belongs to.
"""
deprecated_get_set(self.__class__, self.get_figure, "figure")
return self.figure

def set_figure(self, fig):
Expand All @@ -608,11 +623,8 @@ def set_figure(self, fig):

ACCEPTS: a :class:`matplotlib.figure.Figure` instance
"""
deprecated_get_set(self.__class__, self.set_figure, "figure")
self.figure = fig
if self.figure and self.figure is not self:
self.add_callback(_stale_figure_callback)
self.pchanged()
self.stale = True

def set_clip_box(self, clipbox):
"""
Expand Down
14 changes: 9 additions & 5 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def __init__(self, fig, rect,
# 'shared axes: "adjustable" is being changed to "datalim"')
self._adjustable = 'datalim'
self.set_label(label)
self.set_figure(fig)
self.figure = fig

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

Expand Down Expand Up @@ -500,8 +500,12 @@ def set_figure(self, fig):

accepts a class:`~matplotlib.figure.Figure` instance
"""
martist.Artist.set_figure(self, fig)
deprecated_get_set(self.__class__, self.set_figure, "figure")
self.figure = fig

@martist.Artist.figure.setter
def figure(self, fig):
martist.Artist.figure.__set__(self, fig)
self.bbox = mtransforms.TransformedBbox(self._position,
fig.transFigure)
# these will be updated later as data is added
Expand Down Expand Up @@ -781,7 +785,7 @@ def get_axes_locator(self):

def _set_artist_props(self, a):
"""set the boilerplate props for artists added to axes"""
a.set_figure(self.figure)
a.figure = self.figure
if not a.is_transform_set():
a.set_transform(self.transData)

Expand Down Expand Up @@ -959,7 +963,7 @@ def cla(self):
# deprecated. We use the frame to draw the edges so we are
# setting the edgecolor to None
self.patch = self.axesPatch = self._gen_axes_patch()
self.patch.set_figure(self.figure)
self.patch.figure = self.figure
self.patch.set_facecolor(self._axisbg)
self.patch.set_edgecolor('None')
self.patch.set_linewidth(0)
Expand Down Expand Up @@ -1211,7 +1215,7 @@ def apply_aspect(self, position=None):
warnings.warn(
'shared axes: "adjustable" is being changed to "datalim"')

figW, figH = self.get_figure().get_size_inches()
figW, figH = self.figure.get_size_inches()
fig_aspect = figH / figW
if self._adjustable in ['box', 'box-forced']:
if aspect_scale_mode == "log":
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/axes/_subplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, fig, *args, **kwargs):
decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*.
"""

self.figure = fig
self._figure = fig

if len(args) == 1:
if isinstance(args[0], SubplotSpec):
Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, axes, loc, label,
else:
gridOn = False

self.set_figure(axes.figure)
self.figure = axes.figure
self.axes = axes

name = self.__name__.lower()
Expand Down Expand Up @@ -277,7 +277,7 @@ def set_label2(self, s):
self.stale = True

def _set_artist_props(self, a):
a.set_figure(self.figure)
a.figure = self.figure

def get_view_interval(self):
'return the view Interval instance for the axis this tick is ticking'
Expand Down Expand Up @@ -625,7 +625,7 @@ def __init__(self, axes, pickradius=15):
Init the axis with the parent Axes instance
"""
artist.Artist.__init__(self)
self.set_figure(axes.figure)
self.figure = axes.figure

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

def iter_ticks(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/qt_editor/figureoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def apply_callback(data):
new_legend.draggable(draggable)

# Redraw
figure = axes.get_figure()
figure = axes.figure
figure.canvas.draw()

data = formlayout.fedit(datalist, title="Figure options", parent=parent,
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,8 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
if not isinstance(parents, (list, tuple)):
parents = [parents]

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

Expand Down Expand Up @@ -1232,7 +1232,7 @@ def make_axes_gridspec(parent, **kw):
parent.set_position(parent.figbox)
parent.set_anchor(panchor)

fig = parent.get_figure()
fig = parent.figure
cax = fig.add_subplot(gs2[1])
cax.set_aspect(aspect, anchor=anchor, adjustable='box')
return cax, kw
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ def add_axes(self, *args, **kwargs):

if isinstance(args[0], Axes):
a = args[0]
if a.get_figure() is not self:
if a.figure is not self:
msg = "The Axes must have been created in the present figure"
raise ValueError(msg)
else:
Expand Down Expand Up @@ -965,7 +965,7 @@ def add_subplot(self, *args, **kwargs):
if isinstance(args[0], SubplotBase):

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

def _set_artist_props(self, a):
if a != self:
a.set_figure(self)
a.figure = self
a.set_transform(self.transFigure)

@docstring.dedent_interpd
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ def __init__(self, bbox,

def get_window_extent(self, renderer=None):
if renderer is None:
renderer = self.get_figure()._cachedRenderer
renderer = self.figure._cachedRenderer

if isinstance(self.bbox, BboxBase):
return self.bbox
Expand All @@ -1141,7 +1141,7 @@ def contains(self, mouseevent):
if six.callable(self._contains):
return self._contains(self, mouseevent)

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

x, y = mouseevent.x, mouseevent.y
Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ def __init__(self, parent, handles, labels,
if isinstance(parent, Axes):
self.isaxes = True
self.axes = parent
self.set_figure(parent.figure)
self.figure = parent.figure
elif isinstance(parent, Figure):
self.isaxes = False
self.set_figure(parent)
self.figure = parent
else:
raise TypeError("Legend needs either Axes or Figure as parent")
self.parent = parent
Expand Down Expand Up @@ -398,7 +398,7 @@ def _set_artist_props(self, a):
"""
set the boilerplate props for artists added to axes
"""
a.set_figure(self.figure)
a.figure = self.figure
if self.isaxes:
# a.set_axes(self.axes)
a.axes = self.axes
Expand Down Expand Up @@ -714,7 +714,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
align="center",
children=[self._legend_title_box,
self._legend_handle_box])
self._legend_box.set_figure(self.figure)
self._legend_box.figure = self.figure
self.texts = text_list
self.legendHandles = handle_list

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def update_prop(self, legend_handle, orig_handle, legend):

self._update_prop(legend_handle, orig_handle)

legend_handle.set_figure(legend.figure)
legend_handle.figure = legend.figure
#legend._set_artist_props(legend_handle)
legend_handle.set_clip_box(None)
legend_handle.set_clip_path(None)
Expand Down Expand Up @@ -610,7 +610,7 @@ def get_first(prop_array):
legend_handle.set_linewidth(get_first(orig_handle.get_linewidths()))
legend_handle.set_linestyle(get_first(orig_handle.get_linestyles()))
legend_handle.set_transform(get_first(orig_handle.get_transforms()))
legend_handle.set_figure(orig_handle.get_figure())
legend_handle.figure = orig_handle.figure
legend_handle.set_alpha(orig_handle.get_alpha())

def create_artists(self, legend, orig_handle,
Expand Down
19 changes: 14 additions & 5 deletions lib/matplotlib/offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,14 @@ def set_figure(self, fig):

accepts a class:`~matplotlib.figure.Figure` instance
"""
martist.Artist.set_figure(self, fig)
deprecated_get_set(self.__class__, self.set_figure, "figure")
self.figure = fig

@martist.Artist.figure.setter
def figure(self, fig):
martist.Artist.figure.__set__(self, fig)
for c in self.get_children():
c.set_figure(fig)
c.figure = fig

def contains(self, mouseevent):
for c in self.get_children():
Expand Down Expand Up @@ -1460,11 +1465,15 @@ def get_children(self):
return children

def set_figure(self, fig):
deprecated_get_set(self.__class__, self.set_figure, "figure")
self.figure = fig

@martist.Artist.figure.setter
def figure(self, fig):
if self.arrow_patch is not None:
self.arrow_patch.set_figure(fig)
self.offsetbox.set_figure(fig)
martist.Artist.set_figure(self, fig)
self.arrow_patch.figure = fig
self.offsetbox.figure = fig
martist.Artist.figure.__set__(self, fig)

def set_fontsize(self, s=None):
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def update_from(self, other):
self.set_linewidth(other.get_linewidth())
self.set_linestyle(other.get_linestyle())
self.set_transform(other.get_data_transform())
self.set_figure(other.get_figure())
self.figure = other.figure
self.set_alpha(other.get_alpha())

def get_extents(self):
Expand Down
11 changes: 8 additions & 3 deletions lib/matplotlib/quiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def _init(self):
if self.color is not None:
self.vector.set_color(self.color)
self.vector.set_transform(self.Q.get_transform())
self.vector.set_figure(self.get_figure())
self.vector.figure = self.figure
self._initialized = True

def _text_x(self, x):
Expand Down Expand Up @@ -351,8 +351,13 @@ def _set_transform(self):
raise ValueError('unrecognized coordinates')

def set_figure(self, fig):
martist.Artist.set_figure(self, fig)
self.text.set_figure(fig)
deprecated_get_set(self.__class__, self.set_figure, "figure")
self.figure = fig

@martist.Artist.figure.setter
def figure(self, fig):
martist.Artist.figure.__set__(self, fig)
self.text.figure = fig

def contains(self, mouseevent):
# Maybe the dictionary should allow one to
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/spines.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, axes, spine_type, path, **kwargs):
"""
super(Spine, self).__init__(**kwargs)
self.axes = axes
self.set_figure(self.axes.figure)
self.figure = self.axes.figure
self.spine_type = spine_type
self.set_facecolor('none')
self.set_edgecolor(rcParams['axes.edgecolor'])
Expand Down
Loading