Contour/Contourf misunderstanding?

Hi All,

    I am trying to create a contour map with matplotlib. I have
modified the source code for the contour sample which comes with the
matplotlib 0.98.3 online documentation: I am using the "contour(X, Y,
Z, V)" API call and, as the docs say:

"""
contour(X,Y,Z,V)
draw contour lines at the values specified in sequence V
"""

I have specified a 20-elements vector in V, buit I still only see 6
contours being drawn, namely the ones at the values:

[-1.0, -0.5, 0.0, 0.5, 1.0, 1.5]

I attach my small sample to the message. Am I doing something worng in
my call to contour? Why I am unable to see the 20 contour I specified
in my vector V?

Another related problem is with contourf: if I modify the attached
sample to use contourf and clabel, I get this error:

Traceback (most recent call last):
  File "E:\MyProjects\prova143.py", line 24, in <module>
    plt.clabel(CS, fontsize=9, inline=1)
  File "C:\Python25\lib\site-packages\matplotlib\pyplot.py", line
1736, in clabel
    ret = gca().clabel(*args, **kwargs)
  File "C:\Python25\lib\site-packages\matplotlib\axes.py", line 6027, in clabel
    return CS.clabel(*args, **kwargs)
  File "C:\Python25\lib\site-packages\matplotlib\contour.py", line
161, in clabel
    self.labelCValueList = np.take(self.cvalues, self.labelIndiceList)
  File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
85, in take
    return take(indices, axis, out, mode)
IndexError: index out of range for array

This is in Windows XP, Python 2.5.2, matplotlib 0.98.3, numpy 1.2.0.

Thank you for your suggestions.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

# CODE START

#!/usr/bin/env python

import matplotlib
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

V = np.linspace(np.min(np.min(Z)), np.max(np.max(Z)), 20)

print V

# And you can manually specify the colors of the contour
plt.figure()
CS = plt.contourf(X, Y, Z, V=V)
plt.clabel(CS, fontsize=9, inline=1)

plt.show()

# CODE END

Dear Andrea,

Greetings. I have tried your script here.

2008/12/10 Andrea Gavana <andrea.gavana@...287...>:

I attach my small sample to the message. Am I doing something worng in
my call to contour? Why I am unable to see the 20 contour I specified
in my vector V?

But you can. Simply change the line below to:

CS = plt.contourf(X, Y, Z, V=V)

CS = plt.contourf(X, Y, Z, V)

(I did not understand the reason behind the "V=V" in the argument list?)

Another related problem is with contourf: if I modify the attached
sample to use contourf and clabel, I get this error:

Well, this one I do not understand myself.

The plot I generated is attached.

HTH,

Ciao,

gavana.png

···

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@...287...
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Hi Mauro & All,

Dear Andrea,

Greetings. I have tried your script here.

2008/12/10 Andrea Gavana <andrea.gavana@...287...>:

I attach my small sample to the message. Am I doing something worng in
my call to contour? Why I am unable to see the 20 contour I specified
in my vector V?

But you can. Simply change the line below to:

CS = plt.contourf(X, Y, Z, V=V)

CS = plt.contourf(X, Y, Z, V)

(I did not understand the reason behind the "V=V" in the argument list?)

Ah! I thought "V" was a keyword argument. Shame on me. Thank you for the hint.

Another related problem is with contourf: if I modify the attached
sample to use contourf and clabel, I get this error:

Well, this one I do not understand myself.

Uhm, I will wait for further suggestions. Maybe I am doing something
stupid, again :wink:

Thank you!

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On Wed, Dec 10, 2008 at 10:32 AM, Mauro Cavalcanti wrote:

Andrea Gavana wrote:

Hi Mauro & All,

Dear Andrea,

Greetings. I have tried your script here.

2008/12/10 Andrea Gavana <andrea.gavana@...287...>:

I attach my small sample to the message. Am I doing something worng in
my call to contour? Why I am unable to see the 20 contour I specified
in my vector V?

But you can. Simply change the line below to:

CS = plt.contourf(X, Y, Z, V=V)
CS = plt.contourf(X, Y, Z, V)

(I did not understand the reason behind the "V=V" in the argument list?)

Ah! I thought "V" was a keyword argument. Shame on me. Thank you for the hint.

Another related problem is with contourf: if I modify the attached
sample to use contourf and clabel, I get this error:

Well, this one I do not understand myself.

Uhm, I will wait for further suggestions. Maybe I am doing something
stupid, again :wink:

Not stupid at all; it just happens that clabel works only with contour, not with contourf. Clabel operates on the LineCollection generated by contour, but contourf generates a PolyCollection. It should be possible to call contourf, then contour, then clabel, and then make the LineCollection from contour invisible, but I don't have time now to come up with an example.

Eric

···

On Wed, Dec 10, 2008 at 10:32 AM, Mauro Cavalcanti wrote:

Thank you!

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users