PYPLOT中数据方向的惯例是什么?

发布于 2025-01-31 01:42:24 字数 1912 浏览 1 评论 0原文

有两种不同的方法来调用Pyplot的轮廓图函数,如下所示:

import matplotlib.pyplot as plt
import numpy as np

def main():
    x = np.array([0, 1, 2, 3, 4])  # x has length M=5.
    y = np.array([10, 20, 30])  # y has length N=3

    x_grid, y_grid = np.meshgrid(x, y, indexing='ij')

    """
    x_grid = array(
    [[0, 0, 0],
     [1, 1, 1],
     [2, 2, 2],
     [3, 3, 3],
     [4, 4, 4]])

    x_grid varies along index 0 (the rows). Similarly, y_grid varies along index 1
    (the columns). x_grid and y_grid have shape (M, N) = (5, 3).
    """

    z_grid = x_grid + y_grid
    """
    z_grid = array(
    [[10, 20, 30],
     [11, 21, 31],
     [12, 22, 32],
     [13, 23, 33],
     [14, 24, 34]])

    z_grid varies by 1 along the rows and 10 along the columns.
    """

    fig, axes = plt.subplots(1, 2)

    # First call signature of contour
    contour = axes[0].contour(x_grid, y_grid, z_grid, levels=20)
    axes[0].clabel(contour, inline=1, fontsize=10)
    axes[0].set_xlabel('x')
    axes[0].set_ylabel('y')
    axes[0].grid()
    axes[0].set_title("First call signature")

    # Second call signature of contour
    contour = axes[1].contour(y, x, z_grid, levels=20)
    axes[1].clabel(contour, inline=1, fontsize=10)
    axes[1].set_xlabel('y')
    axes[1].set_ylabel('x')
    axes[1].grid()
    axes[1].set_title("Second call signature")

    plt.show()

if __name__ == "__main__":
    main()

< img src =“ https://i.sstatic.net/rkxaz.png” alt =“在此处输入图像说明”>

在第一种情况下,我通过meshgrid 对于xy,而在第二种情况下,我将我传递给meshgrid for x >和y。但是,我不得不将它们的订单交换,因为尽管我将相同的相关数据Z_GRID Contour()在两种情况下。

为什么轮廓以这种方式工作?

There are two different ways to call pyplot's contour plot function, as illustrated here:

import matplotlib.pyplot as plt
import numpy as np

def main():
    x = np.array([0, 1, 2, 3, 4])  # x has length M=5.
    y = np.array([10, 20, 30])  # y has length N=3

    x_grid, y_grid = np.meshgrid(x, y, indexing='ij')

    """
    x_grid = array(
    [[0, 0, 0],
     [1, 1, 1],
     [2, 2, 2],
     [3, 3, 3],
     [4, 4, 4]])

    x_grid varies along index 0 (the rows). Similarly, y_grid varies along index 1
    (the columns). x_grid and y_grid have shape (M, N) = (5, 3).
    """

    z_grid = x_grid + y_grid
    """
    z_grid = array(
    [[10, 20, 30],
     [11, 21, 31],
     [12, 22, 32],
     [13, 23, 33],
     [14, 24, 34]])

    z_grid varies by 1 along the rows and 10 along the columns.
    """

    fig, axes = plt.subplots(1, 2)

    # First call signature of contour
    contour = axes[0].contour(x_grid, y_grid, z_grid, levels=20)
    axes[0].clabel(contour, inline=1, fontsize=10)
    axes[0].set_xlabel('x')
    axes[0].set_ylabel('y')
    axes[0].grid()
    axes[0].set_title("First call signature")

    # Second call signature of contour
    contour = axes[1].contour(y, x, z_grid, levels=20)
    axes[1].clabel(contour, inline=1, fontsize=10)
    axes[1].set_xlabel('y')
    axes[1].set_ylabel('x')
    axes[1].grid()
    axes[1].set_title("Second call signature")

    plt.show()

if __name__ == "__main__":
    main()

enter image description here

In the first case, I pass the 2D arrays returned by meshgrid for X and Y, whereas in the second case I pass the 1D arrays that I passed into meshgrid for X and Y. However, I had to swap their order, because the plots are transposed despite the fact that I've passed the same dependent data z_grid into contour() in both cases.

Why does contour work this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜血缘 2025-02-07 01:42:24

它是关于meshgrid使用的索引。默认值(请参阅在这里)是笛卡尔索引( ”),这是matplotlib使用的那个(请参阅)。

假设您要创建一个轮廓:

contour(X, Y, Z)

选项1

一个选项是提供X,Y和Z的所有尺寸相同。这是您在第一种情况下所做的。 x和y是由:和z创建的,

X, Y = np.meshgrid(x, y, indexing='ij')
#x_grid, y_grid

然后以与x和y相同的2D大小创建。在这种情况下,轮廓不需要假设任何东西,因为您已经以相同形式的2D网格提供了三个参数。

选项2

另一方面,您可以提供1D阵列。但是,在这种情况下,轮廓需要单独运行meshgrid。类似

X, Y = np.meshgrid(x, y, indexing='xy')

Contour使用“ XY”索引。这意味着这些X和Y现在与上面的X不同,因为在每种情况下使用了独特的索引。

因此,在第二个选项中,轮廓假设z是在“ xy”索引下创建的。

因此:

x -> size (N)
y -> size (M)
X -> size (M, N) 
Y -> size (M, N) 

但是...

Z -> size (N,M) 

因为您由“ IJ”创建Z,但是Contour由“ XY”创建X和Y。因此,不匹配。

当您绘制轮廓(y,x,z_grid)时,现在x和y现在的形状与z相同,因此轮廓不会丢弃任何错误,因为所有数组均以表单为单位( n,m)。

It is about the indexing used by meshgrid. The default (see here) is Cartesian indexing ("xy"), which is the one used by Matplotlib (see here).

Say, you want to created a contour:

contour(X, Y, Z)

Option 1

One option is to provide X, Y and Z all with the same 2D size. This is what you do in the first case. X and Y were created by:

X, Y = np.meshgrid(x, y, indexing='ij')
#x_grid, y_grid

and Z created afterwards with the same 2D size as X and Y. In this case, contour does not need to assume anything, as you are already providing the three arguments in the same form of 2D grid.

Option 2

On the other hand, you can provide 1D arrays. However, in this case, Contour needs to run meshgrid by itself. Something like

X, Y = np.meshgrid(x, y, indexing='xy')

Contour uses "xy" indexing. This means that these X and Y are now different from the ones above, because of the distinct indexing used on each case.

Accordingly, in this second option, Contour assumes that the Z was created under "xy" indexing.

So:

x -> size (N)
y -> size (M)
X -> size (M, N) 
Y -> size (M, N) 

but...

Z -> size (N,M) 

Because you created Z by "ij", but Contour created X and Y by "xy". Hence the mismatch.

When you plot instead contour(y, x, z_grid), now X and Y will now have the same shape as Z, so the Contour does not throw any error, because all arrays are in the form (N, M).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文