Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

PEP8 fixes on offsetbox.py #1400

Merged
merged 1 commit into from

3 participants

@NelleV
Collaborator

PEP8 fixes on the module offsetbox

Thanks,
N

lib/matplotlib/offsetbox.py
@@ -331,10 +330,10 @@ def get_extent_offsets(self, renderer):
if isinstance(c, PackerBase) and c.mode == "expand":
c.set_width(self.width)
- whd_list = [c.get_extent(renderer) for c in self.get_visible_children()]
+ whd_list = [c.get_extent(renderer) for c
+ in self.get_visible_children()]
@dmcdougall Collaborator

Could you put the for c on this line too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/offsetbox.py
@@ -389,7 +387,8 @@ def get_extent_offsets(self, renderer):
pad = self.pad * dpicor
sep = self.sep * dpicor
- whd_list = [c.get_extent(renderer) for c in self.get_visible_children()]
+ whd_list = [c.get_extent(renderer) for c
+ in self.get_visible_children()]
@dmcdougall Collaborator

Same here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/offsetbox.py
@@ -1332,7 +1335,8 @@ def update_positions(self, renderer):
self.arrow_patch.set_mutation_scale(mutation_scale)
def _update_position_xybox(self, renderer, xy_pixel):
- "Update the pixel positions of the annotation text and the arrow patch."
+ """Update the pixel positions of the annotation text and the arrow
+ patch."""
@dmcdougall Collaborator

I think this looks better:

"""
Update the pixel positions of the annotation text and the arrow
patch.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@NelleV
Collaborator

I've fixed the 3 issues mentionned above.

Cheers,
N

@pelson pelson merged commit e637e12 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 8, 2012
  1. @NelleV

    PEP8 fixes on offsetbox.py

    NelleV authored
This page is out of date. Refresh to see the latest.
Showing with 71 additions and 60 deletions.
  1. +71 −60 lib/matplotlib/offsetbox.py
View
131 lib/matplotlib/offsetbox.py
@@ -20,33 +20,34 @@
import matplotlib.artist as martist
import matplotlib.text as mtext
import numpy as np
-from matplotlib.transforms import Bbox, BboxBase, TransformedBbox, \
- IdentityTransform, BboxTransformFrom
+from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
from matplotlib.font_manager import FontProperties
from matplotlib.patches import FancyBboxPatch, FancyArrowPatch
from matplotlib import rcParams
from matplotlib import docstring
-import matplotlib.cbook as cbook
#from bboximage import BboxImage
from matplotlib.image import BboxImage
from matplotlib.patches import bbox_artist as mbbox_artist
+from matplotlib.text import _AnnotationBase
DEBUG = False
+
+
# for debuging use
def bbox_artist(*args, **kwargs):
if DEBUG:
mbbox_artist(*args, **kwargs)
-
# _get_packed_offsets() and _get_aligned_offsets() are coded assuming
# that we are packing boxes horizontally. But same function will be
# used with vertical packing.
+
def _get_packed_offsets(wd_list, total, sep, mode="fixed"):
"""
Geiven a list of (width, xdescent) of each boxes, calculate the
@@ -145,22 +146,22 @@ def __init__(self, *args, **kwargs):
def __getstate__(self):
state = martist.Artist.__getstate__(self)
-
+
# pickle cannot save instancemethods, so handle them here
from cbook import _InstanceMethodPickler
import inspect
-
+
offset = state['_offset']
if inspect.ismethod(offset):
state['_offset'] = _InstanceMethodPickler(offset)
return state
-
+
def __setstate__(self, state):
self.__dict__ = state
from cbook import _InstanceMethodPickler
if isinstance(self._offset, _InstanceMethodPickler):
self._offset = self._offset.get_instancemethod()
-
+
def set_figure(self, fig):
"""
Set the figure
@@ -249,7 +250,8 @@ def draw(self, renderer):
to the given *renderer*.
"""
- width, height, xdescent, ydescent, offsets = self.get_extent_offsets(renderer)
+ width, height, xdescent, ydescent, offsets = self.get_extent_offsets(
+ renderer)
px, py = self.get_offset(width, height, xdescent, ydescent, renderer)
@@ -315,8 +317,6 @@ def __init__(self, pad=None, sep=None, width=None, height=None,
align, mode,
children)
-
-
def get_extent_offsets(self, renderer):
"""
update offset of childrens and return the extents of the box
@@ -331,10 +331,10 @@ def get_extent_offsets(self, renderer):
if isinstance(c, PackerBase) and c.mode == "expand":
c.set_width(self.width)
- whd_list = [c.get_extent(renderer) for c in self.get_visible_children()]
+ whd_list = [c.get_extent(renderer)
+ for c in self.get_visible_children()]
whd_list = [(w, h, xd, (h - yd)) for w, h, xd, yd in whd_list]
-
wd_list = [(w, xd) for w, h, xd, yd in whd_list]
width, xdescent, xoffsets = _get_aligned_offsets(wd_list,
self.width,
@@ -351,7 +351,6 @@ def get_extent_offsets(self, renderer):
#w, h, xd, h_yd = whd_list[-1]
yoffsets = yoffsets - ydescent
-
return width + 2 * pad, height + 2 * pad, \
xdescent + pad, ydescent + pad, \
zip(xoffsets, yoffsets)
@@ -389,7 +388,8 @@ def get_extent_offsets(self, renderer):
pad = self.pad * dpicor
sep = self.sep * dpicor
- whd_list = [c.get_extent(renderer) for c in self.get_visible_children()]
+ whd_list = [c.get_extent(renderer)
+ for c in self.get_visible_children()]
if not whd_list:
return 2 * pad, 2 * pad, pad, pad, []
@@ -399,7 +399,7 @@ def get_extent_offsets(self, renderer):
ydescent = max([yd for w, h, xd, yd in whd_list])
height = height_descent + ydescent
else:
- height = self.height - 2 * pad # width w/o pad
+ height = self.height - 2 * pad # width w/o pad
hd_list = [(h, yd) for w, h, xd, yd in whd_list]
height, ydescent, yoffsets = _get_aligned_offsets(hd_list,
@@ -407,7 +407,7 @@ def get_extent_offsets(self, renderer):
self.align)
pack_list = [(w, xd) for w, h, xd, yd in whd_list]
-
+
width, xoffsets_ = _get_packed_offsets(pack_list, self.width,
sep, self.mode)
@@ -440,7 +440,7 @@ def __init__(self, child, pad=None, draw_frame=False, patch_attrs=None):
self.patch = FancyBboxPatch(
xy=(0.0, 0.0), width=1., height=1.,
facecolor='w', edgecolor='k',
- mutation_scale=1, #self.prop.get_size_in_points(),
+ mutation_scale=1, # self.prop.get_size_in_points(),
snap=True
)
@@ -471,7 +471,8 @@ def draw(self, renderer):
to the given *renderer*.
"""
- width, height, xdescent, ydescent, offsets = self.get_extent_offsets(renderer)
+ width, height, xdescent, ydescent, offsets = self.get_extent_offsets(
+ renderer)
px, py = self.get_offset(width, height, xdescent, ydescent, renderer)
@@ -563,7 +564,7 @@ def get_window_extent(self, renderer):
get the bounding box in display space.
'''
w, h, xd, yd = self.get_extent(renderer)
- ox, oy = self.get_offset() #w, h, xd, yd)
+ ox, oy = self.get_offset() # w, h, xd, yd)
return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h)
@@ -629,12 +630,12 @@ def __init__(self, s,
self._children = [self._text]
-
self.offset_transform = mtransforms.Affine2D()
self.offset_transform.clear()
self.offset_transform.translate(0, 0)
self._baseline_transform = mtransforms.Affine2D()
- self._text.set_transform(self.offset_transform + self._baseline_transform)
+ self._text.set_transform(self.offset_transform +
+ self._baseline_transform)
self._multilinebaseline = multilinebaseline
self._minimumdescent = minimumdescent
@@ -706,7 +707,7 @@ def get_window_extent(self, renderer):
get the bounding box in display space.
'''
w, h, xd, yd = self.get_extent(renderer)
- ox, oy = self.get_offset() #w, h, xd, yd)
+ ox, oy = self.get_offset() # w, h, xd, yd)
return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h)
def get_extent(self, renderer):
@@ -717,7 +718,7 @@ def get_extent(self, renderer):
bbox, info = self._text._get_layout(renderer)
w, h = bbox.width, bbox.height
- line = info[-1][0] # last line
+ line = info[-1][0] # last line
_, hh, dd = renderer.get_text_width_height_descent(
line, self._text._fontproperties, ismath=ismath)
d = dd # the baseline of the last line
@@ -729,7 +730,7 @@ def get_extent(self, renderer):
self._baseline_transform.translate(0, d - d_new)
d = d_new
- else: # single line
+ else: # single line
h_d = max(h_ - d_, h - d)
@@ -824,13 +825,13 @@ def get_window_extent(self, renderer):
get the bounding box in display space.
'''
w, h, xd, yd = self.get_extent(renderer)
- ox, oy = self.get_offset() #w, h, xd, yd)
+ ox, oy = self.get_offset() # w, h, xd, yd)
return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h)
def get_extent(self, renderer):
# clear the offset transforms
- _off = self.offset_transform.to_values() # to be restored later
+ _off = self.offset_transform.to_values() # to be restored later
self.ref_offset_transform.clear()
self.offset_transform.clear()
@@ -866,7 +867,7 @@ class AnchoredOffsetbox(OffsetBox):
the offset box is anchored against its parent axes. You may
explicitly specify the bbox_to_anchor.
"""
- zorder = 5 # zorder of the legend
+ zorder = 5 # zorder of the legend
def __init__(self, loc,
pad=0.4, borderpad=0.5,
@@ -1008,7 +1009,8 @@ def _update_offset_func(self, renderer, fontsize=None):
renderer (because of the padding).
"""
if fontsize is None:
- fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
+ fontsize = renderer.points_to_pixels(
+ self.prop.get_size_in_points())
def _offset(w, h, xd, yd, renderer, fontsize=fontsize, self=self):
bbox = Bbox.from_bounds(0, 0, w, h)
@@ -1033,7 +1035,8 @@ def update_frame(self, bbox, fontsize=None):
def draw(self, renderer):
"draw the artist"
- if not self.get_visible(): return
+ if not self.get_visible():
+ return
fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
self._update_offset_func(renderer, fontsize)
@@ -1044,7 +1047,6 @@ def draw(self, renderer):
self.update_frame(bbox, fontsize)
self.patch.draw(renderer)
-
width, height, xdescent, ydescent = self.get_extent(renderer)
px, py = self.get_offset(width, height, xdescent, ydescent, renderer)
@@ -1057,20 +1059,20 @@ def _get_anchored_bbox(self, loc, bbox, parentbbox, borderpad):
return the position of the bbox anchored at the parentbbox
with the loc code, with the borderpad.
"""
- assert loc in range(1, 11) # called only internally
+ assert loc in range(1, 11) # called only internally
BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = range(11)
- anchor_coefs = {UR:"NE",
- UL:"NW",
- LL:"SW",
- LR:"SE",
- R:"E",
- CL:"W",
- CR:"E",
- LC:"S",
- UC:"N",
- C:"C"}
+ anchor_coefs = {UR: "NE",
+ UL: "NW",
+ LL: "SW",
+ LR: "SE",
+ R: "E",
+ CL: "W",
+ CR: "E",
+ LC: "S",
+ UC: "N",
+ C: "C"}
c = anchor_coefs[loc]
@@ -1089,7 +1091,8 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5, prop=None, **kwargs):
*s* : string
*loc* : location code
*prop* : font property
- *pad* : pad between the text and the frame as fraction of the font size.
+ *pad* : pad between the text and the frame as fraction of the font
+ size.
*borderpad* : pad between the frame and the axes (or bbox_to_anchor).
other keyword parameters of AnchoredOffsetbox are also allowed.
@@ -1186,7 +1189,8 @@ def get_window_extent(self, renderer):
def get_extent(self, renderer):
- if self._dpi_cor: # True, do correction
+ # FIXME dpi_cor is never used
+ if self._dpi_cor: # True, do correction
dpi_cor = renderer.points_to_pixels(1.)
else:
dpi_cor = 1.
@@ -1206,9 +1210,6 @@ def draw(self, renderer):
#bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
-from matplotlib.text import _AnnotationBase
-
-
class AnnotationBbox(martist.Artist, _AnnotationBase):
"""
Annotation-like class, but with offsetbox instead of Text.
@@ -1217,12 +1218,13 @@ class AnnotationBbox(martist.Artist, _AnnotationBase):
def __str__(self):
return "AnnotationBbox(%g,%g)" % (self.xy[0], self.xy[1])
+
@docstring.dedent_interpd
def __init__(self, offsetbox, xy,
xybox=None,
xycoords='data',
boxcoords=None,
- frameon=True, pad=0.4, # BboxPatch
+ frameon=True, pad=0.4, # BboxPatch
annotation_clip=None,
box_alignment=(0.5, 0.5),
bboxprops=None,
@@ -1251,7 +1253,6 @@ def __init__(self, offsetbox, xy,
self.set_fontsize(fontsize)
-
if arrowprops is not None:
self._arrow_relpos = self.arrowprops.pop("relpos", (0.5, 0.5))
self.arrow_patch = FancyArrowPatch((0, 0), (1, 1),
@@ -1321,7 +1322,9 @@ def get_fontsize(self, s=None):
return self.prop.get_size_in_points()
def update_positions(self, renderer):
- "Update the pixel positions of the annotated point and the text."
+ """
+ Update the pixel positions of the annotated point and the text.
+ """
xy_pixel = self._get_position_xy(renderer)
self._update_position_xybox(renderer, xy_pixel)
@@ -1332,7 +1335,10 @@ def update_positions(self, renderer):
self.arrow_patch.set_mutation_scale(mutation_scale)
def _update_position_xybox(self, renderer, xy_pixel):
- "Update the pixel positions of the annotation text and the arrow patch."
+ """
+ Update the pixel positions of the annotation text and the arrow
+ patch.
+ """
x, y = self.xytext
if isinstance(self.textcoords, tuple):
@@ -1395,7 +1401,8 @@ def draw(self, renderer):
if renderer is not None:
self._renderer = renderer
- if not self.get_visible(): return
+ if not self.get_visible():
+ return
xy_pixel = self._get_position_xy(renderer)
@@ -1486,12 +1493,15 @@ def on_pick(self, evt):
if self._use_blit:
self.ref_artist.set_animated(True)
self.canvas.draw()
- self.background = self.canvas.copy_from_bbox(self.ref_artist.figure.bbox)
+ self.background = self.canvas.copy_from_bbox(
+ self.ref_artist.figure.bbox)
self.ref_artist.draw(self.ref_artist.figure._cachedRenderer)
self.canvas.blit(self.ref_artist.figure.bbox)
- self._c1 = self.canvas.mpl_connect('motion_notify_event', self.on_motion_blit)
+ self._c1 = self.canvas.mpl_connect('motion_notify_event',
+ self.on_motion_blit)
else:
- self._c1 = self.canvas.mpl_connect('motion_notify_event', self.on_motion)
+ self._c1 = self.canvas.mpl_connect('motion_notify_event',
+ self.on_motion)
self.save_offset()
def on_release(self, event):
@@ -1504,7 +1514,7 @@ def on_release(self, event):
self.ref_artist.set_animated(False)
def disconnect(self):
- 'disconnect the callbacks'
+ """disconnect the callbacks"""
for cid in self.cids:
self.canvas.mpl_disconnect(cid)
@@ -1573,12 +1583,14 @@ def update_offset(self, dx, dy):
ann = self.annotation
ann.xytext = self.ox + dx, self.oy + dy
x, y = ann.xytext
+ # xy is never used
xy = ann._get_xy(self.canvas.renderer, x, y, ann.textcoords)
def finalize_offset(self):
loc_in_canvas = self.annotation.xytext
self.annotation.textcoords = "axes fraction"
- pos_axes_fraction = self.annotation.axes.transAxes.inverted().transform_point(loc_in_canvas)
+ pos_axes_fraction = self.annotation.axes.transAxes.inverted()
+ pos_axes_fraction = pos_axes_fraction.transform_point(loc_in_canvas)
self.annotation.xytext = tuple(pos_axes_fraction)
@@ -1602,7 +1614,6 @@ def finalize_offset(self):
myimage.set_offset((100, 100))
-
myimage2 = OffsetImage(a,
zoom=2,
norm=None,
@@ -1613,13 +1624,13 @@ def finalize_offset(self):
xybox=(30, 30),
xycoords='data',
boxcoords="offset points",
- frameon=True, pad=0.4, # BboxPatch
+ frameon=True, pad=0.4, # BboxPatch
bboxprops=dict(boxstyle="round", fc="y"),
fontsize=None,
arrowprops=dict(arrowstyle="->"),
)
ax.add_artist(ann)
-
+
plt.draw()
plt.show()
Something went wrong with that request. Please try again.