Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 使用imshow设置图像数据形状更改时的正确限制_Image_Matplotlib_Rotation_Reshape - Fatal编程技术网

Image 使用imshow设置图像数据形状更改时的正确限制

Image 使用imshow设置图像数据形状更改时的正确限制,image,matplotlib,rotation,reshape,Image,Matplotlib,Rotation,Reshape,我有一个3D数组,前两个维度是空间的,比如(x,y)。第三个维度包含特定于点的信息 print H.shape # --> (200, 480, 640) spatial extents (200,480) 现在,通过选择三维中的某个平面,我可以用 imdat = H[:,:,100] # shape (200, 480) img = ax.imshow(imdat, cmap='jet',vmin=imdat.min(),vmax=imdat.max(), animated=

我有一个3D数组,前两个维度是空间的,比如(x,y)。第三个维度包含特定于点的信息

print H.shape  # --> (200, 480, 640)  spatial extents (200,480)
现在,通过选择三维中的某个平面,我可以用

imdat = H[:,:,100]    # shape (200, 480)
img = ax.imshow(imdat, cmap='jet',vmin=imdat.min(),vmax=imdat.max(), animated=True, aspect='equal')
我现在要旋转立方体,以便从(x,y)切换到(y,x)

现在,当我打电话时:

imdat = H[:,:,100]   # shape (480,200)
img.set_data(imdat)
ax.relim()
ax.autoscale_view(tight=True)
我有奇怪的行为。沿行的图像显示数据,直到第200行,然后显示黑色,直到y轴(480)结束。x轴从0延伸到200,并显示旋转后的数据。现在,再旋转90度,图像显示正确(当然只是旋转180度)

在我看来,在旋转数据之后,轴限制(或图像范围?)或某些东西没有正确刷新。有人能帮忙吗


PS:为了沉迷于糟糕的黑客行为,我还尝试在每次旋转后重新生成一个新图像(通过调用ax.imshow),但我仍然得到相同的行为。

下面我为您的问题提供了一个解决方案。方法
resetExtent
使用数据和图像将范围显式设置为所需值。希望我正确地模拟了预期的结果

import matplotlib.pyplot as plt
import numpy as np

def resetExtent(data,im):
    """
    Using the data and axes from an AxesImage, im, force the extent and 
    axis values to match shape of data.
    """
    ax = im.get_axes()
    dataShape = data.shape

    if im.origin == 'upper':
        im.set_extent((-0.5,dataShape[0]-.5,dataShape[1]-.5,-.5))
        ax.set_xlim((-0.5,dataShape[0]-.5))
        ax.set_ylim((dataShape[1]-.5,-.5))
    else:
        im.set_extent((-0.5,dataShape[0]-.5,-.5,dataShape[1]-.5))
        ax.set_xlim((-0.5,dataShape[0]-.5))
        ax.set_ylim((-.5,dataShape[1]-.5))

def main():
    fig = plt.gcf()
    ax = fig.gca()

    H = np.zeros((200,480,10))
    # make distinguishing corner of data
    H[100:,...] = 1
    H[100:,240:,:] = 2

    imdat = H[:,:,5]
    datShape = imdat.shape

    im = ax.imshow(imdat,cmap='jet',vmin=imdat.min(),
                    vmax=imdat.max(),animated=True,
                    aspect='equal',
                    #                origin='lower'
                    )

    resetExtent(imdat,im)

    fig.savefig("img1.png")

    H = np.rot90(H)

    imdat = H[:,:,0]
    im.set_data(imdat)
    resetExtent(imdat,im)

    fig.savefig("img2.png")

if __name__ == '__main__':
  main()
此脚本生成两个图像: 第一次未轮调: 然后旋转:


我认为只要显式调用
set\u extent
就可以完成
resetExtent
所做的一切,因为如果“autoscle”为真,它应该调整轴限制。但是由于一些未知的原因,单独调用
set\u extent
并不能完成这项工作

你能提供一些背景吗?因为我只建议在一个单独的图形中绘制一个新图像。如果这是不可取的,请说明原因。我很困惑。我假设您需要在每次调用后显式地使用
set\u extent
,这也应该调用
set\u xlim
set\u ylim
,确实如此,但我这样做时似乎无法获得所需的行为。感谢您的回复。这确实说明了我面临的问题。我在清理后通过重新填充轴来解决这个问题,但这似乎是一个不太重要的解决方案。
import matplotlib.pyplot as plt
import numpy as np

def resetExtent(data,im):
    """
    Using the data and axes from an AxesImage, im, force the extent and 
    axis values to match shape of data.
    """
    ax = im.get_axes()
    dataShape = data.shape

    if im.origin == 'upper':
        im.set_extent((-0.5,dataShape[0]-.5,dataShape[1]-.5,-.5))
        ax.set_xlim((-0.5,dataShape[0]-.5))
        ax.set_ylim((dataShape[1]-.5,-.5))
    else:
        im.set_extent((-0.5,dataShape[0]-.5,-.5,dataShape[1]-.5))
        ax.set_xlim((-0.5,dataShape[0]-.5))
        ax.set_ylim((-.5,dataShape[1]-.5))

def main():
    fig = plt.gcf()
    ax = fig.gca()

    H = np.zeros((200,480,10))
    # make distinguishing corner of data
    H[100:,...] = 1
    H[100:,240:,:] = 2

    imdat = H[:,:,5]
    datShape = imdat.shape

    im = ax.imshow(imdat,cmap='jet',vmin=imdat.min(),
                    vmax=imdat.max(),animated=True,
                    aspect='equal',
                    #                origin='lower'
                    )

    resetExtent(imdat,im)

    fig.savefig("img1.png")

    H = np.rot90(H)

    imdat = H[:,:,0]
    im.set_data(imdat)
    resetExtent(imdat,im)

    fig.savefig("img2.png")

if __name__ == '__main__':
  main()