Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

PEP8 fixes on contour.py #1395

Merged
merged 1 commit into from

4 participants

@NelleV
Collaborator

PEP8 fixes on module contour.

Thanks,
N.

lib/matplotlib/contour.py
((7 lines not shown))
else:
return [mpath.Path(seg) for seg in segs]
def changed(self):
- tcolors = [ (tuple(rgba),) for rgba in
- self.to_rgba(self.cvalues, alpha=self.alpha)]
+ tcolors = [(tuple(rgba),) for rgba
+ in self.to_rgba(self.cvalues, alpha=self.alpha)]
@dmcdougall Collaborator

Perhaps move the for rgba part of the list comprehension into this line?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dmcdougall dmcdougall commented on the diff
lib/matplotlib/contour.py
@@ -1341,8 +1365,9 @@ def _get_allsegs_and_allkinds(self):
lowers, uppers = self._get_lowers_and_uppers()
allkinds = []
for level, level_upper in zip(lowers, uppers):
- nlist = self.Cntr.trace(level, level_upper, nchunk = self.nchunk)
- nseg = len(nlist)//2
+ nlist = self.Cntr.trace(level, level_upper,
+ nchunk=self.nchunk)
+ nseg = len(nlist) // 2
@dmcdougall Collaborator

What does // do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@NelleV
Collaborator

Done,

Cheers,
N

lib/matplotlib/contour.py
((20 lines not shown))
artists.append(patch)
lower = str_format(lower)
upper = str_format(upper)
if i == 0 and self.extend in ('min', 'both'):
- labels.append(r'$%s \leq %s$' % (variable_name, lower, ))
- elif i == n_levels-1 and self.extend in ('max', 'both'):
- labels.append(r'$%s > %s$' % (variable_name, upper, ))
+ labels.append(r'$%s \leq %s$' % (variable_name,
+ lower, ))
+ elif i == n_levels - 1 and self.extend in ('max', 'both'):
+ labels.append(r'$%s > %s$' % (variable_name,
+ upper, ))
@efiring Owner
efiring added a note

Looks like the trailing commas are just a distraction; you might as well go ahead and remove them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@NelleV
Collaborator

I've removed the unnecessary commas

@dmcdougall
Collaborator

Thanks. Does anyone else have any feedback for @NelleV?

@efiring efiring merged commit 2044223 into matplotlib:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 9, 2012
  1. @NelleV

    PEP8 fixes on contour.py

    NelleV authored
This page is out of date. Refresh to see the latest.
Showing with 213 additions and 184 deletions.
  1. +213 −184 lib/matplotlib/contour.py
View
397 lib/matplotlib/contour.py
@@ -152,7 +152,7 @@ def clabel(self, *args, **kwargs):
self._use_clabeltext = kwargs.get('use_clabeltext', False)
# Detect if manual selection is desired and remove from argument list
- self.labelManual=kwargs.get('manual',False)
+ self.labelManual = kwargs.get('manual', False)
self.rightside_up = kwargs.get('rightside_up', True)
if len(args) == 0:
@@ -176,7 +176,7 @@ def clabel(self, *args, **kwargs):
self.labelIndiceList = indices
self.labelFontProps = font_manager.FontProperties()
- if fontsize == None:
+ if fontsize is None:
font_size = int(self.labelFontProps.get_size_in_points())
else:
if type(fontsize) not in [int, float, str]:
@@ -190,21 +190,21 @@ def clabel(self, *args, **kwargs):
font_size = fontsize
self.labelFontSizeList = [font_size] * len(levels)
- if _colors == None:
+ if _colors is None:
self.labelMappable = self
self.labelCValueList = np.take(self.cvalues, self.labelIndiceList)
else:
cmap = colors.ListedColormap(_colors, N=len(self.labelLevelList))
self.labelCValueList = range(len(self.labelLevelList))
- self.labelMappable = cm.ScalarMappable(cmap = cmap,
- norm = colors.NoNorm())
+ self.labelMappable = cm.ScalarMappable(cmap=cmap,
+ norm=colors.NoNorm())
#self.labelTexts = [] # Initialized in ContourSet.__init__
#self.labelCValues = [] # same
self.labelXYs = []
if cbook.iterable(self.labelManual):
- for x,y in self.labelManual:
+ for x, y in self.labelManual:
self.add_label_near(x, y, inline,
inline_spacing)
@@ -215,34 +215,33 @@ def clabel(self, *args, **kwargs):
print('Remove last label by clicking third mouse button.')
blocking_contour_labeler = BlockingContourLabeler(self)
- blocking_contour_labeler(inline,inline_spacing)
+ blocking_contour_labeler(inline, inline_spacing)
else:
- self.labels(inline,inline_spacing)
+ self.labels(inline, inline_spacing)
# Hold on to some old attribute names. These are deprecated and will
- # be removed in the near future (sometime after 2008-08-01), but keeping
- # for now for backwards compatibility
+ # be removed in the near future (sometime after 2008-08-01), but
+ # keeping for now for backwards compatibility
self.cl = self.labelTexts
self.cl_xy = self.labelXYs
self.cl_cvalues = self.labelCValues
- self.labelTextsList = cbook.silent_list('text.Text', self.labelTexts)
+ self.labelTextsList = cbook.silent_list('text.Text', self.labelTexts)
return self.labelTextsList
-
def print_label(self, linecontour, labelwidth):
"Return *False* if contours are too short for a label."
lcsize = len(linecontour)
if lcsize > 10 * labelwidth:
return True
- xmax = np.amax(linecontour[:,0])
- xmin = np.amin(linecontour[:,0])
- ymax = np.amax(linecontour[:,1])
- ymin = np.amin(linecontour[:,1])
+ xmax = np.amax(linecontour[:, 0])
+ xmin = np.amin(linecontour[:, 0])
+ ymax = np.amax(linecontour[:, 1])
+ ymin = np.amin(linecontour[:, 1])
lw = labelwidth
- if (xmax - xmin) > 1.2* lw or (ymax - ymin) > 1.2 * lw:
+ if (xmax - xmin) > 1.2 * lw or (ymax - ymin) > 1.2 * lw:
return True
else:
return False
@@ -250,8 +249,8 @@ def print_label(self, linecontour, labelwidth):
def too_close(self, x, y, lw):
"Return *True* if a label is already near this location."
for loc in self.labelXYs:
- d = np.sqrt((x-loc[0]) ** 2 + (y-loc[1]) ** 2)
- if d < 1.2*lw:
+ d = np.sqrt((x - loc[0]) ** 2 + (y - loc[1]) ** 2)
+ if d < 1.2 * lw:
return True
return False
@@ -266,18 +265,18 @@ def get_label_coords(self, distances, XX, YY, ysize, lw):
If all such candidates are rejected, the beginning
of the contour is chosen.
"""
- hysize = int(ysize/2)
+ hysize = int(ysize / 2)
adist = np.argsort(distances)
for ind in adist:
x, y = XX[ind][hysize], YY[ind][hysize]
- if self.too_close(x,y, lw):
+ if self.too_close(x, y, lw):
continue
- return x,y, ind
+ return x, y, ind
ind = adist[0]
x, y = XX[ind][hysize], YY[ind][hysize]
- return x,y, ind
+ return x, y, ind
def get_label_width(self, lev, fmt, fsize):
"""
@@ -290,18 +289,21 @@ def get_label_width(self, lev, fmt, fsize):
if ismath == 'TeX':
if not hasattr(self, '_TeX_manager'):
self._TeX_manager = texmanager.TexManager()
- lw, _, _ = self._TeX_manager.get_text_width_height_descent(lev, fsize)
+ lw, _, _ = self._TeX_manager.get_text_width_height_descent(lev,
+ fsize)
elif ismath:
if not hasattr(self, '_mathtext_parser'):
self._mathtext_parser = mathtext.MathTextParser('bitmap')
- img, _ = self._mathtext_parser.parse(lev, dpi=72, prop=self.labelFontProps)
- lw = img.get_width() # at dpi=72, the units are PostScript points
+ img, _ = self._mathtext_parser.parse(lev, dpi=72,
+ prop=self.labelFontProps)
+ lw = img.get_width() # at dpi=72, the units are PostScript points
else:
- lw = (len(lev)) * fsize * 0.6 # width is much less than "font size"
+ # width is much less than "font size"
+ lw = (len(lev)) * fsize * 0.6
return lw
- def get_real_label_width( self, lev, fmt, fsize ):
+ def get_real_label_width(self, lev, fmt, fsize):
"""
This computes actual onscreen label width.
This uses some black magic to determine onscreen extent of non-drawn
@@ -310,11 +312,11 @@ def get_real_label_width( self, lev, fmt, fsize ):
This method is not being used, and may be modified or removed.
"""
# Find middle of axes
- xx = np.mean( np.asarray(self.ax.axis()).reshape(2,2), axis=1 )
+ xx = np.mean(np.asarray(self.ax.axis()).reshape(2, 2), axis=1)
# Temporarily create text object
- t = text.Text( xx[0], xx[1] )
- self.set_label_props( t, self.get_text(lev,fmt), 'k' )
+ t = text.Text(xx[0], xx[1])
+ self.set_label_props(t, self.get_text(lev, fmt), 'k')
# Some black magic to get onscreen extent
# NOTE: This will only work for already drawn figures, as the canvas
@@ -323,7 +325,7 @@ def get_real_label_width( self, lev, fmt, fsize ):
bbox = t.get_window_extent(renderer=self.ax.figure.canvas.renderer)
# difference in pixel extent of image
- lw = np.diff(bbox.corners()[0::2,0])[0]
+ lw = np.diff(bbox.corners()[0::2, 0])[0]
return lw
@@ -339,12 +341,12 @@ def get_text(self, lev, fmt):
if cbook.is_string_like(lev):
return lev
else:
- if isinstance(fmt,dict):
+ if isinstance(fmt, dict):
return fmt[lev]
elif callable(fmt):
return fmt(lev)
else:
- return fmt%lev
+ return fmt % lev
def locate_label(self, linecontour, labelwidth):
"""
@@ -352,9 +354,9 @@ def locate_label(self, linecontour, labelwidth):
part of the contour).
"""
- nsize= len(linecontour)
+ nsize = len(linecontour)
if labelwidth > 1:
- xsize = int(np.ceil(nsize/labelwidth))
+ xsize = int(np.ceil(nsize / labelwidth))
else:
xsize = 1
if xsize == 1:
@@ -362,28 +364,28 @@ def locate_label(self, linecontour, labelwidth):
else:
ysize = int(labelwidth)
- XX = np.resize(linecontour[:,0],(xsize, ysize))
- YY = np.resize(linecontour[:,1],(xsize, ysize))
+ XX = np.resize(linecontour[:, 0], (xsize, ysize))
+ YY = np.resize(linecontour[:, 1], (xsize, ysize))
#I might have fouled up the following:
- yfirst = YY[:,0].reshape(xsize, 1)
- ylast = YY[:,-1].reshape(xsize, 1)
- xfirst = XX[:,0].reshape(xsize, 1)
- xlast = XX[:,-1].reshape(xsize, 1)
- s = (yfirst-YY) * (xlast-xfirst) - (xfirst-XX) * (ylast-yfirst)
- L = np.sqrt((xlast-xfirst)**2+(ylast-yfirst)**2).ravel()
- dist = np.add.reduce(([(abs(s)[i]/L[i]) for i in range(xsize)]),-1)
- x,y,ind = self.get_label_coords(dist, XX, YY, ysize, labelwidth)
+ yfirst = YY[:, 0].reshape(xsize, 1)
+ ylast = YY[:, -1].reshape(xsize, 1)
+ xfirst = XX[:, 0].reshape(xsize, 1)
+ xlast = XX[:, -1].reshape(xsize, 1)
+ s = (yfirst - YY) * (xlast - xfirst) - (xfirst - XX) * (ylast - yfirst)
+ L = np.sqrt((xlast - xfirst) ** 2 + (ylast - yfirst) ** 2).ravel()
+ dist = np.add.reduce(([(abs(s)[i] / L[i]) for i in range(xsize)]), -1)
+ x, y, ind = self.get_label_coords(dist, XX, YY, ysize, labelwidth)
#print 'ind, x, y', ind, x, y
# There must be a more efficient way...
lc = [tuple(l) for l in linecontour]
- dind = lc.index((x,y))
+ dind = lc.index((x, y))
#print 'dind', dind
#dind = list(linecontour).index((x,y))
return x, y, dind
- def calc_label_rot_and_inline( self, slc, ind, lw, lc=None, spacing=5 ):
+ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
"""
This function calculates the appropriate label rotation given
the linecontour coordinates in screen units, the index of the
@@ -402,40 +404,41 @@ def calc_label_rot_and_inline( self, slc, ind, lw, lc=None, spacing=5 ):
determine rotation and then to break contour if desired.
"""
- if lc is None: lc = []
+ if lc is None:
+ lc = []
# Half the label width
- hlw = lw/2.0
+ hlw = lw / 2.0
# Check if closed and, if so, rotate contour so label is at edge
closed = mlab.is_closed_polygon(slc)
if closed:
- slc = np.r_[ slc[ind:-1], slc[:ind+1] ]
+ slc = np.r_[slc[ind:-1], slc[:ind + 1]]
- if len(lc): # Rotate lc also if not empty
- lc = np.r_[ lc[ind:-1], lc[:ind+1] ]
+ if len(lc): # Rotate lc also if not empty
+ lc = np.r_[lc[ind:-1], lc[:ind + 1]]
ind = 0
# Path length in pixel space
pl = mlab.path_length(slc)
- pl = pl-pl[ind]
+ pl = pl - pl[ind]
# Use linear interpolation to get points around label
- xi = np.array( [ -hlw, hlw ] )
- if closed: # Look at end also for closed contours
- dp = np.array([pl[-1],0])
+ xi = np.array([-hlw, hlw])
+ if closed: # Look at end also for closed contours
+ dp = np.array([pl[-1], 0])
else:
dp = np.zeros_like(xi)
- ll = mlab.less_simple_linear_interpolation( pl, slc, dp+xi,
- extrap=True )
+ ll = mlab.less_simple_linear_interpolation(pl, slc, dp + xi,
+ extrap=True)
# get vector in pixel space coordinates from one point to other
- dd = np.diff( ll, axis=0 ).ravel()
+ dd = np.diff(ll, axis=0).ravel()
# Get angle of vector - must be calculated in pixel space for
# text rotation to work correctly
- if np.all(dd==0): # Must deal with case of zero length label
+ if np.all(dd == 0): # Must deal with case of zero length label
rotation = 0.0
else:
rotation = np.arctan2(dd[1], dd[0]) * 180.0 / np.pi
@@ -451,35 +454,35 @@ def calc_label_rot_and_inline( self, slc, ind, lw, lc=None, spacing=5 ):
nlc = []
if len(lc):
# Expand range by spacing
- xi = dp + xi + np.array([-spacing,spacing])
+ xi = dp + xi + np.array([-spacing, spacing])
# Get indices near points of interest
I = mlab.less_simple_linear_interpolation(
- pl, np.arange(len(pl)), xi, extrap=False )
+ pl, np.arange(len(pl)), xi, extrap=False)
# If those indices aren't beyond contour edge, find x,y
- if (not np.isnan(I[0])) and int(I[0])!=I[0]:
+ if (not np.isnan(I[0])) and int(I[0]) != I[0]:
xy1 = mlab.less_simple_linear_interpolation(
- pl, lc, [ xi[0] ] )
+ pl, lc, [xi[0]])
- if (not np.isnan(I[1])) and int(I[1])!=I[1]:
+ if (not np.isnan(I[1])) and int(I[1]) != I[1]:
xy2 = mlab.less_simple_linear_interpolation(
- pl, lc, [ xi[1] ] )
+ pl, lc, [xi[1]])
# Make integer
- I = [ np.floor(I[0]), np.ceil(I[1]) ]
+ I = [np.floor(I[0]), np.ceil(I[1])]
# Actually break contours
if closed:
# This will remove contour if shorter than label
if np.all(~np.isnan(I)):
- nlc.append( np.r_[ xy2, lc[I[1]:I[0]+1], xy1 ] )
+ nlc.append(np.r_[xy2, lc[I[1]:I[0] + 1], xy1])
else:
# These will remove pieces of contour if they have length zero
if not np.isnan(I[0]):
- nlc.append( np.r_[ lc[:I[0]+1], xy1 ] )
+ nlc.append(np.r_[lc[:I[0] + 1], xy1])
if not np.isnan(I[1]):
- nlc.append( np.r_[ xy2, lc[I[1]:] ] )
+ nlc.append(np.r_[xy2, lc[I[1]:]])
# The current implementation removes contours completely
# covered by labels. Uncomment line below to keep
@@ -488,49 +491,49 @@ def calc_label_rot_and_inline( self, slc, ind, lw, lc=None, spacing=5 ):
return rotation, nlc
- def _get_label_text(self,x,y,rotation):
- dx,dy = self.ax.transData.inverted().transform_point((x,y))
- t = text.Text(dx, dy, rotation = rotation,
+ def _get_label_text(self, x, y, rotation):
+ dx, dy = self.ax.transData.inverted().transform_point((x, y))
+ t = text.Text(dx, dy, rotation=rotation,
horizontalalignment='center',
verticalalignment='center')
return t
- def _get_label_clabeltext(self,x,y,rotation):
+ def _get_label_clabeltext(self, x, y, rotation):
# x, y, rotation is given in pixel coordinate. Convert them to
# the data coordinate and create a label using ClabelText
# class. This way, the roation of the clabel is along the
# contour line always.
transDataInv = self.ax.transData.inverted()
- dx,dy = transDataInv.transform_point((x,y))
+ dx, dy = transDataInv.transform_point((x, y))
drotation = transDataInv.transform_angles(np.array([rotation]),
- np.array([[x,y]]))
- t = ClabelText(dx, dy, rotation = drotation[0],
+ np.array([[x, y]]))
+ t = ClabelText(dx, dy, rotation=drotation[0],
horizontalalignment='center',
verticalalignment='center')
return t
def _add_label(self, t, x, y, lev, cvalue):
- color = self.labelMappable.to_rgba(cvalue,alpha=self.alpha)
+ color = self.labelMappable.to_rgba(cvalue, alpha=self.alpha)
- _text = self.get_text(lev,self.labelFmt)
+ _text = self.get_text(lev, self.labelFmt)
self.set_label_props(t, _text, color)
self.labelTexts.append(t)
self.labelCValues.append(cvalue)
- self.labelXYs.append((x,y))
+ self.labelXYs.append((x, y))
# Add label to plot here - useful for manual mode label selection
self.ax.add_artist(t)
- def add_label(self,x,y,rotation,lev,cvalue):
+ def add_label(self, x, y, rotation, lev, cvalue):
"""
Add contour label using :class:`~matplotlib.text.Text` class.
"""
- t = self._get_label_text(x,y,rotation)
+ t = self._get_label_text(x, y, rotation)
self._add_label(t, x, y, lev, cvalue)
- def add_label_clabeltext(self,x,y,rotation,lev,cvalue):
+ def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
"""
Add contour label using :class:`ClabelText` class.
"""
@@ -539,10 +542,9 @@ def add_label_clabeltext(self,x,y,rotation,lev,cvalue):
# class. This way, the roation of the clabel is along the
# contour line always.
- t = self._get_label_clabeltext(x,y,rotation)
+ t = self._get_label_clabeltext(x, y, rotation)
self._add_label(t, x, y, lev, cvalue)
-
def add_label_near(self, x, y, inline=True, inline_spacing=5,
transform=None):
"""
@@ -565,9 +567,9 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
transform = self.ax.transData
if transform:
- x,y = transform.transform_point((x, y))
+ x, y = transform.transform_point((x, y))
- conmin,segmin,imin,xmin,ymin = self.find_nearest_contour(
+ conmin, segmin, imin, xmin, ymin = self.find_nearest_contour(
x, y, self.labelIndiceList)[:5]
# Get index of nearest level in subset of levels used for labeling
@@ -589,11 +591,11 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
lcarg = lc
else:
lcarg = None
- rotation,nlc = self.calc_label_rot_and_inline(
+ rotation, nlc = self.calc_label_rot_and_inline(
slc, imin, lw, lcarg,
- inline_spacing )
+ inline_spacing)
- self.add_label(xmin,ymin,rotation,self.labelLevelList[lmin],
+ self.add_label(xmin, ymin, rotation, self.labelLevelList[lmin],
self.labelCValueList[lmin])
if inline:
@@ -602,10 +604,10 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
# Add paths if not empty or single point
for n in nlc:
- if len(n)>1:
- paths.append( mpath.Path(n) )
+ if len(n) > 1:
+ paths.append(mpath.Path(n))
- def pop_label(self,index=-1):
+ def pop_label(self, index=-1):
'''Defaults to removing last label, but any index can be supplied'''
self.labelCValues.pop(index)
t = self.labelTexts.pop(index)
@@ -620,45 +622,49 @@ def labels(self, inline, inline_spacing):
for icon, lev, fsize, cvalue in zip(
self.labelIndiceList, self.labelLevelList, self.labelFontSizeList,
- self.labelCValueList ):
+ self.labelCValueList):
con = self.collections[icon]
trans = con.get_transform()
lw = self.get_label_width(lev, self.labelFmt, fsize)
- lw *= self.ax.figure.dpi/72.0 # scale to screen coordinates
+ lw *= self.ax.figure.dpi / 72.0 # scale to screen coordinates
additions = []
paths = con.get_paths()
for segNum, linepath in enumerate(paths):
- lc = linepath.vertices # Line contour
- slc0 = trans.transform(lc) # Line contour in screen coords
+ lc = linepath.vertices # Line contour
+ slc0 = trans.transform(lc) # Line contour in screen coords
# For closed polygons, add extra point to avoid division by
# zero in print_label and locate_label. Other than these
# functions, this is not necessary and should probably be
# eventually removed.
- if mlab.is_closed_polygon( lc ):
- slc = np.r_[ slc0, slc0[1:2,:] ]
+ if mlab.is_closed_polygon(lc):
+ slc = np.r_[slc0, slc0[1:2, :]]
else:
slc = slc0
- if self.print_label(slc,lw): # Check if long enough for a label
- x,y,ind = self.locate_label(slc, lw)
+ # Check if long enough for a label
+ if self.print_label(slc, lw):
+ x, y, ind = self.locate_label(slc, lw)
- if inline: lcarg = lc
- else: lcarg = None
- rotation,new=self.calc_label_rot_and_inline(
+ if inline:
+ lcarg = lc
+ else:
+ lcarg = None
+ rotation, new = self.calc_label_rot_and_inline(
slc0, ind, lw, lcarg,
- inline_spacing )
+ inline_spacing)
# Actually add the label
- add_label(x,y,rotation,lev,cvalue)
+ add_label(x, y, rotation, lev, cvalue)
# If inline, add new contours
if inline:
for n in new:
# Add path if not empty or single point
- if len(n)>1: additions.append( mpath.Path(n) )
- else: # If not adding label, keep old path
+ if len(n) > 1:
+ additions.append(mpath.Path(n))
+ else: # If not adding label, keep old path
additions.append(linepath)
# After looping over all segments on a contour, remove old
@@ -667,6 +673,7 @@ def labels(self, inline, inline_spacing):
del paths[:]
paths.extend(additions)
+
class ContourSet(cm.ScalarMappable, ContourLabeler):
"""
Store a set of contour lines or filled regions.
@@ -750,8 +757,8 @@ def __init__(self, ax, *args, **kwargs):
self.extend = kwargs.get('extend', 'neither')
self.antialiased = kwargs.get('antialiased', None)
if self.antialiased is None and self.filled:
- self.antialiased = False # eliminate artifacts; we are not
- # stroking the boundaries.
+ self.antialiased = False # eliminate artifacts; we are not
+ # stroking the boundaries.
# The default for line contours will be taken from
# the LineCollection default, which uses the
# rcParams['lines.antialiased']
@@ -764,16 +771,19 @@ def __init__(self, ax, *args, **kwargs):
if norm is None:
norm = colors.LogNorm()
if self.extend is not 'neither':
- raise ValueError('extend kwarg does not work yet with log scale')
+ raise ValueError('extend kwarg does not work yet with log '
+ ' scale')
else:
self.logscale = False
- if self.origin is not None: assert(self.origin in
- ['lower', 'upper', 'image'])
- if self.extent is not None: assert(len(self.extent) == 4)
+ if self.origin is not None:
+ assert(self.origin in ['lower', 'upper', 'image'])
+ if self.extent is not None:
+ assert(len(self.extent) == 4)
if self.colors is not None and cmap is not None:
raise ValueError('Either colors or cmap must be None')
- if self.origin == 'image': self.origin = mpl.rcParams['image.origin']
+ if self.origin == 'image':
+ self.origin = mpl.rcParams['image.origin']
self._transform = kwargs.get('transform', None)
@@ -796,7 +806,8 @@ def __init__(self, ax, *args, **kwargs):
kw = {'cmap': cmap}
if norm is not None:
kw['norm'] = norm
- cm.ScalarMappable.__init__(self, **kw) # sets self.cmap, norm if needed;
+ # sets self.cmap, norm if needed;
+ cm.ScalarMappable.__init__(self, **kw)
if vmin is not None:
self.norm.vmin = vmin
if vmax is not None:
@@ -814,7 +825,7 @@ def __init__(self, ax, *args, **kwargs):
# Ensure allkinds can be zipped below.
if self.allkinds is None:
- self.allkinds = [None]*len(self.allsegs)
+ self.allkinds = [None] * len(self.allsegs)
for level, level_upper, segs, kinds in \
zip(lowers, uppers, self.allsegs, self.allkinds):
@@ -822,8 +833,8 @@ def __init__(self, ax, *args, **kwargs):
# Default zorder taken from Collection
zorder = kwargs.get('zorder', 1)
col = mcoll.PathCollection(paths,
- antialiaseds = (self.antialiased,),
- edgecolors= 'none',
+ antialiaseds=(self.antialiased,),
+ edgecolors='none',
alpha=self.alpha,
transform=self.get_transform(),
zorder=zorder)
@@ -841,17 +852,17 @@ def __init__(self, ax, *args, **kwargs):
# Default zorder taken from LineCollection
zorder = kwargs.get('zorder', 2)
col = mcoll.LineCollection(segs,
- antialiaseds = aa,
- linewidths = width,
- linestyle = lstyle,
+ antialiaseds=aa,
+ linewidths=width,
+ linestyle=lstyle,
alpha=self.alpha,
transform=self.get_transform(),
zorder=zorder)
col.set_label('_nolegend_')
self.ax.add_collection(col, False)
self.collections.append(col)
- self.changed() # set the colors
-
+ self.changed() # set the colors
+
def get_transform(self):
"""
Return the :class:`~matplotlib.transforms.Transform`
@@ -890,24 +901,30 @@ def legend_elements(self, variable_name='x', str_format=str):
lowers, uppers = self._get_lowers_and_uppers()
n_levels = len(self.collections)
- for i, (collection, lower, upper) in enumerate(zip(self.collections,
- lowers, uppers)):
- patch = mpatches.Rectangle((0, 0), 1, 1,
- facecolor=collection.get_facecolor()[0],
- hatch=collection.get_hatch(),
- alpha=collection.get_alpha(),
- )
+ for i, (collection, lower, upper) in enumerate(
+ zip(self.collections,
+ lowers, uppers)):
+ patch = mpatches.Rectangle(
+ (0, 0), 1, 1,
+ facecolor=collection.get_facecolor()[0],
+ hatch=collection.get_hatch(),
+ alpha=collection.get_alpha(),
+ )
artists.append(patch)
lower = str_format(lower)
upper = str_format(upper)
if i == 0 and self.extend in ('min', 'both'):
- labels.append(r'$%s \leq %s$' % (variable_name, lower, ))
- elif i == n_levels-1 and self.extend in ('max', 'both'):
- labels.append(r'$%s > %s$' % (variable_name, upper, ))
+ labels.append(r'$%s \leq %s$' % (variable_name,
+ lower))
+ elif i == n_levels - 1 and self.extend in ('max', 'both'):
+ labels.append(r'$%s > %s$' % (variable_name,
+ upper))
else:
- labels.append(r'$%s < %s \leq %s$' % (lower, variable_name, upper))
+ labels.append(r'$%s < %s \leq %s$' % (lower,
+ variable_name,
+ upper))
else:
for collection, level in zip(self.collections, self.levels):
@@ -937,14 +954,16 @@ def _process_args(self, *args, **kwargs):
# Check lengths of levels and allsegs.
if self.filled:
- if len(self.allsegs) != len(self.levels)-1:
- raise ValueError('must be one less number of segments as levels')
+ if len(self.allsegs) != len(self.levels) - 1:
+ raise ValueError('must be one less number of segments as '
+ 'levels')
else:
if len(self.allsegs) != len(self.levels):
raise ValueError('must be same number of segments as levels')
# Check length of allkinds.
- if self.allkinds is not None and len(self.allkinds) != len(self.allsegs):
+ if (self.allkinds is not None and
+ len(self.allkinds) != len(self.allsegs)):
raise ValueError('allkinds has different length to allsegs')
# Determine x,y bounds and update axes data limits.
@@ -959,7 +978,7 @@ def _process_args(self, *args, **kwargs):
min = seg.min(axis=0)
max = seg.max(axis=0)
havelimits = True
-
+
if havelimits:
self.ax.update_datalim([min, max])
self.ax.autoscale_view(tight=True)
@@ -978,7 +997,7 @@ def _get_lowers_and_uppers(self):
lowers = self._levels[:-1]
if self.zmin == lowers[0]:
# Include minimum values in lowest interval
- lowers = lowers.copy() # so we don't change self._levels
+ lowers = lowers.copy() # so we don't change self._levels
if self.logscale:
lowers[0] = 0.99 * self.zmin
else:
@@ -988,16 +1007,18 @@ def _get_lowers_and_uppers(self):
def _make_paths(self, segs, kinds):
if kinds is not None:
- return [mpath.Path(seg,codes=kind) for seg,kind in zip(segs,kinds)]
+ return [mpath.Path(seg, codes=kind)
+ for seg, kind in zip(segs, kinds)]
else:
return [mpath.Path(seg) for seg in segs]
def changed(self):
- tcolors = [ (tuple(rgba),) for rgba in
- self.to_rgba(self.cvalues, alpha=self.alpha)]
+ tcolors = [(tuple(rgba),)
+ for rgba in self.to_rgba(self.cvalues, alpha=self.alpha)]
self.tcolors = tcolors
hatches = self.hatches * len(tcolors)
- for color, hatch, collection in zip(tcolors, hatches, self.collections):
+ for color, hatch, collection in zip(tcolors, hatches,
+ self.collections):
if self.filled:
collection.set_facecolor(color)
# update the collection's hatch (may be None)
@@ -1025,7 +1046,7 @@ def _autolev(self, z, N):
if self.logscale:
self.locator = ticker.LogLocator()
else:
- self.locator = ticker.MaxNLocator(N+1)
+ self.locator = ticker.MaxNLocator(N + 1)
zmax = self.zmax
zmin = self.zmin
lev = self.locator.tick_values(zmin, zmax)
@@ -1039,8 +1060,10 @@ def _contour_level_args(self, z, args):
"""
Determine the contour levels and store in self.levels.
"""
- if self.filled: fn = 'contourf'
- else: fn = 'contour'
+ if self.filled:
+ fn = 'contourf'
+ else:
+ fn = 'contour'
self._auto = False
if self.levels is None:
if len(args) == 0:
@@ -1054,7 +1077,8 @@ def _contour_level_args(self, z, args):
lev = np.asarray(level_arg).astype(np.float64)
except:
raise TypeError(
- "Last %s arg must give levels; see help(%s)" % (fn,fn))
+ "Last %s arg must give levels; see help(%s)" %
+ (fn, fn))
self.levels = lev
if self.filled and len(self.levels) < 2:
raise ValueError("Filled contours require at least 2 levels.")
@@ -1083,9 +1107,9 @@ def _process_levels(self):
return
if self.extend in ('both', 'min'):
- self._levels.insert(0, min(self.levels[0],self.zmin) - 1)
+ self._levels.insert(0, min(self.levels[0], self.zmin) - 1)
if self.extend in ('both', 'max'):
- self._levels.append(max(self.levels[-1],self.zmax) + 1)
+ self._levels.append(max(self.levels[-1], self.zmax) + 1)
self._levels = np.asarray(self._levels)
# layer values are mid-way between levels
@@ -1153,7 +1177,7 @@ def _process_linewidths(self):
else:
linewidths = list(linewidths)
if len(linewidths) < Nlev:
- nreps = int(np.ceil(Nlev/len(linewidths)))
+ nreps = int(np.ceil(Nlev / len(linewidths)))
linewidths = linewidths * nreps
if len(linewidths) > Nlev:
linewidths = linewidths[:Nlev]
@@ -1177,7 +1201,7 @@ def _process_linestyles(self):
elif cbook.iterable(linestyles):
tlinestyles = list(linestyles)
if len(tlinestyles) < Nlev:
- nreps = int(np.ceil(Nlev/len(linestyles)))
+ nreps = int(np.ceil(Nlev / len(linestyles)))
tlinestyles = tlinestyles * nreps
if len(tlinestyles) > Nlev:
tlinestyles = tlinestyles[:Nlev]
@@ -1194,7 +1218,7 @@ def set_alpha(self, alpha):
self.alpha = alpha
self.changed()
- def find_nearest_contour( self, x, y, indices=None, pixel=True ):
+ def find_nearest_contour(self, x, y, indices=None, pixel=True):
"""
Finds contour that is closest to a point. Defaults to
measuring distance in pixels (screen space - useful for manual
@@ -1229,7 +1253,7 @@ def find_nearest_contour( self, x, y, indices=None, pixel=True ):
# sufficiently well that the time is not noticeable.
# Nonetheless, improvements could probably be made.
- if indices==None:
+ if indices is None:
indices = range(len(self.levels))
dmin = 1e10
@@ -1249,17 +1273,17 @@ def find_nearest_contour( self, x, y, indices=None, pixel=True ):
if pixel:
lc = trans.transform(lc)
- ds = (lc[:,0]-x)**2 + (lc[:,1]-y)**2
- d = min( ds )
+ ds = (lc[:, 0] - x) ** 2 + (lc[:, 1] - y) ** 2
+ d = min(ds)
if d < dmin:
dmin = d
conmin = icon
segmin = segNum
- imin = mpl.mlab.find( ds == d )[0]
- xmin = lc[imin,0]
- ymin = lc[imin,1]
+ imin = mpl.mlab.find(ds == d)[0]
+ xmin = lc[imin, 0]
+ ymin = lc[imin, 1]
- return (conmin,segmin,imin,xmin,ymin,dmin)
+ return (conmin, segmin, imin, xmin, ymin, dmin)
class QuadContourSet(ContourSet):
@@ -1305,14 +1329,14 @@ def _process_args(self, *args, **kwargs):
self.zmax = args[0].zmax
else:
x, y, z = self._contour_args(args, kwargs)
-
+
_mask = ma.getmask(z)
if _mask is ma.nomask:
_mask = None
C = _cntr.Cntr(x, y, z.filled(), _mask)
-
+
t = self.get_transform()
-
+
# if the transform is not trans data, and some part of it
# contains transData, transform the xs and ys to data coordinates
if (t != self.ax.transData and
@@ -1322,12 +1346,12 @@ def _process_args(self, *args, **kwargs):
transformed_pts = trans_to_data.transform(pts)
x = transformed_pts[..., 0]
y = transformed_pts[..., 1]
-
+
x0 = ma.minimum(x)
x1 = ma.maximum(x)
y0 = ma.minimum(y)
y1 = ma.maximum(y)
- self.ax.update_datalim([(x0,y0), (x1,y1)])
+ self.ax.update_datalim([(x0, y0), (x1, y1)])
self.ax.autoscale_view(tight=True)
self.Cntr = C
@@ -1341,8 +1365,9 @@ def _get_allsegs_and_allkinds(self):
lowers, uppers = self._get_lowers_and_uppers()
allkinds = []
for level, level_upper in zip(lowers, uppers):
- nlist = self.Cntr.trace(level, level_upper, nchunk = self.nchunk)
- nseg = len(nlist)//2
+ nlist = self.Cntr.trace(level, level_upper,
+ nchunk=self.nchunk)
+ nseg = len(nlist) // 2
@dmcdougall Collaborator

What does // do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
segs = nlist[:nseg]
kinds = nlist[nseg:]
allsegs.append(segs)
@@ -1351,24 +1376,27 @@ def _get_allsegs_and_allkinds(self):
allkinds = None
for level in self.levels:
nlist = self.Cntr.trace(level)
- nseg = len(nlist)//2
+ nseg = len(nlist) // 2
segs = nlist[:nseg]
allsegs.append(segs)
return allsegs, allkinds
def _contour_args(self, args, kwargs):
- if self.filled: fn = 'contourf'
- else: fn = 'contour'
+ if self.filled:
+ fn = 'contourf'
+ else:
+ fn = 'contour'
Nargs = len(args)
if Nargs <= 2:
z = ma.asarray(args[0], dtype=np.float64)
x, y = self._initialize_x_y(z)
args = args[1:]
- elif Nargs <=4:
- x,y,z = self._check_xyz(args[:3], kwargs)
+ elif Nargs <= 4:
+ x, y, z = self._check_xyz(args[:3], kwargs)
args = args[3:]
else:
- raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn))
+ raise TypeError("Too many arguments to %s; see help(%s)" %
+ (fn, fn))
z = ma.masked_invalid(z, copy=False)
self.zmax = ma.maximum(z)
self.zmin = ma.minimum(z)
@@ -1398,9 +1426,10 @@ def _check_xyz(self, args, kwargs):
z = ma.asarray(args[2], dtype=np.float64)
if z.ndim != 2:
raise TypeError("Input z must be a 2D array.")
- else: Ny, Nx = z.shape
+ else:
+ Ny, Nx = z.shape
if x.shape == z.shape and y.shape == z.shape:
- return x,y,z
+ return x, y, z
if x.ndim != 1 or y.ndim != 1:
raise TypeError("Inputs x and y must be 1D or 2D.")
nx, = x.shape
@@ -1408,8 +1437,8 @@ def _check_xyz(self, args, kwargs):
if nx != Nx or ny != Ny:
raise TypeError("Length of x must be number of columns in z,\n" +
"and length of y must be number of rows.")
- x,y = np.meshgrid(x,y)
- return x,y,z
+ x, y = np.meshgrid(x, y)
+ return x, y, z
def _initialize_x_y(self, z):
'''
@@ -1432,22 +1461,22 @@ def _initialize_x_y(self, z):
if self.extent is None:
return np.meshgrid(np.arange(Nx), np.arange(Ny))
else:
- x0,x1,y0,y1 = self.extent
+ x0, x1, y0, y1 = self.extent
x = np.linspace(x0, x1, Nx)
y = np.linspace(y0, y1, Ny)
return np.meshgrid(x, y)
# Match image behavior:
if self.extent is None:
- x0,x1,y0,y1 = (0, Nx, 0, Ny)
+ x0, x1, y0, y1 = (0, Nx, 0, Ny)
else:
- x0,x1,y0,y1 = self.extent
- dx = float(x1 - x0)/Nx
- dy = float(y1 - y0)/Ny
+ x0, x1, y0, y1 = self.extent
+ dx = float(x1 - x0) / Nx
+ dy = float(y1 - y0) / Ny
x = x0 + (np.arange(Nx) + 0.5) * dx
y = y0 + (np.arange(Ny) + 0.5) * dy
if self.origin == 'upper':
y = y[::-1]
- return np.meshgrid(x,y)
+ return np.meshgrid(x, y)
contour_doc = """
Plot contours.
Something went wrong with that request. Please try again.