contour_image.py :  » Chart-Report » Matplotlib » matplotlib-0.99.1.1 » doc » mpl_examples » pylab_examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Chart Report » Matplotlib 
Matplotlib » matplotlib 0.99.1.1 » doc » mpl_examples » pylab_examples » contour_image.py
#!/usr/bin/env python
'''
Test combinations of contouring, filled contouring, and image plotting.
For contour labelling, see contour_demo.py.

The emphasis in this demo is on showing how to make contours register
correctly on images, and on how to get both of them oriented as
desired.  In particular, note the usage of the "origin" and "extent"
keyword arguments to imshow and contour.
'''
from pylab import *

#Default delta is large because that makes it fast, and it illustrates
# the correct registration between image and contours.
delta = 0.5

extent = (-3,4,-4,3)

x = arange(-3.0, 4.001, delta)
y = arange(-4.0, 3.001, delta)
X, Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = (Z1 - Z2) * 10

levels = arange(-2.0, 1.601, 0.4) # Boost the upper limit to avoid truncation
                                  # errors.

figure()


subplot(2,2,1)

cset1 = contourf(X, Y, Z, levels,
                        cmap=cm.get_cmap('jet', len(levels)-1),
                        )
# It is not necessary, but for the colormap, we need only the
# number of levels minus 1.  To avoid discretization error, use
# either this number or a large number such as the default (256).

#If we want lines as well as filled regions, we need to call
# contour separately; don't try to change the edgecolor or edgewidth
# of the polygons in the collections returned by contourf.
# Use levels output from previous call to guarantee they are the same.
cset2 = contour(X, Y, Z, cset1.levels,
                        colors = 'k',
                        hold='on')
# We don't really need dashed contour lines to indicate negative
# regions, so let's turn them off.
for c in cset2.collections:
    c.set_linestyle('solid')

# It is easier here to make a separate call to contour than
# to set up an array of colors and linewidths.
# We are making a thick green line as a zero contour.
# Specify the zero level as a tuple with only 0 in it.
cset3 = contour(X, Y, Z, (0,),
                colors = 'g',
                linewidths = 2,
                hold='on')
title('Filled contours')
colorbar(cset1)
#hot()


subplot(2,2,2)

imshow(Z, extent=extent)
v = axis()
contour(Z, cset3.levels, hold='on', colors = 'k',
        origin='upper', extent=extent)
axis(v)
title("Image, origin 'upper'")

subplot(2,2,3)

imshow(Z, origin='lower', extent=extent)
v = axis()
contour(Z, cset3.levels, hold='on', colors = 'k',
        origin='lower', extent=extent)
axis(v)
title("Image, origin 'lower'")

subplot(2,2,4)

# We will use the interpolation "nearest" here to show the actual
# image pixels.
# Note that the contour lines don't extend to the edge of the box.
# This is intentional. The Z values are defined at the center of each
# image pixel (each color block on the following subplot), so the
# domain that is contoured does not extend beyond these pixel centers.
im = imshow(Z, interpolation='nearest', extent=extent)
v = axis()
contour(Z, cset3.levels, hold='on', colors = 'k',
        origin='image', extent=extent)
axis(v)
ylim = get(gca(), 'ylim')
setp(gca(), ylim=ylim[::-1])
title("Image, origin from rc, reversed y-axis")
colorbar(im)

show()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.