【发布时间】:2014-08-14 18:27:37
【问题描述】:
我正在使用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_observationssn-p:
[[ 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:与其将我的数据存放在任何地方,不如通过以下代码sn-p生成测试数据。
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 列的跨度绘制多个热图
【问题讨论】:
-
你能提供一些示例数据吗?
-
“左”和“右”的数据如何?养成发布可运行代码的习惯是件好事。
-
现在提供数据和可运行代码。标签在
fig.show()和生成的 PNG 中都是不可读的。
标签: python matplotlib