Python 矩形阵列和matshow的空白空间
我正在尝试使用python和matplotlib创建一个关于我从问卷调查中得到的一些答案的热图 矩阵应为15x30,但结果为30x30矩阵 结果: 我的代码:Python 矩形阵列和matshow的空白空间,python,python-3.x,numpy,matrix,matplotlib,Python,Python 3.x,Numpy,Matrix,Matplotlib,我正在尝试使用python和matplotlib创建一个关于我从问卷调查中得到的一些答案的热图 矩阵应为15x30,但结果为30x30矩阵 结果: 我的代码: 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,
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矩阵
是什么导致我的代码出现此问题?我该如何解决这个问题
一般代码反馈也是受欢迎的问题在于两个for循环,它们建议执行与实际不同的操作 在一个类似法拉利的循环中[自行车,公共汽车,大篷车]:你永远不会把法拉利弄出来,即使你这样称呼它 因此,在两个循环中,您都在矩阵的行上循环,即使您以不同的方式调用循环变量 解决此问题的一个好方法是将输入列表设置为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()
您的解释非常简洁明了,谢谢!