【问题标题】:How do I plot a really large heatmap using Python's pylab.figure?如何使用 Python 的 pylab.figure 绘制一个非常大的热图?
【发布时间】:2014-08-14 18:27:37
【问题描述】:

我正在使用pylab.figure 绘制热图。这没有什么不寻常的,我在较小的热图尺寸上得到了很好的结果。

问题:我想绘制一个大约 30 行和近 2000 列的热图。这行得通——除了我无法让标签以可读的非重叠方式呈现。

我尝试使用figsizedpi,但它所做的只是重新调整输出图像的最终分辨率。我实际上未能使图像足够宽,以使标签不再重叠。

代码摘录:

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


【解决方案1】:

我想你可以使用下面的代码来设置字体更小

ax.tick_params(axis='both', which='major', labelsize=9)

但是,我可能无法完全理解您的问题。你能把ordered_observations的内容保存在一个文件中并附上吗?

【讨论】:

  • 我不知道如何在此处附加文件,但我编辑了问题以显示我的数据的截断版本。
  • 您可以将文件转储到网站filedropper.com 并链接到它。或者,发布热图的图片。
  • 我添加了一个代码片段,用于生成可用于ordered_observations的测试数据。
猜你喜欢
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2013-09-18
  • 2012-07-21
  • 1970-01-01
相关资源
最近更新 更多