Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib创建热图_Python_Numpy_Matplotlib - Fatal编程技术网

Python 使用matplotlib创建热图

Python 使用matplotlib创建热图,python,numpy,matplotlib,Python,Numpy,Matplotlib,我有一个138x138的numpy矩阵fooarray。行和列中的每个条目都是一个单词。以下代码用于生成同一矩阵的热图。但我无法展示情节中的所有文字 在色标中显示的值似乎也是错误的。虽然矩阵中的值范围为3.2到-0.2,但热图中显示的值范围为0.1到-0.1。如何使用numpy矩阵绘制热图 fig = plt.figure() ax = fig.add_subplot(111) cax = ax.matshow(fooarray, interpolation='nearest', cmap='

我有一个138x138的numpy矩阵
fooarray
。行和列中的每个条目都是一个单词。以下代码用于生成同一矩阵的热图。但我无法展示情节中的所有文字

在色标中显示的值似乎也是错误的。虽然矩阵中的值范围为3.2到-0.2,但热图中显示的值范围为0.1到-0.1。如何使用numpy矩阵绘制热图

fig = plt.figure()
ax = fig.add_subplot(111)

cax = ax.matshow(fooarray, interpolation='nearest', cmap='hot')
fig.colorbar(cax)

ax.set_xticklabels([' | '] + labels)
ax.set_yticklabels(['|'] + labels)

plt.show() 

我不清楚为什么要在标签前面添加['|']和['|'],因此我将其从代码中删除。色标适合我(见代码),我相信你的数据有问题

下面的代码用
set\xticks
标记位置,用
ax.set\xticklabels
标记标签。我添加了90度旋转,但仍然很难有138个刻度的可读标签

import numpy as np
import matplotlib.pyplot as plt

#create test data:
s=138 #size of array
labels=[str(a) for a in range(s)]
fooarray=np.random.random(s*s).reshape((s,s))

#--- original code here:
fig = plt.figure()
ax = fig.add_subplot(111)

cax = ax.matshow(fooarray, interpolation='nearest', cmap='hot')
fig.colorbar(cax)
#----

#ticks and labels:
ax.set_xticks(range(len(labels)) , minor=False)
ax.set_xticklabels(labels)
ax.set_yticks(range(len(labels)) , minor=False)
ax.set_yticklabels(labels)
plt.xticks(rotation=90)

plt.show()