【问题标题】:Empty space with rectangular array and matshow带有矩形阵列和 matshow 的空白空间
【发布时间】:2017-03-25 15:37:56
【问题描述】:

我正在尝试使用 python 和 matplotlib 创建我从问卷中获得的一些答案的热图。

矩阵应该是 15x30,但结果是 30x30 矩阵。

结果:wrong result

我的代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap

m = [[1,2,3,4,5,6,7,8,9,10,9,8,7,6,5],
    [4,5,6,7,8,9,10,9,8,7,8,6,4,1,3],
    [3,2,1,10,9,8,7,6,5,4,9,8,7,6,2],
    [6,7,8,9,10,10,9,8,7,6,7,8,6,1,4],
    [2,1,2,1,2,1,2,1,2,1,7,7,7,7,7],
    [3,2,1,10,9,8,7,6,5,4,8,8,8,8,8],
    [10,9,8,7,6,5,4,3,2,1,9,9,9,9,9],
    [7,3,1,9,4,6,8,2,5,10,4,4,4,5,7],
    [1,1,2,3,5,8,5,3,2,1,1,1,1,4,7],
    [7,8,10,9,8,7,6,5,4,3,9,9,9,9,9],
    [1,2,3,4,5,6,7,8,9,10,9,8,7,6,5],
    [4,5,6,7,8,9,10,9,8,7,8,6,4,1,3],
    [3,2,1,10,9,8,7,6,5,4,9,8,7,6,2],
    [6,7,8,9,10,10,9,8,7,6,7,8,6,1,4],
    [2,1,2,1,2,1,2,1,2,1,7,7,7,7,7],
    [3,2,1,10,9,8,7,6,5,4,8,8,8,8,8],
    [10,9,8,7,6,5,4,3,2,1,9,9,9,9,9],
    [7,3,1,9,4,6,8,2,5,10,4,4,4,5,7],
    [1,1,2,3,5,8,5,3,2,1,1,1,1,4,7],
    [7,8,10,9,8,7,6,5,4,3,9,9,9,9,9],
    [1,2,3,4,5,6,7,8,9,10,9,8,7,6,5],
    [4,5,6,7,8,9,10,9,8,7,8,6,4,1,3],
    [3,2,1,10,9,8,7,6,5,4,9,8,7,6,2],
    [6,7,8,9,10,10,9,8,7,6,7,8,6,1,4],
    [2,1,2,1,2,1,2,1,2,1,7,7,7,7,7],
    [3,2,1,10,9,8,7,6,5,4,8,8,8,8,8],
    [10,9,8,7,6,5,4,3,2,1,9,9,9,9,9],
    [7,3,1,9,4,6,8,2,5,10,4,4,4,5,7],
    [1,1,2,3,5,8,5,3,2,1,1,1,1,4,7],
    [7,8,10,9,8,7,6,5,4,3,9,9,9,9,9]]

person = []
p = 1
for column in m:
    person.append('person' + str(p))
    p += 1
x_pos = np.arange(len(person))

question = []
i = 1
for row in m:
    question.append('question' + str(i))
    i += 1
y_pos = np.arange(len(question))


Cmap =plt.get_cmap('RdYlGn' , np.max(m)-np.min(m)+1)
mat = plt.matshow(m,cmap=Cmap,vmin = np.min(m)-.5, vmax = np.max(m)+.5)
cax = plt.colorbar(mat, ticks=np.arange(np.min(m),np.max(m)+5))

plt.xticks(x_pos, question, rotation=90)
plt.yticks(y_pos, person)

plt.show()

print(person)
print(question)

如果我删除 X_pos 我得到正确的矩阵,但是没有标签。 如果我手动制作 15 个标签,例如

Question = ['Question 1', 'Question 2',...]

然后我只得到 15 个标签,但仍然是 30x30 矩阵。

是什么导致我的代码出现此问题?我该如何解决这个问题?

也欢迎一般代码反馈

【问题讨论】:

    标签: python python-3.x numpy matrix matplotlib


    【解决方案1】:

    问题在于两个 for 循环,它们建议做一些不同于他们实际做的事情。

    在像for Ferrari in ["bike", "bus", "caravan"]: 这样的循环中,即使您这样称呼它,您也永远无法获得法拉利。

    因此,在两个循环中,您都在遍历矩阵的行,即使您以不同的方式调用循环变量。

    解决这个问题的一个好方法是使输入列表成为一个 numpy 数组。然后,您可以遍历其形状以获得刻度标签。

    所以解决方案可能看起来像这样。

    import matplotlib.pyplot as plt
    import numpy as np
    
    m = [[1,2,3,4,5,6,7,8,9,10,9,8,7,6,5],
        [4,5,6,7,8,9,10,9,8,7,8,6,4,1,3],
        [3,2,1,10,9,8,7,6,5,4,9,8,7,6,2],
        [6,7,8,9,10,10,9,8,7,6,7,8,6,1,4],
        [2,1,2,1,2,1,2,1,2,1,7,7,7,7,7],
        [3,2,1,10,9,8,7,6,5,4,8,8,8,8,8],
        [10,9,8,7,6,5,4,3,2,1,9,9,9,9,9],
        [7,3,1,9,4,6,8,2,5,10,4,4,4,5,7],
        [1,1,2,3,5,8,5,3,2,1,1,1,1,4,7],
        [7,8,10,9,8,7,6,5,4,3,9,9,9,9,9],
        [1,2,3,4,5,6,7,8,9,10,9,8,7,6,5],
        [4,5,6,7,8,9,10,9,8,7,8,6,4,1,3],
        [3,2,1,10,9,8,7,6,5,4,9,8,7,6,2],
        [6,7,8,9,10,10,9,8,7,6,7,8,6,1,4],
        [2,1,2,1,2,1,2,1,2,1,7,7,7,7,7],
        [3,2,1,10,9,8,7,6,5,4,8,8,8,8,8],
        [10,9,8,7,6,5,4,3,2,1,9,9,9,9,9],
        [7,3,1,9,4,6,8,2,5,10,4,4,4,5,7],
        [1,1,2,3,5,8,5,3,2,1,1,1,1,4,7],
        [7,8,10,9,8,7,6,5,4,3,9,9,9,9,9],
        [1,2,3,4,5,6,7,8,9,10,9,8,7,6,5],
        [4,5,6,7,8,9,10,9,8,7,8,6,4,1,3],
        [3,2,1,10,9,8,7,6,5,4,9,8,7,6,2],
        [6,7,8,9,10,10,9,8,7,6,7,8,6,1,4],
        [2,1,2,1,2,1,2,1,2,1,7,7,7,7,7],
        [3,2,1,10,9,8,7,6,5,4,8,8,8,8,8],
        [10,9,8,7,6,5,4,3,2,1,9,9,9,9,9],
        [7,3,1,9,4,6,8,2,5,10,4,4,4,5,7],
        [1,1,2,3,5,8,5,3,2,1,1,1,1,4,7],
        [7,8,10,9,8,7,6,5,4,3,9,9,9,9,9]]
    
    #make m a numpy array
    m = np.array(m)
    #assume there are as many persons as rows in the array (=30)
    persons = list(map(lambda x: "person {}".format(x+1), range(m.shape[0])))
    #assume there are as many questions as columns in the array (=15)
    questions = list(map(lambda x: "question {}".format(x+1), range(m.shape[1])))
    
    fig, ax = plt.subplots()
    Cmap =plt.get_cmap('RdYlGn' , np.max(m)-np.min(m)+1)
    mat = ax.matshow(m,cmap=Cmap,vmin = np.min(m)-.5, vmax = np.max(m)+.5)
    cax = fig.colorbar(mat, ticks=np.arange(np.min(m),np.max(m)+5))
    
    plt.xticks(range(len(questions)), questions, rotation=90)
    plt.yticks(range(len(persons)), persons)
    
    plt.tight_layout()
    plt.show()
    

    【讨论】:

    • 你的解释很简洁明了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多