Python 中的矩形可视化
Visualization of rectangles in Python
所以,我有一个 10x10 的网格。关于如何在其中可视化 2025 个矩形的任何想法?
我的意思是,我想将它们放在一页上(也许是一张图片),但我不知道应该使用哪个库。
我想用 alpha 可视化所有的东西,比如当彼此之间有更多的边界时,我也想可视化它。我的意思是我不知道该怎么做。我不需要给我代码什么的,我只想要一个库名就可以使用。
编辑:
我的矩形在一个列表中定义,其中有 4 个元组,其中一个元组具有角的 x、y 坐标,例如:[(x, y), (x2, y), (x, y2), (x2, y2)]
编辑2:
现在我的代码是:
multipler = 100
canvasH = 10 * multipler # 10 == Y in squares.py
canvasW = 10 * multipler # 10 == X in squares.py
img = numpy.zeros((canvasH, canvasW, 3), numpy.uint8)
# img = cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
rectangles = [[(0, 0), (1, 0), (0, 1), (1, 1)], [(0, 0), (1, 0), (0, 2), (1, 2)], [(0, 0), (1, 0), (0, 3), (1, 3)]...]
for rectangle in rectangles:
cv2.rectangle(img, (rectangle[0][0] * multipler, rectangle[0][1] * multipler), (rectangle[3][0] * multipler, rectangle[3][1] * multipler), (206, 220, 242))
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
但是我只看到9x9
张图片,所以看不到全部(我有2025张)
谢谢,
马尔西
你的问题有很多未知数。这是一个可能的解释。
一些备注:
- 有许多库可以绘制矩形。 Matplotlib 被大量用于可视化,并且具有方便的
Rectangle
功能。
- 颜色有多种表示方式,例如由 0 到 1 之间的 3 个值表示红-绿-蓝(更多信息 here)。
- alpha 可以作为第 4 个元素并入,也可以单独给出。
- 如果只想看到边缘,请将面色设置为'none'。
- 无需缩放值。 matplotlib 等库通过设置 dpi(每英寸点数,标准值:100、300、600)自动执行此操作
- 如果您不想只在屏幕上使用结果,
savefig
可以保存为多种格式。 '.png' 最适合纯图像格式。 '.svg' 对于高质量矢量格式很有趣。
- 最终结果现在看起来像一幅抽象画,但可以作为探索的起点。
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
import random
rectangles = []
for _ in range(2025):
x = random.uniform(0.0, 10.0)
y = random.uniform(0.0, 10.0)
w = random.uniform(0.1, 1.0)
h = random.uniform(0.1, 1.0)
rectangles.append([(x,y), (x+w,y), (x,y+h), (x+w,y+h)])
color = [c/256 for c in (206, 220, 242)] # rgb values between 0 and 1
fig, ax = plt.subplots(figsize=(10,10))
ax.set(xlim=(0, 10), ylim=(0, 10))
ax.axis('off')
for rectangle in rectangles:
xy = rectangle[0]
width = rectangle[3][0] - rectangle[0][0]
height = rectangle[3][1] - rectangle[0][1]
rect = Rectangle(xy, width, height, linewidth=1, edgecolor='none', facecolor=color, alpha=0.3)
ax.add_patch(rect)
plt.savefig("test.png", bbox_inches='tight', dpi=300)
plt.show()
PS1:对于您在关于网格上重叠的许多矩形的评论中的附加问题,您可能会绘制一个图像,该图像由覆盖每个单位正方形的矩形数着色。一些示例代码:
from matplotlib import pyplot as plt
rectangles = [[(0, 0), (1, 0), (0, 1), (1, 1)], [(0, 0), (1, 0), (0, 2), (1, 2)], [(0, 0), (1, 0), (0, 3), (1, 3)],
[(0, 0), (1, 0), (0, 4), (1, 4)], [(0, 0), (1, 0), (0, 5), (1, 5)], [(0, 0), (1, 0), (0, 6), (1, 6)],
[(0, 0), (1, 0), (0, 7), (1, 7)], [(0, 0), (1, 0), (0, 8), (1, 8)], [(0, 0), (1, 0), (0, 9), (1, 9)],
... ]
grid = [[0 for y in range(10)] for x in range(10)]
for r in rectangles:
for x in range(r[0][0], r[3][0] + 1):
for y in range(r[0][1], r[3][1] + 1):
grid [x][y] += 1
plt.imshow(grid, cmap='magma_r')
plt.show()
PS2:对于您的示例,几乎所有矩形都在 10x10 的网格上具有整数坐标,一个想法是仅绘制边缘,使用随机颜色区分并随机移动所有点,因此它们不会不再重叠了。即便如此,仍然有大量几乎重叠的矩形。
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib import cm
import random
rectangles = ...
fig, ax = plt.subplots(figsize=(10,10))
ax.set(xlim=(0, 11), ylim=(0, 11))
ax.axis('off')
cmap = cm.get_cmap('rainbow')
for rectangle in rectangles:
x0 = rectangle[0][0] + random.uniform(-.45, .45)
y0 = rectangle[0][1] + random.uniform(-.45, .45)
x1 = rectangle[3][0] + random.uniform(-.45, .45)
y1 = rectangle[3][1] + random.uniform(-.45, .45)
width = x1-x0
height = y1-y0
rect = Rectangle((x0, y0), width, height, linewidth=1,
facecolor='none', edgecolor=cmap(random.uniform(0,1)), alpha=0.5)
ax.add_patch(rect)
plt.savefig("test.png", bbox_inches='tight', dpi=300)
plt.show()
所以,我有一个 10x10 的网格。关于如何在其中可视化 2025 个矩形的任何想法? 我的意思是,我想将它们放在一页上(也许是一张图片),但我不知道应该使用哪个库。 我想用 alpha 可视化所有的东西,比如当彼此之间有更多的边界时,我也想可视化它。我的意思是我不知道该怎么做。我不需要给我代码什么的,我只想要一个库名就可以使用。
编辑:
我的矩形在一个列表中定义,其中有 4 个元组,其中一个元组具有角的 x、y 坐标,例如:[(x, y), (x2, y), (x, y2), (x2, y2)]
编辑2: 现在我的代码是:
multipler = 100
canvasH = 10 * multipler # 10 == Y in squares.py
canvasW = 10 * multipler # 10 == X in squares.py
img = numpy.zeros((canvasH, canvasW, 3), numpy.uint8)
# img = cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
rectangles = [[(0, 0), (1, 0), (0, 1), (1, 1)], [(0, 0), (1, 0), (0, 2), (1, 2)], [(0, 0), (1, 0), (0, 3), (1, 3)]...]
for rectangle in rectangles:
cv2.rectangle(img, (rectangle[0][0] * multipler, rectangle[0][1] * multipler), (rectangle[3][0] * multipler, rectangle[3][1] * multipler), (206, 220, 242))
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
但是我只看到9x9
张图片,所以看不到全部(我有2025张)
谢谢, 马尔西
你的问题有很多未知数。这是一个可能的解释。
一些备注:
- 有许多库可以绘制矩形。 Matplotlib 被大量用于可视化,并且具有方便的
Rectangle
功能。 - 颜色有多种表示方式,例如由 0 到 1 之间的 3 个值表示红-绿-蓝(更多信息 here)。
- alpha 可以作为第 4 个元素并入,也可以单独给出。
- 如果只想看到边缘,请将面色设置为'none'。
- 无需缩放值。 matplotlib 等库通过设置 dpi(每英寸点数,标准值:100、300、600)自动执行此操作
- 如果您不想只在屏幕上使用结果,
savefig
可以保存为多种格式。 '.png' 最适合纯图像格式。 '.svg' 对于高质量矢量格式很有趣。 - 最终结果现在看起来像一幅抽象画,但可以作为探索的起点。
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
import random
rectangles = []
for _ in range(2025):
x = random.uniform(0.0, 10.0)
y = random.uniform(0.0, 10.0)
w = random.uniform(0.1, 1.0)
h = random.uniform(0.1, 1.0)
rectangles.append([(x,y), (x+w,y), (x,y+h), (x+w,y+h)])
color = [c/256 for c in (206, 220, 242)] # rgb values between 0 and 1
fig, ax = plt.subplots(figsize=(10,10))
ax.set(xlim=(0, 10), ylim=(0, 10))
ax.axis('off')
for rectangle in rectangles:
xy = rectangle[0]
width = rectangle[3][0] - rectangle[0][0]
height = rectangle[3][1] - rectangle[0][1]
rect = Rectangle(xy, width, height, linewidth=1, edgecolor='none', facecolor=color, alpha=0.3)
ax.add_patch(rect)
plt.savefig("test.png", bbox_inches='tight', dpi=300)
plt.show()
PS1:对于您在关于网格上重叠的许多矩形的评论中的附加问题,您可能会绘制一个图像,该图像由覆盖每个单位正方形的矩形数着色。一些示例代码:
from matplotlib import pyplot as plt
rectangles = [[(0, 0), (1, 0), (0, 1), (1, 1)], [(0, 0), (1, 0), (0, 2), (1, 2)], [(0, 0), (1, 0), (0, 3), (1, 3)],
[(0, 0), (1, 0), (0, 4), (1, 4)], [(0, 0), (1, 0), (0, 5), (1, 5)], [(0, 0), (1, 0), (0, 6), (1, 6)],
[(0, 0), (1, 0), (0, 7), (1, 7)], [(0, 0), (1, 0), (0, 8), (1, 8)], [(0, 0), (1, 0), (0, 9), (1, 9)],
... ]
grid = [[0 for y in range(10)] for x in range(10)]
for r in rectangles:
for x in range(r[0][0], r[3][0] + 1):
for y in range(r[0][1], r[3][1] + 1):
grid [x][y] += 1
plt.imshow(grid, cmap='magma_r')
plt.show()
PS2:对于您的示例,几乎所有矩形都在 10x10 的网格上具有整数坐标,一个想法是仅绘制边缘,使用随机颜色区分并随机移动所有点,因此它们不会不再重叠了。即便如此,仍然有大量几乎重叠的矩形。
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib import cm
import random
rectangles = ...
fig, ax = plt.subplots(figsize=(10,10))
ax.set(xlim=(0, 11), ylim=(0, 11))
ax.axis('off')
cmap = cm.get_cmap('rainbow')
for rectangle in rectangles:
x0 = rectangle[0][0] + random.uniform(-.45, .45)
y0 = rectangle[0][1] + random.uniform(-.45, .45)
x1 = rectangle[3][0] + random.uniform(-.45, .45)
y1 = rectangle[3][1] + random.uniform(-.45, .45)
width = x1-x0
height = y1-y0
rect = Rectangle((x0, y0), width, height, linewidth=1,
facecolor='none', edgecolor=cmap(random.uniform(0,1)), alpha=0.5)
ax.add_patch(rect)
plt.savefig("test.png", bbox_inches='tight', dpi=300)
plt.show()