【问题标题】:Extent and aspect; square pixels in an image with shared axis in matplotlib范围和方面; matplotlib中具有共享轴的图像中的正方形像素
【发布时间】:2014-04-29 12:43:57
【问题描述】:

我陷入了一个相当复杂的境地。我正在使用 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

所以我感兴趣的值都在一个三角形中,顶角位于正方形顶边的中间。同时,我绘制了一些数据(在这种情况下是很多彩色线)以及靠近底部的图像。

所以起初这看起来不错,但实际上并非如此:图像中的所有像素都不是正方形的,而是被拉长的,它们的高度大于宽度。如果我放大它们,这就是它们的样子:

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

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

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

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

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

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

更新

按照 cmets 的建议,我尝试使用

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

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

【问题讨论】:

  • 查看stackoverflow.com/questions/14907062/… 问题是由于同时使用 aspect=1 并将其链接到另一个轴,您过度限制了轴限制。
  • @tcaswell,对不起,我不太明白你指的是那个答案的哪一部分。是的,我确实设置了特定方面并将其链接到另一个轴,但这就是我需要做的......
  • 可调节的kwarg。
  • @tcaswell,谢谢!它确实有帮助,但设置adjustable='box-forced' 会导致两个轴相互分离。我会用这个更新问题。

标签: python matplotlib


【解决方案1】:

在找到您问题的解决方案后,重新编辑了我的整个答案。我按照 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 被共享:

希望有帮助!

【讨论】:

  • 感谢您的回复!尽管您的函数运行良好,但当我将两个轴绘制在一起时,它仍然会导致相同的问题:ax1 内有大量空白,或者 ax1 和 axPlotx 被大量空白分隔(如果我设置了可调='bbox-forced' ) - 查看问题中的最后两张图片。
  • 我现在有点困惑。 “ax1 内部有很多空白区域”是什么意思?您的意思是您在问题的第三张图片中看到的内容吗?如果是这种情况,您能否尝试像我一样定义ax1?也就是说,没有host_subplot?如您的代码注释中所示,这不是很重要。在我的上一张图片中,图框周围有大的白色区域。但是,正如您所看到的,图中没有任何大的白色区域。保存图形时,可以使用 savefig 选项删除图形周围的白色区域 bbox_inches = "tight"
  • 是的,我就是这个意思。好的,我将摆脱 host_subplot,希望它能解决问题,谢谢。
  • 您能否在包和绘图方面显示更多代码?我将host_subplotdetermine_aspect 结合使用(请参阅更新的答案)。但是,结果保持不变,我在ax 内没有看到大的白色区域
  • 我认为重要的是我在图像下还有另一个情节 - 你可以尝试在你的例子中添加它吗?我只是使用 plt.show() 并手动保存图片。我真的不知道,这里的包裹有什么帮助......我在那部分没有做任何不寻常的事情。
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2023-02-14
相关资源
最近更新 更多