范围和方面;具有共享轴的图像中的方形像素

问题描述 投票:0回答:1

我陷入了一个相当复杂的境地。我正在使用 imshow() 将一些数据绘制为图像。不幸的是,我的脚本很长而且有点混乱,因此很难制作一个有效的示例,但我展示了关键步骤。这就是我从写入文件的更大数组中获取图像数据的方法:

data = np.tril(np.loadtxt('IC-heatmap-20K.mtx'), 1)
#
#Here goes lot's of other stuff, where I define start and end
#
chrdata = data[start:end, start:end]
chrdata = ndimage.rotate(chrdata, 45, order=0, reshape=True, 
                         prefilter=False, cval=0)
ax1 = host_subplot(111) 
#I don't really need host_subplot() in this case, I could use something more common;
#It is just divider.append_axes("bottom", ...) is really convenient.
plt.imshow(chrdata, origin='lower', interpolation='none',
           extent=[0, length*resolution, 0, length*resolution]) #resolution=20000

所以我感兴趣的值都在一个三角形中,顶角位于正方形顶边的中间。同时,我绘制一些数据(在本例中为大量彩色线)以及底部附近的图像。 with extent without aspect

乍一看这看起来不错,但实际上并非如此:图像中的所有像素都不是正方形的,而是拉长的,它们的高度大于宽度。如果我放大的话,它们看起来是这样的: pixels with extent without aspect

如果我在调用 imshow() 时没有设置范围,则不会发生这种情况,但我需要它,以便图像和其他图中的坐标(在本例中底部的彩色线)相同(请参阅Converting matplotlib 中图片的坐标?)。 我尝试使用方面来修复它。我尝试这样做并修复了像素的形状,但我得到了一张非常奇怪的图片: extent and aspect

问题是,稍后在代码中我明确设置了这一点:

ax1.set_ylim(0*resolution, length*resolution) #resolution=20000

但是在设置方面之后,我得到了完全不同的 y 限制。最糟糕的是: ax1 现在比底部另一个图的轴更宽,因此它们的坐标不再匹配!我是这样添加的:

axPlotx = divider.append_axes("bottom", size=0.1, pad=0, sharex=ax1)

我非常感谢您帮助修复它:正方形像素,两个(或更多,在其他情况下)图中的相同坐标。正如我所看到的,图像的轴需要变得更宽(正如纵横比一样),ylims 应该适用,并且第二个轴的宽度应该与图像的宽度相同。 感谢您阅读这个可能不清楚的解释,如果我需要澄清任何事情,请告诉我。

更新

按照评论中的建议,我尝试使用

ax1.set(adjustable='box-forced')

它确实对图像本身有帮助,但它导致两个轴被空白分开。有什么办法可以让他们彼此靠近吗?

extent, aspect and box-forced

python matplotlib imshow
1个回答
2
投票

重新编辑了我的整个答案,因为我找到了您问题的解决方案。我按照 tcaswell 评论的建议使用

set_adjustable("box_forced")
选项解决了这个问题。

import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot, make_axes_locatable


#Calculate aspect ratio
def determine_aspect(shape, extent):
    dx = (extent[1] - extent[0]) / float(shape[1])
    dy = (extent[3] - extent[2]) / float(shape[0])
    return dx / dy

data = numpy.random.random((30,60))

shape = data.shape
extent = [-10, 10, -20, 20]
x_size, y_size = 6, 6

fig = plt.figure(figsize = (x_size, y_size))
ax = host_subplot(1, 1, 1)
ax.imshow(data, extent = extent, interpolation = "None", aspect = determine_aspect(shape, extent))

#Determine width and height of the subplot frame
bbox = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
width, height = bbox.width, bbox.height

#Calculate distance, the second plot needs to be elevated by
padding = (y_size - (height - width)) / float(1 / (2. * determine_aspect(shape, extent)))

#Create second image in subplot with shared x-axis
divider = make_axes_locatable(ax)
axPlotx = divider.append_axes("bottom", size = 0.1, pad = -padding, sharex = ax)

#Turn off yticks for axPlotx and xticks for ax 
axPlotx.set_yticks([])
plt.setp(ax.get_xticklabels(), visible=False)

#Make the plot obey the frame
ax.set_adjustable("box-forced")

fig.savefig("test.png", dpi=300, bbox_inches = "tight")

plt.show()

这会产生以下图像,其中共享

x-axis

enter image description here

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.