如何在Python图中翻译/移位梯度矩阵?

发布于 2025-01-30 19:02:09 字数 6176 浏览 2 评论 0原文

上下文

假设我有以下梯度矩阵:

[[0.         0.5        1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.5        1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.2        0.4        0.6        0.8        1.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.2        0.4        0.6        0.8        1.         0.         0.         0.         0.         0.         0.        ]]

产生以下图:

”在此处输入图像描述”

MWE

可以使用仅显示运行矩阵的梯度的补丁创建图像,

import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.path import Path
from scipy.ndimage import rotate


def generate_patch_plot():
    """Creates the geometry of the logo."""
    E=(8.0, 17.32050807568877)
    F=(10.0, 17.32050807568877)
    G=(16.0, 0)
    H=(20.0, 0.0)

    fig = plt.figure()
    ax = fig.add_subplot(111, aspect="equal")
    square_patch = Path([(0, 0), (0, 20), (20, 20), (20, 0), (0, 0)])
    square_path_patch = PathPatch(square_patch, facecolor="none")
    ax.add_patch(square_path_patch)
    gradient_matrix=[[0,0.6,0],[0,0.6,0],[0,0.6,0]]
    plt.plot(E[0], E[1], "ro", label="E")
    plt.plot(F[0], F[1], "yo", label="F")
    plt.plot(G[0], G[1], "bo", label="G")
    plt.plot(H[0], H[1], "co", label="H")
    im = plt.imshow(
        gradient_matrix,  # Gradient matrix
        interpolation="bilinear",
        origin="lower",  # Something
        cmap=plt.cm.hsv,
        extent=[0, 20, 0, 20],
        # extent=extension_domain,
        # clip_path=patch, # original.
        clip_path=square_path_patch,
        clip_on=True,
    )
    # im.set_clip_path(patch) # original.
    im.set_clip_path(square_path_patch)
    plt.show()

其中e,f,f,f,g ,H是补丁的左上,右上,左下的左下右下坐标。补丁可确保仅在补丁中可见梯度,并且矩阵的其余部分被忽略了。但是,当我查看生成的绘图时,矩阵将从左上绘制为y = 0 to y = y = 15

问题

一个人如何将此矩阵从x = 4转移到-24,然后从y = 7转移到-27?

Context

Suppose I have the following gradient matrix:

[[0.         0.5        1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.5        1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.33333333 0.66666667 1.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.2        0.4        0.6        0.8        1.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.25       0.5        0.75       1.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.2        0.4        0.6        0.8        1.         0.         0.         0.         0.         0.         0.        ]]

Which yields the following plot:

enter image description here

MWE

That image can be created with a patch that only displays the gradient that runs accross the matrix with:

import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.path import Path
from scipy.ndimage import rotate


def generate_patch_plot():
    """Creates the geometry of the logo."""
    E=(8.0, 17.32050807568877)
    F=(10.0, 17.32050807568877)
    G=(16.0, 0)
    H=(20.0, 0.0)

    fig = plt.figure()
    ax = fig.add_subplot(111, aspect="equal")
    square_patch = Path([(0, 0), (0, 20), (20, 20), (20, 0), (0, 0)])
    square_path_patch = PathPatch(square_patch, facecolor="none")
    ax.add_patch(square_path_patch)
    gradient_matrix=[[0,0.6,0],[0,0.6,0],[0,0.6,0]]
    plt.plot(E[0], E[1], "ro", label="E")
    plt.plot(F[0], F[1], "yo", label="F")
    plt.plot(G[0], G[1], "bo", label="G")
    plt.plot(H[0], H[1], "co", label="H")
    im = plt.imshow(
        gradient_matrix,  # Gradient matrix
        interpolation="bilinear",
        origin="lower",  # Something
        cmap=plt.cm.hsv,
        extent=[0, 20, 0, 20],
        # extent=extension_domain,
        # clip_path=patch, # original.
        clip_path=square_path_patch,
        clip_on=True,
    )
    # im.set_clip_path(patch) # original.
    im.set_clip_path(square_path_patch)
    plt.show()

where E,F,G,H are the top left, top right, bottom left bottom right coordinates of the patch. The patch ensures only the gradient is visible inside the patch, and that the rest of the matrix is ignored outside the patch. However, when I look at the plot that is generated, the matrix is plotted from top left at y=0 to y=15.

Question

How could one shift this matrix to be displayed from x = 4 to -24, and from y = 7 to -27?

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

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

发布评论

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

评论(1

电影里的梦 2025-02-06 19:02:09

可以使用patch扩展在某个位置/偏移量绘制颜色梯度补丁的XY问题。下面的代码生成3个具有不同颜色渐变矩阵和不同颜色图的单独的补丁:

代码

# -*- coding: utf-8 -*-
"""Generates three separate patches with a colour gradient that co-exist in the
same geometry."""


import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.path import Path

from src.create_gradient import re_order_coordinates


def generate_patch_plot():
    """Creates the geometry of the logo."""
    E = (8.0, 17.32050807568877)
    F = (10.0, 17.32050807568877)
    G = (16.0, 0)
    H = (20.0, 0.0)

    fig = plt.figure()
    ax = fig.add_subplot(111, aspect="equal")

    # Create gradient matrix for that segment.
    gradient_matrix_top_right = [[0, 0, 0], [0, 0.6, 0], [0, 0.6, 0]]
    gradient_matrix_mid = [[0, 0, 0], [1, 1, 1], [0, 0, 0]]
    gradient_matrix_bottom_left = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
    # First row is on bottom, last row on top.

    # Plot the patch coordinates.
    # TODO: shift left and down for manual verification.
    plt.plot(E[0], E[1], "ro", label="E")
    plt.plot(F[0], F[1], "yo", label="F")
    plt.plot(G[0], G[1], "bo", label="G")
    plt.plot(H[0], H[1], "co", label="H")

    extent_one = [0, 1, 0, 1]
    extent_two = [1, 2, 1, 2]
    extent_three = [-1, -2, -1, -2]

    # Create the first image
    plt.imshow(
        gradient_matrix_top_right,  # Gradient matrix
        interpolation="bilinear",
        origin="lower",  # Something
        cmap=plt.cm.hsv,
        extent=extent_one,
        # extent=extension_domain,
        # clip_path=patch, # original.
        clip_path=add_first_patch(ax),
        clip_on=True,
    )

    # Create the second
    plt.imshow(
        gradient_matrix_mid,  # Gradient matrix
        interpolation="bilinear",
        origin="lower",  # Something
        cmap=plt.cm.summer_r,
        extent=extent_two,
        # extent=extension_domain,
        # clip_path=patch, # original.
        clip_path=add_second_patch(ax),
        clip_on=True,
    )

    # Create the bottom left image
    plt.imshow(
        gradient_matrix_bottom_left,  # Gradient matrix
        interpolation="bilinear",
        origin="lower",  # Something
        cmap=plt.cm.summer_r,
        extent=extent_three,
        # extent=extension_domain,
        # clip_path=patch, # original.
        clip_path=add_third_patch(ax),
        clip_on=True,
    )

    # im.set_clip_path(patch) # original.
    # im.set_clip_path(square_path_patch)
    ax.set_xlim((-2, 2))
    ax.set_ylim((-2, 2))
    plt.show()


def create_4_coord_patch(top_left, top_right, bottom_left, bottom_right):
    """Generate a patch that describes the visible part of the gradient
    matrix."""
    top_left, top_right, bottom_left, bottom_right = re_order_coordinates(
        top_left, top_right, bottom_left, bottom_right
    )
    square_patch = Path(
        [top_left, top_right, bottom_right, bottom_left, top_left]
    )
    print(square_patch)
    square_path_patch = PathPatch(square_patch, facecolor="none")
    return square_path_patch


def create_square_patch(top_left, top_right, bottom_left, bottom_right):
    """Generate a patch that describes the visible part of the gradient
    matrix."""
    top_left, top_right, bottom_left, bottom_right = re_order_coordinates(
        top_left, top_right, bottom_left, bottom_right
    )
    bottom_left = (0, 0)
    top_left = (0, top_left[1])
    top_right = (400, 20)

    square_patch = Path(
        [top_left, top_right, bottom_right, bottom_left, top_left]
    )
    print(square_patch)
    square_path_patch = PathPatch(square_patch, facecolor="none")
    return square_path_patch


def add_first_patch(ax):
    """Adds a patch to the top right of the plot."""
    # First patch.
    top_left = (0, 1)
    top_right = (1, 1)
    bottom_left = (0, 0)
    bottom_right = (1, 0)
    square_path_patch_one = create_4_coord_patch(
        top_left, top_right, bottom_left, bottom_right
    )
    ax.add_patch(square_path_patch_one)
    return square_path_patch_one


def add_second_patch(ax):
    """Adds a patch to the top right of the origin of the plot."""
    # Second patch.
    top_left = (1, 2)
    top_right = (2, 2)
    bottom_left = (1, 1)
    bottom_right = (2, 1)
    square_path_patch_two = create_4_coord_patch(
        top_left, top_right, bottom_left, bottom_right
    )
    ax.add_patch(square_path_patch_two)
    return square_path_patch_two


def add_third_patch(ax):
    """Adds a patch to the bottom left of the origin of the plot."""
    # Third patch.
    top_left = (-2, -1)
    top_right = (-1, -1)
    bottom_left = (-2, -2)
    bottom_right = (-1, -2)
    square_path_patch_three = create_4_coord_patch(
        top_left, top_right, bottom_left, bottom_right
    )
    ax.add_patch(square_path_patch_three)
    return square_path_patch_three

输出

”在此处输入图像描述”

The XY-problem of plotting a colour gradient patch at a certain location/offset can be realised using a patch and extend. The code below generates 3 separate patches with different colour gradient matrices and different colour maps:

Code

# -*- coding: utf-8 -*-
"""Generates three separate patches with a colour gradient that co-exist in the
same geometry."""


import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.path import Path

from src.create_gradient import re_order_coordinates


def generate_patch_plot():
    """Creates the geometry of the logo."""
    E = (8.0, 17.32050807568877)
    F = (10.0, 17.32050807568877)
    G = (16.0, 0)
    H = (20.0, 0.0)

    fig = plt.figure()
    ax = fig.add_subplot(111, aspect="equal")

    # Create gradient matrix for that segment.
    gradient_matrix_top_right = [[0, 0, 0], [0, 0.6, 0], [0, 0.6, 0]]
    gradient_matrix_mid = [[0, 0, 0], [1, 1, 1], [0, 0, 0]]
    gradient_matrix_bottom_left = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
    # First row is on bottom, last row on top.

    # Plot the patch coordinates.
    # TODO: shift left and down for manual verification.
    plt.plot(E[0], E[1], "ro", label="E")
    plt.plot(F[0], F[1], "yo", label="F")
    plt.plot(G[0], G[1], "bo", label="G")
    plt.plot(H[0], H[1], "co", label="H")

    extent_one = [0, 1, 0, 1]
    extent_two = [1, 2, 1, 2]
    extent_three = [-1, -2, -1, -2]

    # Create the first image
    plt.imshow(
        gradient_matrix_top_right,  # Gradient matrix
        interpolation="bilinear",
        origin="lower",  # Something
        cmap=plt.cm.hsv,
        extent=extent_one,
        # extent=extension_domain,
        # clip_path=patch, # original.
        clip_path=add_first_patch(ax),
        clip_on=True,
    )

    # Create the second
    plt.imshow(
        gradient_matrix_mid,  # Gradient matrix
        interpolation="bilinear",
        origin="lower",  # Something
        cmap=plt.cm.summer_r,
        extent=extent_two,
        # extent=extension_domain,
        # clip_path=patch, # original.
        clip_path=add_second_patch(ax),
        clip_on=True,
    )

    # Create the bottom left image
    plt.imshow(
        gradient_matrix_bottom_left,  # Gradient matrix
        interpolation="bilinear",
        origin="lower",  # Something
        cmap=plt.cm.summer_r,
        extent=extent_three,
        # extent=extension_domain,
        # clip_path=patch, # original.
        clip_path=add_third_patch(ax),
        clip_on=True,
    )

    # im.set_clip_path(patch) # original.
    # im.set_clip_path(square_path_patch)
    ax.set_xlim((-2, 2))
    ax.set_ylim((-2, 2))
    plt.show()


def create_4_coord_patch(top_left, top_right, bottom_left, bottom_right):
    """Generate a patch that describes the visible part of the gradient
    matrix."""
    top_left, top_right, bottom_left, bottom_right = re_order_coordinates(
        top_left, top_right, bottom_left, bottom_right
    )
    square_patch = Path(
        [top_left, top_right, bottom_right, bottom_left, top_left]
    )
    print(square_patch)
    square_path_patch = PathPatch(square_patch, facecolor="none")
    return square_path_patch


def create_square_patch(top_left, top_right, bottom_left, bottom_right):
    """Generate a patch that describes the visible part of the gradient
    matrix."""
    top_left, top_right, bottom_left, bottom_right = re_order_coordinates(
        top_left, top_right, bottom_left, bottom_right
    )
    bottom_left = (0, 0)
    top_left = (0, top_left[1])
    top_right = (400, 20)

    square_patch = Path(
        [top_left, top_right, bottom_right, bottom_left, top_left]
    )
    print(square_patch)
    square_path_patch = PathPatch(square_patch, facecolor="none")
    return square_path_patch


def add_first_patch(ax):
    """Adds a patch to the top right of the plot."""
    # First patch.
    top_left = (0, 1)
    top_right = (1, 1)
    bottom_left = (0, 0)
    bottom_right = (1, 0)
    square_path_patch_one = create_4_coord_patch(
        top_left, top_right, bottom_left, bottom_right
    )
    ax.add_patch(square_path_patch_one)
    return square_path_patch_one


def add_second_patch(ax):
    """Adds a patch to the top right of the origin of the plot."""
    # Second patch.
    top_left = (1, 2)
    top_right = (2, 2)
    bottom_left = (1, 1)
    bottom_right = (2, 1)
    square_path_patch_two = create_4_coord_patch(
        top_left, top_right, bottom_left, bottom_right
    )
    ax.add_patch(square_path_patch_two)
    return square_path_patch_two


def add_third_patch(ax):
    """Adds a patch to the bottom left of the origin of the plot."""
    # Third patch.
    top_left = (-2, -1)
    top_right = (-1, -1)
    bottom_left = (-2, -2)
    bottom_right = (-1, -2)
    square_path_patch_three = create_4_coord_patch(
        top_left, top_right, bottom_left, bottom_right
    )
    ax.add_patch(square_path_patch_three)
    return square_path_patch_three

Output

enter image description here

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