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

try*_*dom 4 python math visualization

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

我正在寻找这样的东西:

在此输入图像描述

看这里

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

AKX*_*AKX 7

如果我理解正确的话,您希望能够看到一种变换后的笛卡尔空间的网格图?在这种情况下,也许类似这样,使用 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()
Run Code Online (Sandbox Code Playgroud)

结果图像是: 在此输入图像描述