有人可以解释这个matplotlib pcolormesh怪癖吗? [英] Can someone explain this matplotlib pcolormesh quirk?

查看:41
本文介绍了有人可以解释这个matplotlib pcolormesh怪癖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用带有x和y数据的pcolormesh绘制数组会删除数据行

为说明我的意思,请参见以下内容:

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

此5x5网格的结果:

但是如果我想为这样的数据定义 x 和 y 轴,pcolormesh 会创建一个 4x4 网格,顶部和右侧的行丢失...

  plt.pcolormesh(range(5),range(5),数据)

为了获得完整的5x5图,我必须做

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

这只是 pcolormesh 的一个怪癖吗?或者有人可以解释为什么 pcolormesh 会这样?

解决方案

您看到的行为是预期的.

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:

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)

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

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

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

解决方案

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.

这篇关于有人可以解释这个matplotlib pcolormesh怪癖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆