Arun Kalyanaraman
Arun Kalyanaraman

Reputation: 648

Long error in contour plot python

I am trying to create a contour plot with the x coordinates being label EF and y being labeled EB and z being a function labeled a. It returns a long error posted below. Any help would be appreciated. The error is

File "contour.py", line 19, in <module>
c = plt.contour(EF,EB,a)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2016, in contour
    ret = ax.contour(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 7326, in contour
    return mcontour.QuadContourSet(self, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 1088, in __init__
    ContourSet.__init__(self, ax, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 686, in __init__
    self._process_args(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 1101, in _process_args
    x, y, z = self._contour_args(args, kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 1148, in _contour_args
    x,y,z = self._check_xyz(args[:3], kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 1180, in _check_xyz
    raise TypeError("Input z must be a 2D array.")
TypeError: Input z must be a 2D array.

Upvotes: 3

Views: 1063

Answers (2)

Hooked
Hooked

Reputation: 88248

The error states that

TypeError: Input z must be a 2D array. 

if you look at the sizes of the input objects:

print EF.shape, EB.shape, a.shape
(51,) (51,) (51,)

you'll see that these are not 2D arrays. Did you intend to use X and Y instead?

When I make the change to

a = ((1+.5*(np.exp(1.7*X)+np.exp(1.7*Y)+np.exp(1.7*(X+Y))))/(1+np.exp(1.7*X)+np.exp(1.7*Y)+np.exp(1.7*(X+Y))))
c = plt.contour(EF,EB,a,30)

The output is

enter image description here

It looks like you may need to adjust your parameter space since all the interesting stuff is around (0,0).

Upvotes: 8

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 59005

You just have to creat a as a meshgrid, using X abd Y instead of EF and EB:

a = ((1+.5*(np.exp(1.7*Y)+np.exp(1.7*X)+np.exp(1.7*(Y+X))))/(1+np.exp(1.7*Y)+np.exp(1.7*X)+np.exp(1.7*(Y+X))))

Another thing, if you create your meshgrids using copy=False it may prevent you from running out of memory:

(X,Y) = np.meshgrid(EF,EB, copy=False)

In this case it creates a view of your original 1D arrays.

Upvotes: 4

Related Questions