gpt4 book ai didi

python - Matplotlib masking - 根据像素当前的颜色值重置像素的 zorder?

转载 作者:行者123 更新时间:2023-12-01 07:14:11 27 4
gpt4 key购买 nike

1) 我需要 Matplotlib 中似乎不存在的屏蔽功能。(如果情况并非如此,请告诉我)。

2) Matplotlib 具有裁剪功能,但“裁剪”出的区域使用相同的 zorder,因此渲染几个重叠的多边形形状,这些形状应该根据它们的 zorder 来查看在使用剪切来渲染其中之一的区域中失败,因为我们之前在剪切区域下方渲染的任何内容都被删除(可以说全是白色)。

3)如果可以在渲染时随时“重置”给定像素颜色(我们的背景颜色)的 zorder,那么我们就可以通过裁剪解决问题。我们甚至不需要裁剪,因为我们只需对定义所需 mask 区域的部分使用给定的 zorder 值,并为要裁剪的部分使用较低的 zorder,并且在渲染相关部分后,我们更改所有的 zorder将“白色”像素设置为非常低的 zorder 值,并且我们可以使用 mask 功能!

这是一个非常简单的示例代码来说明需求:

'''Example of two overlapping squares having arbitrary openings in them.
In this example the opening is just two smaller overlapping square, but it can be anything.
We want to paint the green and red squares so that we see what is behind the opening(s)'''
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 6))

# This is to test if a well ordered polygon with slits (to define holes) can be rendered
quad_small = [(-0.2, -0.2), (-0.2, 0.2), (0.2, 0.2), (0.2, -0.2), (-0.2, -0.2)]
quad1x1 = [(-1, 1.), (-1, -1.), (1, -1.), (1., 1.), (-1, 1.)]

#Fill quad1x1 with green
x11 = [coord[0] for coord in quad1x1]
y11 = [coord[1] for coord in quad1x1]
plt.fill(x11, y11, facecolor='green', zorder=5)

'''Now make one or more holes, possibly overlapping holes,
NOTE: if we use clipping to define the holes, Matplotlib sets the same zorder over all the clippped areas
which was used. Also clipping does not work well with overlapping small squares. Would only work with nonoverlapping
squares.'''
xsq = [coord[0] for coord in quad_small]
ysq = [coord[1] for coord in quad_small]
plt.fill(xsq, ysq, facecolor='white', zorder=5)
xsq = [coord[0]+0.2 for coord in quad_small]
ysq = [coord[1]+0.2 for coord in quad_small]
plt.fill(xsq, ysq, facecolor='white', zorder=5)

'''At this point green and white openings have the same zorder=5.
We would need a call to change the zorder of all the 'white' pixels to a lower value, say 3'''

'''Now we want to render another polygon (red) with holes, but ONLY on areas not covered by the previous rendering,
we use a lower zorder so that we do not paint on the green part'''
x11 = [coord[0]+0.3 for coord in quad1x1]
y11 = [coord[1]+0.3 for coord in quad1x1]
plt.fill(x11, y11, facecolor='red', zorder=4)
xsq = [coord[0]+0.3 for coord in quad_small]
ysq = [coord[1]+0.3 for coord in quad_small]
plt.fill(xsq, ysq, facecolor='white', zorder=4)
xsq = [coord[0]+0.5 for coord in quad_small]
ysq = [coord[1]+0.5 for coord in quad_small]
plt.fill(xsq, ysq, facecolor='white', zorder=4)

'''The hole (white area) of the green square is not painted in red because the zorder of the white area is higher'''

plt.show()

Snapshot of the rendering produced by the code above

最佳答案

看起来甚至不需要剪辑;因为所有表面都是封闭的。

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 6))


xi, yi = np.array([(-0.2, -0.2), (-0.2, 0.2), (0.2, 0.2), (0.2, -0.2), (-0.2, -0.2)]).T
xo, yo = np.array([(-1, 1.), (-1, -1.), (1, -1.), (1., 1.), (-1, 1.)]).T

plt.fill(list(xo)+list(xi), list(yo)+list(yi), facecolor='green', zorder=5)
plt.fill(list(xo+.3)+list(xi+.3), list(yo+.3)+list(yi+.3), facecolor='red', zorder=4)

plt.show()

enter image description here

关于python - Matplotlib masking - 根据像素当前的颜色值重置像素的 zorder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58053011/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com