如何在Python图中翻译/移位梯度矩阵?
上下文
假设我有以下梯度矩阵:
[[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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以使用
patch
和扩展
在某个位置/偏移量绘制颜色梯度补丁的XY问题。下面的代码生成3个具有不同颜色渐变矩阵和不同颜色图的单独的补丁:代码
输出
The XY-problem of plotting a colour gradient patch at a certain location/offset can be realised using a
patch
andextend
. The code below generates 3 separate patches with different colour gradient matrices and different colour maps:Code
Output