【发布时间】: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