如何使用Python的pylab.figure绘制超大热图?
我正在使用 pylab.figure
来绘制热图。这没什么特别的,对于较小的热图尺寸,我得到了不错的结果。
问题:我想绘制一个大约有30行和近2000列的热图。这是可以做到的——但我无法让标签以可读且不重叠的方式显示。
我尝试调整 figsize
和 dpi
,但这样做只是改变了输出图像的分辨率。我没有成功让图像宽到足以让标签不再重叠。
代码片段:
fig = pylab.figure(figsize = (640,30), dpi = 50)
axmatrix = fig.add_axes([ 0.1, 0.1, 0.85, 0.1 ])
axmatrix.tick_params(axis='both', which='major', labelsize = 8)
im = axmatrix.matshow(ordered_observations, aspect = 'auto', origin = 'lower', cmap = pylab.cm.YlGnBu)
axcolor = fig.add_axes([ 0.96, 0.1, 0.01, 0.1 ])
pylab.colorbar(im, cax = axcolor)
axmatrix.set_xticks(range(0, len(right) - 1))
axmatrix.set_yticks(range(0, len(left) - 1))
axmatrix.set_xticklabels(map(lambda name: name, list(right)[0:len(right) - 1]), rotation = 90)
axmatrix.set_yticklabels(map(lambda name: name, list(left)[0:len(left) - 1]))
fig.show()
fig.savefig('linkage.png')
谢谢,
金
编辑:ordered_observations
片段:
[[ 0.5 0.5 0.5 ..., 0.5 0.5 0.5 ]
[ 1. 1. 1. ..., 0.5 0.5 0.5 ]
[ 1. 1. 1. ..., 0.5 0.5 0.5 ]
...,
[ 0.25 0.2 0.2 ..., 0.2 0.2 0.2 ]
[ 0.25 0.2 0.2 ..., 0.2 0.2 0.2 ]
[ 0.25 0.2 0.2 ..., 0.2 0.2 0.2 ]]
这是截断的 pylab 输出。真实数据是一个29 x 1835的矩阵。
编辑 2:与其把我的数据放到某个地方,我觉得通过以下代码片段生成测试数据更好。
import numpy
import random
ordered_observations = numpy.array([ [ random.random() for column in range(0, 1835) ] for row in range(0, 29) ])
编辑 3:一个完整的工作示例。
import numpy
import random
import pylab
left = range(0, 29)
right = range(0, 1835)
ordered_observations = numpy.array([ [ random.random() for column in range(0, 1835) ] for row in range(0, 29) ])
fig = pylab.figure(figsize = (640,30), dpi = 50)
axmatrix = fig.add_axes([ 0.1, 0.1, 0.85, 0.1 ])
axmatrix.tick_params(axis='both', which='major', labelsize = 8)
im = axmatrix.matshow(ordered_observations, aspect = 'auto', origin = 'lower', cmap = pylab.cm.YlGnBu)
axcolor = fig.add_axes([ 0.96, 0.1, 0.01, 0.1 ])
pylab.colorbar(im, cax = axcolor)
axmatrix.set_xticks(range(0, len(right) - 1))
axmatrix.set_yticks(range(0, len(left) - 1))
axmatrix.set_xticklabels(map(lambda name: name, list(right)[0:len(right) - 1]), rotation = 90)
axmatrix.set_yticklabels(map(lambda name: name, list(left)[0:len(left) - 1]))
fig.show()
fig.savefig('linkage.png')
解决方法
看起来这个问题有点棘手,所以我最终决定使用这个解决方法:
- 绘制多个热图,每次处理200列。
1 个回答
0
我觉得你可以用下面的代码来把字体大小调小一些。
ax.tick_params(axis='both', which='major', labelsize=9)
不过,我可能不太明白你的问题。你能把 ordered_observations
的内容保存到一个文件里,然后发给我吗?