Can someone explain this matplotlib pcolormesh qui

2020-02-14 17:21发布

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?

1条回答
ゆ 、 Hurt°
2楼-- · 2020-02-14 17:30

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

查看更多
登录 后发表回答