Skip to content

Commit bc6186c

Browse files
committed
contour, contourf return ContourSet object; colorbar uses it
svn path=/trunk/matplotlib/; revision=1788
1 parent 8713fd8 commit bc6186c

File tree

10 files changed

+495
-452
lines changed

10 files changed

+495
-452
lines changed

API_CHANGES

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
1+
2+
Changed the internal structure of contour.py to a more OO style.
3+
Calls to contour or contourf in axes.py or pylab.py now return
4+
a ContourSet object which contains references to the
5+
LineCollections or PolyCollections created by the call,
6+
as well as the configuration variables that were used.
7+
The ContourSet object is a "mappable" if a colormap was used.
8+
9+
Added a clip_ends kwarg to contourf. From the docstring:
10+
* clip_ends = True
11+
If False, the limits for color scaling are set to the
12+
minimum and maximum contour levels.
13+
True (default) clips the scaling limits. Example:
14+
if the contour boundaries are V = [-100, 2, 1, 0, 1, 2, 100],
15+
then the scaling limits will be [-100, 100] if clip_ends
16+
is False, and [-3, 3] if clip_ends is True.
17+
18+
Changed Figure.colorbar():
19+
kw argument order changed;
20+
if mappable arg is a non-filled ContourSet, colorbar() shows
21+
lines instead of polygons.
22+
if mappable arg is a filled ContourSet with clip_ends=True,
23+
the endpoints are not labelled, so as to give the
24+
correct impression of open-endedness.
25+
26+
Changed LineCollection.get_linewidths to get_linewidth, for
27+
consistency.
28+
29+
130
API Changes in matplotlib-0.84
231

3-
Unified argument handling between hlines and vlines. Both now
32+
Unified argument handling between hlines and vlines. Both now
433
take optionally a fmt argument (as in plot) and a keyword args
534
that can be passed onto Line2D.
635

@@ -32,13 +61,13 @@ API Changes in matplotlib-0.83
3261

3362
from backends import pylab_setup
3463
new_figure_manager, draw_if_interactive, show = pylab_setup()
35-
64+
3665
- Moved Figure.get_width_height() to FigureCanvasBase. It now
3766
returns int instead of float.
3867

3968
API Changes in matplotlib-0.82
4069

41-
- toolbar import change in GTKAgg, GTKCairo and WXAgg
70+
- toolbar import change in GTKAgg, GTKCairo and WXAgg
4271

4372
- Added subplot config tool to GTK* backends -- note you must now
4473
import the NavigationToolbar2 from your backend of choice rather
@@ -47,7 +76,7 @@ API Changes in matplotlib-0.82
4776
wx backend -- see examples/embedding_in_wxagg.py
4877

4978

50-
- hist bin change
79+
- hist bin change
5180

5281
Sean Richards notes there was a problem in the way we created
5382
the binning for histogram, which made the last bin
@@ -63,7 +92,7 @@ API Changes in matplotlib-0.82
6392
each bin? (i.e. each value should be the lower bound of a
6493
bin). To provide the correct bins for hist would it not be
6594
something like
66-
95+
6796
def bins(xmin, xmax, N):
6897
if N==1: return xmax
6998
dx = (xmax-xmin)/N # instead of N-1
@@ -159,7 +188,7 @@ API CHANGES in matplotlib-0.72
159188
consistency with set/get introspection mechanism,
160189

161190
- pylab figure now defaults to num=None, which creates a new figure
162-
with a guaranteed unique number
191+
with a guaranteed unique number
163192

164193
- contour method syntax changed - now it is matlab compatible
165194

@@ -181,7 +210,7 @@ API CHANGES in matplotlib-0.72
181210
do not want this behavior, use fig.add_subplot or the axes
182211
command
183212

184-
API CHANGES in matplotlib-0.71
213+
API CHANGES in matplotlib-0.71
185214

186215
Significant numerix namespace changes, introduced to resolve
187216
namespace clashes between python built-ins and mlab names.
@@ -261,7 +290,7 @@ API CHANGES in matplotlib-0.63
261290

262291
Most of the date tick locators have a different meaning in their
263292
constructors. In the prior implementation, the first argument was a
264-
base and multiples of the base were ticked. Eg
293+
base and multiples of the base were ticked. Eg
265294

266295
HourLocator(5) # old: tick every 5 minutes
267296

@@ -278,7 +307,7 @@ API CHANGES in matplotlib-0.63
278307
that years are not recurrent.
279308

280309

281-
Financial functions:
310+
Financial functions:
282311

283312
matplotlib.finance.quotes_historical_yahoo(ticker, date1, date2)
284313

@@ -300,7 +329,7 @@ API CHANGES in matplotlib-0.60
300329
ColormapJet and Grayscale are deprecated. For backwards
301330
compatibility, they can be obtained either by doing
302331

303-
from matplotlib.cm import ColormapJet
332+
from matplotlib.cm import ColormapJet
304333

305334
or
306335

@@ -355,15 +384,15 @@ available on PolyCollections, eg you can say
355384
c.set_facecolor('r')
356385
c.set_alpha(0.5)
357386

358-
or
387+
or
359388

360389
c = scatter(blah, blah)
361390
set(c, 'linewidth', 1.0, 'facecolor', 'r', 'alpha', 0.5)
362391

363392

364393
Because the collection is a single object, you no longer need to loop
365394
over the return value of scatter or pcolor to set properties for the
366-
entire list.
395+
entire list.
367396

368397
If you want the different elements of a collection to vary on a
369398
property, eg to have different line widths, see matplotlib.collections
@@ -411,7 +440,7 @@ Axes creation
411440
f = Figure(figsize=(5,4), dpi=100)
412441
a = f.add_subplot(111)
413442

414-
That is, add_axis no longer exists and is replaced by
443+
That is, add_axis no longer exists and is replaced by
415444

416445
add_axes(rect, axisbg=defaultcolor, frameon=True)
417446
add_subplot(num, axisbg=defaultcolor, frameon=True)
@@ -423,12 +452,12 @@ Artist methods
423452
to draw
424453

425454
Bounding boxes
426-
--------------
455+
--------------
427456

428457
matplotlib.transforms.Bound2D is replaced by
429458
matplotlib.transforms.Bbox. If you want to construct a bbox from
430459
left, bottom, width, height (the signature for Bound2D), use
431-
matplotlib.transforms.lbwh_to_bbox, as in
460+
matplotlib.transforms.lbwh_to_bbox, as in
432461

433462
bbox = clickBBox = lbwh_to_bbox(left, bottom, width, height)
434463

@@ -466,7 +495,7 @@ Object constructors
466495
l = Line2D(dpi, bbox, x, y,
467496
color = color,
468497
transx = transx,
469-
transy = transy,
498+
transy = transy,
470499
)
471500

472501
now all you need is
@@ -502,7 +531,7 @@ Transformations
502531

503532
* because they operate on x and y together, they can do the entire
504533
transformation in one loop. Earlier I did something along the
505-
lines of
534+
lines of
506535

507536
xt = sx*func(x) + tx
508537
yt = sy*func(y) + ty
@@ -520,8 +549,8 @@ Transformations
520549
Likewise, if you are using the artist bounding boxes to pick items
521550
on the canvas with the GUI, the bbox methods are somewhat
522551
different. You will need to see the updated
523-
examples/object_picker.py.
524-
552+
examples/object_picker.py.
553+
525554
See unit/transforms_unit.py for many examples using the new
526555
transformations.
527556

@@ -559,12 +588,12 @@ API changes at 0.50
559588

560589
Migrating code:
561590

562-
Matlab interface:
591+
Matlab interface:
563592

564593
The only API change for those using the matlab interface is in how
565594
you call figure redraws for dynamically updating figures. In the
566595
old API, you did
567-
596+
568597
fig.draw()
569598

570599
In the new API, you do
@@ -594,23 +623,23 @@ Migrating code:
594623
FigureCanvas, not a Figure. The examples embedding_in_gtk.py,
595624
embedding_in_gtk2.py, and mpl_with_glade.py all reflect the new
596625
API so use these as a guide.
597-
598-
All prior calls to
626+
627+
All prior calls to
599628

600629
figure.draw() and
601-
figure.print_figure(args)
630+
figure.print_figure(args)
602631

603632
should now be
604-
633+
605634
canvas.draw() and
606-
canvas.print_figure(args)
635+
canvas.print_figure(args)
607636

608637
Apologies for the inconvenience. This refactorization brings
609638
significant more freedom in developing matplotlib and should bring
610639
better plotting capabilities, so I hope the inconvenience is worth
611640
it.
612641

613-
642+
614643

615644
API changes at 0.42
616645

@@ -628,7 +657,7 @@ API changes at 0.42
628657
backend_gtk for an example.
629658

630659
* Text._get_xy_display renamed Text.get_xy_display
631-
660+
632661
* Artist set_renderer and wash_brushes methods removed
633662

634663
* Moved Legend class from matplotlib.axes into matplotlib.legend
@@ -646,7 +675,7 @@ API changes at 0.42
646675
matplotlib.matlab.get_current_fig_manager to allow user access to
647676
the GUI window attribute, eg figManager.window for GTK and
648677
figManager.frame for wx
649-
678+
650679

651680

652681

@@ -661,7 +690,7 @@ API changes at 0.40
661690
* the clip_gc method is removed. Artists now clip themselves with
662691
their box
663692
* added _clipOn boolean attribute. If True, gc clip to bbox.
664-
693+
665694
- AxisTextBase
666695
* Initialized with a transx, transy which are Transform instances
667696
* set_drawing_area removed
@@ -685,7 +714,7 @@ API changes at 0.40
685714
* removed figbg attribute
686715
* added fig instance to __init__
687716
* resizing is handled by figure call to resize.
688-
717+
689718
- Subplot
690719
* added fig instance to __init__
691720

CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
New entries should be added at the top
22

3+
2005-09-25 contour.py modified to use a single ContourSet class
4+
that handles filled contours, line contours, and labels;
5+
added keyword arg (clip_ends) to contourf.
6+
Colorbar modified to work with new ContourSet object;
7+
if the ContourSet has lines rather than polygons, the
8+
colorbar will follow suit. Fixed a bug introduced in
9+
0.84, in which contourf(...,colors=...) was broken - EF
10+
311
2005-09-14 Added a new 'resize_event' which triggers a callback with a
412
backend_bases.ResizeEvent object - JDH
513

boilerplate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ def %(func)s(*args, **kwargs):
9292
)
9393

9494
cmappable = {
95-
'clabel' : 'if ret.mappable is not None: gci._current = ret.mappable',
96-
'contour' : 'if ret[1].mappable is not None: gci._current = ret[1].mappable',
97-
'contourf': 'if ret[1].mappable is not None: gci._current = ret[1].mappable',
95+
#'clabel' : 'if ret.mappable is not None: gci._current = ret.mappable',
96+
'contour' : 'if ret._A is not None: gci._current = ret',
97+
'contourf': 'if ret._A is not None: gci._current = ret',
9898
'scatter' : 'gci._current = ret',
9999
'pcolor' : 'gci._current = ret',
100100
'imshow' : 'gci._current = ret',

examples/contour_demo.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
'''
88
from pylab import *
99

10+
rcParams['tick.direction'] = 'out'
11+
1012
delta = 0.025
1113
x = arange(-3.0, 3.0, delta)
1214
y = arange(-2.0, 2.0, delta)
@@ -23,27 +25,29 @@
2325
# over the line segments of the contour, removing the lines beneath
2426
# the label
2527
figure()
26-
levels, colls = contour(X, Y, Z)
27-
clabel(colls, inline=1, fontsize=10)
28+
CS = contour(X, Y, Z)
29+
clabel(CS, inline=1, fontsize=10)
2830
title('Simplest default with labels')
2931

3032

31-
# You can force all the contours to be the same color
33+
# You can force all the contours to be the same color.
34+
# Use colors = 'k' to make negative contours dashed;
35+
# use colors = ('k',) to leave all contours solid.
3236
figure()
33-
levels, colls = contour(X, Y, Z, 6,
34-
colors=('k',)
35-
)
36-
clabel(colls, levels, fontsize=9, inline=1)
37+
CS = contour(X, Y, Z, 6,
38+
colors='k', # or ('k',) for all solid
39+
)
40+
clabel(CS, fontsize=9, inline=1)
3741
title('Single color')
3842

3943

4044
# And you can manually specify the colors of the contour
4145
figure()
42-
levels, colls = contour(X, Y, Z, 6,
43-
linewidths=arange(.5, 4, .5),
44-
colors=('r', 'green', 'blue', (1,1,0), '#afeeee', 0.5)
45-
)
46-
clabel(colls, levels, fontsize=9, inline=1)
46+
CS = contour(X, Y, Z, 6,
47+
linewidths=arange(.5, 4, .5),
48+
colors=('r', 'green', 'blue', (1,1,0), '#afeeee', 0.5)
49+
)
50+
clabel(CS, fontsize=9, inline=1)
4751
title('Crazy lines')
4852

4953

@@ -52,20 +56,30 @@
5256
figure()
5357
im = imshow(Z, interpolation='bilinear', origin='lower',
5458
cmap=cm.gray, extent=(-3,3,-2,2))
55-
levels, colls = contour(Z, arange(-1.2,1.6,0.2),
56-
origin='lower',
57-
linewidths=2,
58-
extent=(-3,3,-2,2))
59+
levels = arange(-1.2, 1.6, 0.2)
60+
CS = contour(Z, levels,
61+
origin='lower',
62+
linewidths=2,
63+
extent=(-3,3,-2,2))
64+
65+
#Thicken the zero contour.
66+
zc = CS.collections[6]
67+
setp(zc, linewidth=4)
5968

60-
clabel(colls, levels,
69+
clabel(CS, levels[1::2], # label every second level
6170
inline=1,
6271
fmt='%1.1f',
63-
fontsize=10)
72+
fontsize=14)
6473

65-
colorbar() # make a colorbar for the contour lines
74+
colorbar(CS) # make a colorbar for the contour lines
6675
title('Lines with colorbar')
6776
hot() # Now change the colormap for the contour lines and colorbar
77+
flag()
6878

79+
# We can still add a colorbar for the image, too.
80+
colorbar(im, orientation='horizontal')
81+
# We could manipulate the colorbar axes sizes for better
82+
# appearance, but we'll leave that for a later demo.
6983

7084
savefig('contour_demo')
7185
show()

0 commit comments

Comments
 (0)