2

Im new on python. Im trying to use pcolor from matplotlib for gridx, gridy and V, all size 401x121. Function is as following:

def plotSpatialKM(V,gridx,gridy,step_h):
    plt.figure(figsize=(18,8), dpi=80, facecolor='white')
    plt.pcolor(gridx,gridy,V)
    plt.colorbar()
    plt.xlim(gridx.min(), gridx.max())
    plt.ylim(gridy.min(), gridy.max())
    plt.xlabel('x/h',fontsize=FONTSIZE)
    plt.ylabel('y/h',fontsize=FONTSIZE)
    plt.xticks(fontsize=FONTSIZE)
    plt.yticks(fontsize=FONTSIZE)    
    plt.show()
    return(1) 

But all the time, it causes following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 276, in resize
    self.show()
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 439, in draw
    self.figure.draw(self.renderer)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 999, in draw
    func(*args)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 2086, in draw
    a.draw(renderer)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\collections.py", line 755, in draw
    return Collection.draw(self, renderer)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\collections.py", line 244, in draw
    self.update_scalarmappable()
  File "C:\Python27\lib\site-packages\matplotlib\collections.py", line 609, in update_scalarmappable
    raise ValueError('Collections can only map rank 1 arrays')
ValueError: Collections can only map rank 1 arrays

Please help out!

5
  • Please help me with this! Commented Feb 7, 2013 at 9:52
  • Please post a self-contained example, which runs out of the box. My guess is that your input arrays have the wrong shape, but I can't tell from the code you're posting. Commented Feb 7, 2013 at 9:59
  • well, I checked the size of 3 arrays, perfectly match. Sorry I cannot put all the code, since data file is so big! Commented Feb 7, 2013 at 10:21
  • 1
    You should still give a minimal example with random data, where the error occurs, such that people can actually reproduce the problem. You don't need to give your original data. Anyway, what are the dimensions of your input arrays? Commented Feb 7, 2013 at 10:35
  • as I meantion, all 3 vectors are 401x121. Gridx like [[-0.006 -0.0059 -0.0058...] [-0.006 -0.0059 -0.0058...] .........................]] Gridy like [[0 0 0 0 0..............] [0.001 0.0001 ...........] .........................]] and V is same size, but magnitude! Thanks for your help! Commented Feb 8, 2013 at 1:07

1 Answer 1

1

I have encountered the same error using np.matrix.

import matplotlib.pyplot as plt
import numpy as np

A = np.random.random([8, 8])

plt.colormesh(A) # no error
M = np.matrix(A)
plt.colormesh(M) # the same error

The numpy matrices do not act at all like ndarrays (for example all(M[0][0][0][0][0] == M[0]) is True even if M.ndim == 2) and here matplotlib does not take that into account... You just have to create a ndarray from the matrix:

plt.colormesh(np.array(M)) # no error
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.