Matplotlib 热图真的很小
Matplotlib heatmap is really small
我创建了一个热图,它将显示随机数数据集中所有列之间的相关性。热图创建的很好,但是热图很小,大部分在垂直方向。我在这个 post 上附上了热图的图像。数据集是来自 csv 文件的 pandas 数据框。代码如下:
def colCorrelation():
xData = []
yData = []
fig, ax = plt.subplots(figsize=(5,5))
# calculates the correlation between all columns and all other columns
for i in range(0,100):
for e in range(0,100):
dataFlow = dict(zip([(i,e+1)],
[np.corrcoef(dfT[i],dfT[e+1])[0,1]]))
if list(dataFlow.values())[0] < .9:
xData.append(list(dataFlow.keys())[0][0])
yData.append(list(dataFlow.values())[0])
## tuple of the two columns being correlated and their correlation
## in the dictionary as key value pairs data structure.
## Ex: {(19, 17): -0.015262993060948592}
## Plot heatmap
heatmap, xedges, yedges = np.histogram2d(xData,yData,bins=(50))
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.clf()
plt.title('Random Data heatmap')
plt.ylabel('y')
plt.xlabel('x')
plt.imshow(heatmap,extent=extent)
plt.show()
colCorrelation()
从 plt.imshow
中删除 extent
参数,因为它会根据数据缩小您的绘图。
我创建了一个热图,它将显示随机数数据集中所有列之间的相关性。热图创建的很好,但是热图很小,大部分在垂直方向。我在这个 post 上附上了热图的图像。数据集是来自 csv 文件的 pandas 数据框。代码如下:
def colCorrelation():
xData = []
yData = []
fig, ax = plt.subplots(figsize=(5,5))
# calculates the correlation between all columns and all other columns
for i in range(0,100):
for e in range(0,100):
dataFlow = dict(zip([(i,e+1)],
[np.corrcoef(dfT[i],dfT[e+1])[0,1]]))
if list(dataFlow.values())[0] < .9:
xData.append(list(dataFlow.keys())[0][0])
yData.append(list(dataFlow.values())[0])
## tuple of the two columns being correlated and their correlation
## in the dictionary as key value pairs data structure.
## Ex: {(19, 17): -0.015262993060948592}
## Plot heatmap
heatmap, xedges, yedges = np.histogram2d(xData,yData,bins=(50))
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.clf()
plt.title('Random Data heatmap')
plt.ylabel('y')
plt.xlabel('x')
plt.imshow(heatmap,extent=extent)
plt.show()
colCorrelation()
从 plt.imshow
中删除 extent
参数,因为它会根据数据缩小您的绘图。