Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

python - Can someone explain this matplotlib pcolormesh quirk?

Plotting an array with pcolormesh with x and y data removes a row of the data

To illustrate what I mean, see the following:

data = np.random.random([5,5])
plt.pcolormesh(data)

results in this 5x5 grid:
enter image description here

But if I want to define the x an y axes for the data like this, pcolormesh creates a 4x4 grid, with the top and right rows missing...

plt.pcolormesh(range(5), range(5), data)

enter image description here

In order to get the full 5x5 plot, I have to do

plt.pcolormesh(range(6), range(6), data)

enter image description here

Is this just a quirk of pcolormesh? Or can someone explain the reasoning why pcolormesh behaves this way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The behaviour you see is expected. The pcolor documentation states for
pcolor(X, Y, C, **kwargs):

Ideally the dimensions of X and Y should be one greater than those of C; if the dimensions are the same, then the last row and column of C will be ignored.

The same is of course true for pcolormesh.

I'm not sure if this is the place to discuss whether this behaviour is a "quirk", but the underlying idea is that the grid defines the edges of the colored faces. Just as on a number line you have n numbers and n-1 intervals in between.

It actually makes sense to define the edges by the grid, because pcolormesh also allows for unequally spaced grids which would be impossible to define otherwise.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...