使用Matplotlib进行绘图会产生-90个旋转图 - Python

时间:2022-09-10 22:22:14

I've had this kind of "problem" when plotting with Matplotlib frequently (Same data saved generate different images - Python). As an example, I created a vector field with a numpy array like this:

当经常使用Matplotlib绘图时,我遇到了这种“问题”(保存相同的数据会生成不同的图像 - Python)。作为一个例子,我创建了一个带有numpy数组的向量字段,如下所示:

def generate_divergent_free_component(sigma, x_dim, y_dim, steps):
    divergent_free_vector_field_x = numpy.zeros((steps, steps), float)
    divergent_free_vector_field_y = numpy.zeros((steps, steps), float)

    u0 = numpy.random.uniform()
    x0 = 1.0 + sigma * u0
    y0 = sigma * u0

    dx = x_dim/float(steps)
    dy = y_dim/float(steps)

    for x, y in product(range(steps), range(steps)):
        x_norm = -x_dim/2.0 + x * dx
        y_norm = -y_dim/2.0 + y * dy
        exp0 = -(math.pow(x_norm - x0, 2) + math.pow(y_norm - y0, 2)) / 2.0

        divergent_free_vector_field_x[x, y] = -(x_norm - x0) * math.exp(exp0)
        divergent_free_vector_field_y[x, y] = -(y_norm - y0) * math.exp(exp0)

    return divergent_free_vector_field_x, divergent_free_vector_field_y

I made some tests and it seems to me that ndarrays follow row-major order and I am iterating over them following this pattern.

我做了一些测试,在我看来,ndarray遵循行主要顺序,我按照这种模式迭代它们。

However, when plotting with Matplotlib, I get the image rotated 90 degrees counter-clock wise.

但是,当使用Matplotlib进行绘图时,我会将图像逆时针旋转90度。

def plot_streamlines(file_path, x_dim, y_dim, steps, vector_field_x, vector_field_y, scalar_field=None):
    plt.figure()

    y, x = numpy.mgrid[-x_dim/2:x_dim/2:steps*1j, -y_dim/2:y_dim/2:steps*1j]

    plt.figure()

    # x, y : 1d arrays, an evenly spaced grid.
    # u, v : 2d arrays
    # x and y-velocities. Number of rows should match length of y, and the number of columns should match x.
    plt.streamplot(x, y, vector_field_x, vector_field_y, cmap=plt.cm.autumn)
    plt.savefig(file_path + '.png')
    plt.close()

As an example, I got this image:

举个例子,我得到了这个图像:

使用Matplotlib进行绘图会产生-90个旋转图 -  Python

But I was expecting (and another programs such as Matlab) the image like this (I just rotated it in my computer now, but I was expecting that point that I circled as the following image shows):

但是我期待(以及像Matlab这样的其他程序)像这样的图像(我现在只是在我的计算机中旋转它,但我期待我圈出的点,如下图所示):

使用Matplotlib进行绘图会产生-90个旋转图 -  Python

So I'm wondering if Matplotlib works or expects column-major order or something like this... I'm just trying to understand how this properly works.

所以我想知道Matplotlib是否工作或期望列主要订单或类似的东西......我只是想了解这是如何正常工作的。

Any help would be appreciated.

任何帮助,将不胜感激。

Thank you in advance.

先谢谢你。

1 个解决方案

#1


0  

The problem appears to be that plt.streamplot wants the divergent_free_vector_field arrays to be indexed [y, x], instead of [x, y].

问题似乎是plt.streamplot希望divergent_free_vector_field数组被索引[y,x],而不是[x,y]。

A good test would be to use different step sizes in x and y. You should get an AssertionError on the grid shape when you try to plot, because it's expecting the number of rows to be same as the size of y, and the number of columns to be the same as the size of x.

一个好的测试是在x和y中使用不同的步长。当您尝试绘制时,您应该在网格形状上得到AssertionError,因为它期望行数与y的大小相同,并且列数与x的大小相同。

Try changing it to this:

尝试将其更改为:

divergent_free_vector_field_x = numpy.zeros((steps_y, steps_x), float)
divergent_free_vector_field_y = numpy.zeros((steps_y, steps_x), float)
...
divergent_free_vector_field_x[y, x] = -(x_norm - x0) * math.exp(exp0)
divergent_free_vector_field_y[y, x] = -(y_norm - y0) * math.exp(exp0)

#1


0  

The problem appears to be that plt.streamplot wants the divergent_free_vector_field arrays to be indexed [y, x], instead of [x, y].

问题似乎是plt.streamplot希望divergent_free_vector_field数组被索引[y,x],而不是[x,y]。

A good test would be to use different step sizes in x and y. You should get an AssertionError on the grid shape when you try to plot, because it's expecting the number of rows to be same as the size of y, and the number of columns to be the same as the size of x.

一个好的测试是在x和y中使用不同的步长。当您尝试绘制时,您应该在网格形状上得到AssertionError,因为它期望行数与y的大小相同,并且列数与x的大小相同。

Try changing it to this:

尝试将其更改为:

divergent_free_vector_field_x = numpy.zeros((steps_y, steps_x), float)
divergent_free_vector_field_y = numpy.zeros((steps_y, steps_x), float)
...
divergent_free_vector_field_x[y, x] = -(x_norm - x0) * math.exp(exp0)
divergent_free_vector_field_y[y, x] = -(y_norm - y0) * math.exp(exp0)