填充矩阵以使用 plt.imshow() 绘制它

问题描述 投票:0回答:1

我有以下房子的平面图,我想用Python将其表示为矩阵:

我想到了具有这样的值的矩阵:

空白空间 - 0,房间 - 1,墙壁 - 2

然后我想用plot.imshow()来绘制它。

我已经得到了这个代码:

import numpy as np
import matplotlib.pyplot as plt

class Dom:
    def __init__(self, params):
        self.params = params
        matrices = {}
        self.matrices = matrices
        for key in params["indexes"].keys():
            row_min = min([tup[0] for tup in params["indexes"][key]])
            row_max = max([tup[0] for tup in params["indexes"][key]])
            col_min = min([tup[1] for tup in params["indexes"][key]])
            col_max = max([tup[1] for tup in params["indexes"][key]])
            self.matrices[key] = np.zeros((row_max - row_min, col_max - col_min))
            mask = params["mask"][key]
            self.matrices[key][mask == 1] = 1
            mask = params["mask"][key]
            self.matrices[key][mask == 1] = 1

        self.bigmatrix = np.zeros((100, 100))
        for key in params["indexes"].keys():
            row_min = min([tup[0] for tup in params["indexes"][key]])
            row_max = max([tup[0] for tup in params["indexes"][key]])
            col_min = min([tup[1] for tup in params["indexes"][key]])
            col_max = max([tup[1] for tup in params["indexes"][key]])

            self.bigmatrix[row_min:row_max, col_min:col_max] = self.matrices[key]


    def build(self):
        return self.matrices.values()

    def build2(self):
        return self.bigmatrix


if __name__ == '__main__':
    params = {"indexes": {"I": [(0, 0), (0, 50), (50, 0), (50, 50)],
                          "II": [(0, 50), (0, 100), (50,50), (50, 100)],
                          "III": [(50, 0), (50, 100), (70, 0), (70, 100)],
                          "IV": [(70, 0), (70,50), (100,0), (100,50)],
                          "V": [(70,50), (70,100), (100,50), (100, 100)]},
              "rozdzielczosc": {},
              "mask": {"I": 1, "II": 0, "III": 1, "IV": 1, "V": 1},
              "wall":{"I":[(0,49), (0,50), (50,49), (50,50)]}}

    dom = Dom(params)
    print(dom.build2())
    plt.imshow(dom.build2())
    plt.show()

这给了我以下(良好)结果:

但是我想向房子添加墙壁,一个宽度单位(因此在矩阵的特定坐标中插入值 2),但我不知道如何做到这一点。我尝试过类似的事情:

        for key in params["wall"].keys():
            w_row_min = min([tup[0] for tup in params["wall"][key]])
            w_row_max = max([tup[0] for tup in params["wall"][key]])
            w_col_min = min([tup[1] for tup in params["wall"][key]])
            w_col_max = max([tup[1] for tup in params["wall"][key]])
            self.matrices[key][w_row_max - w_row_min, w_col_max - w_col_min] = 2

#I added this example line to params dict: wall":{"I":[(0,49), (0,50), (50,49), (50,50)]}}

但它根本不起作用,我不知道如何以其他方式做到这一点。 如果有任何帮助,我将不胜感激。

python matplotlib
1个回答
0
投票

我认为使用 matplotlib 的

plot
(文档这里,用于创建墙壁)和
Rectangle
(文档这里,用于创建房间)可能会更好,你正在写的看起来像是你在尝试使用 imshow 重新实现其中一些功能。

© www.soinside.com 2019 - 2024. All rights reserved.