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

Skip to content

Commit a095b24

Browse files
committed
Committing rewrite of clabel inlining and label rotation code. Also, renaming
ContourLabeler attributes to something like .labelAttribute. A few of the old attribute names have been maintained (in addition to new versions) for backward compatibility, but these should be remove in +1 releases. Added appropriate comments to CHANGELOG and API_CHANGES. svn path=/trunk/matplotlib/; revision=5830
1 parent 59e5087 commit a095b24

File tree

4 files changed

+253
-183
lines changed

4 files changed

+253
-183
lines changed

API_CHANGES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Changes for 0.98.x
88
Axes.patch, Axes.axesFrame renamed Axes.frame, Axes.get_frame, which
99
returns Axes.patch, is deprecated. Examples and users guide updated
1010

11+
* Changes in the ContourLabeler attributes (clabel function) so that they
12+
all have a form like .labelAttribute. The three attributes that are most
13+
likely to be used by end users, .cl, .cl_xy and .cl_cvalues have been
14+
maintained for the moment (in addition to their renamed versions), but they
15+
are depricated and will eventually be removed.
16+
1117
Changes for 0.98.1
1218
==================
1319

CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
2008-07-24 Rewrite of a significant portion of the clabel code (class
2+
ContourLabeler) to improve inlining. - DMK
3+
14
2008-07-22 Added Barbs polygon collection (similar to Quiver) for plotting
25
wind barbs. Added corresponding helpers to Axes and pyplot as
36
well. (examples/pylab_examples/barb_demo.py shows it off.) - RMM
47

58
2008-07-21 Added scikits.delaunay as matplotlib.delaunay. Added griddata
69
function in matplotlib.mlab, with example (griddata_demo.py) in
7-
pylab_examples. griddata function will use mpl_toolkits._natgrid
10+
pylab_examples. griddata function will use mpl_toolkits._natgrid
811
if installed (haven't yet created the toolkit). - JSW
912

1013
2008-07-21 Re-introduced offset_copy that works in the context of the

lib/matplotlib/blocking_input.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
import time
2121
import numpy as np
22+
2223
from matplotlib import path, verbose
23-
from cbook import is_sequence_of_strings
24+
from matplotlib.cbook import is_sequence_of_strings
2425

2526
class BlockingInput(object):
2627
"""
@@ -267,46 +268,38 @@ def button1(self,event):
267268

268269
if event.inaxes == cs.ax:
269270
conmin,segmin,imin,xmin,ymin = cs.find_nearest_contour(
270-
event.x, event.y, cs.label_indices)[:5]
271+
event.x, event.y, cs.labelIndiceList)[:5]
271272

272273
# Get index of nearest level in subset of levels used for labeling
273-
lmin = cs.label_indices.index(conmin)
274+
lmin = cs.labelIndiceList.index(conmin)
274275

276+
# Coordinates of contour
275277
paths = cs.collections[conmin].get_paths()
276278
lc = paths[segmin].vertices
277279

278-
# Figure out label rotation. This is very cludgy.
279-
# Ideally, there would be one method in ContourLabeler
280-
# that would figure out the best rotation for a label at a
281-
# point, but the way automatic label rotation is done is
282-
# quite mysterious to me and doesn't seem easy to
283-
# generalize to non-automatic label placement. The method
284-
# used below is not very robust! It basically looks one
285-
# point before and one point after label location on
286-
# contour and takes mean of angles of two vectors formed.
287-
# This produces "acceptable" results, but not nearly as
288-
# nice as automatic method.
289-
ll = lc[max(0,imin-1):imin+2] # Get points around point
290-
dd = np.diff(ll,axis=0)
291-
rotation = np.mean( np.arctan2(dd[:,1], dd[:,0]) ) * 180 / np.pi
292-
if rotation > 90:
293-
rotation = rotation -180
294-
if rotation < -90:
295-
rotation = 180 + rotation
296-
297-
cs.add_label(xmin,ymin,rotation,cs.label_levels[lmin],
298-
cs.label_cvalues[lmin])
280+
# In pixel/screen space
281+
slc = cs.ax.transData.transform(lc)
282+
283+
# Get label width for rotating labels and breaking contours
284+
lw = cs.get_label_width(cs.labelLevelList[lmin],
285+
cs.labelFmt, cs.labelFontSizeList[lmin])
286+
287+
# Figure out label rotation.
288+
rotation,nlc = cs.calc_label_rot_and_inline(
289+
slc, imin, lw, lc if self.inline else [],
290+
self.inline_spacing )
291+
292+
cs.add_label(xmin,ymin,rotation,cs.labelLevelList[lmin],
293+
cs.labelCValueList[lmin])
299294

300295
if self.inline:
301-
# Get label width for breaking contours
302-
lw = cs.get_label_width(cs.label_levels[lmin],
303-
cs.fmt, cs.fslist[lmin])
304-
# Break contour
305-
new=cs.break_linecontour(lc,rotation,lw,imin)
306-
if len(new[0]):
307-
paths[segmin] = path.Path(new[0])
308-
if len(new[1]):
309-
paths.extend([path.Path(new[1])])
296+
# Remove old, not looping over paths so we can do this up front
297+
paths.pop(segmin)
298+
299+
# Add paths if not empty or single point
300+
for n in nlc:
301+
if len(n)>1:
302+
paths.append( path.Path(n) )
310303

311304
self.fig.canvas.draw()
312305
else: # Remove event if not valid
@@ -320,14 +313,21 @@ def button3(self,event):
320313
broken contour - once humpty-dumpty is broken, he can't be put
321314
back together. In inline mode, this does nothing.
322315
"""
316+
# Remove this last event - not too important for clabel use
317+
# since clabel normally doesn't have a maximum number of
318+
# events, but best for cleanliness sake.
319+
BlockingInput.pop(self)
320+
323321
if self.inline:
324322
pass
325323
else:
326324
self.cs.pop_label()
327325
self.cs.ax.figure.canvas.draw()
328326

329-
def __call__(self,inline,n=-1,timeout=-1):
327+
def __call__(self,inline,inline_spacing=5,n=-1,timeout=-1):
330328
self.inline=inline
329+
self.inline_spacing=inline_spacing
330+
331331
BlockingMouseInput.__call__(self,n=n,timeout=timeout,
332332
show_clicks=False)
333333

0 commit comments

Comments
 (0)