如何在 python 中显示坐标网格线的变换?

How can I show transformation of coordinate grid lines in python?

假设我有常规的笛卡尔坐标系 $(x,y)$ 并且我考虑一个矩形网格区域 $D$(分成小方块)。我想看看域 D 如何在 Python?[=13= 中的坐标变换 T:(x,y) -> (u(x,y) ,v(x,y) ) 下映射]

我正在寻找这样的东西:

See here.

可以告诉我如何做到这一点吗?我是 python 和编程的初学者。

这并不完全是微不足道的。您需要学习如何使用绘图库来创建绘图,例如matplotlib。您还需要弄清楚如何创建、离散化和转换常规网格。 numpy 库对于此类操作非常有用。无论如何,我不确定这对于“完全初学者”来说是不是一个好问题。如果您根本不懂编程或 Python,我建议您从更简单的东西开始。

如果我没理解错的话,您希望能够看到某种变换后的笛卡尔坐标 space 的网格图?在那种情况下,也许是这样的,使用 Numpy 和 Matplotlib。 (你也可以用Pillow画一些线,但是这样更方便...)

编辑: 根据评论中的讨论,与更简单的原始版本(参见编辑历史)相比,我对此做了一些更改,以便更容易绘制多个转换,如以及为线条着色以使其更容易理解它们是如何转换的。不过,它仍然 漂亮 简单。 (为了好玩,我还加入了一些额外的转换。)

import math

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

colormap = cm.get_cmap("rainbow")


def plot_grid(
    xmin: float,
    xmax: float,
    ymin: float,
    ymax: float,
    n_lines: int,
    line_points: int,
    map_func,
):
    """
    Plot a transformation of a regular grid.

    :param xmin: Minimum x value
    :param xmax: Maximum x value
    :param ymin: Minimum y value
    :param ymax: Maximum y value
    :param n_lines: Number of lines per axis
    :param line_points: Number of points per line
    :param map_func: Function to map the grid points to new coordinates
    """
    # List for gathering the lines into.
    lines = []

    # Iterate over horizontal lines.
    for y in np.linspace(ymin, ymax, n_lines):
        lines.append([map_func(x, y) for x in np.linspace(xmin, xmax, line_points)])

    # Iterate over vertical lines.
    for x in np.linspace(xmin, xmax, n_lines):
        lines.append([map_func(x, y) for y in np.linspace(ymin, ymax, line_points)])

    # Plot all the lines.
    for i, line in enumerate(lines):
        p = i / (len(lines) - 1)  # Normalize to 0-1.
        # Transpose the list of points for passing to plot.
        xs, ys = zip(*line)
        # Get the line color from the colormap.
        plt.plot(xs, ys, color=colormap(p))


# Define some mapping functions.


def identity(x, y):
    return x, y


def example(x, y):
    c = complex(x, y) ** 2
    return (c.real, c.imag)


def wobbly(x: float, y: float):
    return x + math.sin(y * 2) * 0.2, y + math.cos(x * 2) * 0.3


def vortex(x: float, y: float):
    dst = (x - 2) ** 2 + (y - 2) ** 2
    ang = math.atan2(y - 2, x - 2)
    return math.cos(ang - dst * 0.1) * dst, math.sin(ang - dst * 0.1) * dst


# Set up the plot surface...
plt.figure(figsize=(8, 8))
plt.tight_layout()

plt.subplot(2, 2, 1)
plt.title("Identity")
plot_grid(0, 4, 0, 4, 10, 10, identity)

plt.subplot(2, 2, 2)
plt.title("Example")
plot_grid(0, 4, 0, 4, 10, 10, example)

plt.subplot(2, 2, 3)
plt.title("Wobbly")
plot_grid(0, 4, 0, 4, 10, 40, wobbly)

plt.subplot(2, 2, 4)
plt.title("Vortex")
plot_grid(0, 4, 0, 4, 10, 40, vortex)

plt.savefig("so71735261-2.png")
plt.show()

结果图像是: