我正在尝试使用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矩阵。
我的代码中导致此问题的原因是什么?我该如何解决这个问题?
也欢迎一般代码反馈
答案 0 :(得分:1)
问题在于两个for循环,它们建议做一些与实际不同的东西。
在像for Ferrari in ["bike", "bus", "caravan"]:
这样的循环中,你永远不会把法拉利拿出来,即使你这样称呼它。
因此,在两个循环中,即使以不同方式调用循环变量,也会循环遍历矩阵的行。
解决问题的一个好方法是使输入列表成为一个numpy数组。然后,您可以迭代其形状以获取ticklabels。
所以解决方案看起来像这样。
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()