使用Button小部件更新AxesImage实例的转换

时间:2016-11-10 01:11:53

标签: python matplotlib transform

我正在尝试允许用户旋转/反转正在显示的图像。 代码的简化版本:

import numpy as np
from matplotlib.widgets import Button
from matplotlib.transforms import Affine2D
import matplotlib.pyplot as plt

rot90 = np.array([[0,-1,0],[1,0,0],[0,0,1]])
I = np.array([[1,0,0],[0,1,0],[0,0,1]])    
fig, axes = plt.subplots(nrows=1,ncols=1) 
i = axes.imshow(np.random.rand(100).reshape((10,10)))
bax = fig.add_axes([0.025,0.025,0.05,0.05])
tr = Affine2D().set_matrix(I)
i.set_transform(tr)
b = Button(bax,label='rot90')

def on_press(event):
    mat = i.get_transform().get_matrix()
    newmat = np.dot(rot90,mat)
    newtr = Affine2D().set_matrix(newmat)
    i.set_transform(newtr)
    fig.canvas.draw()
b.on_clicked(on_press)
plt.show() 

我知道数据传递给按钮,但按下时没有任何反应,也没有错误。

我认为主要的问题是我真的不知道这些转变是如何运作的。

仅供上下文使用:

我将此用于3D图像查看器,以显示模板Brain nifti图像上的PLS回归的潜在变量。显示3个切片(投影),但有时由于模板加载其中一个切片的方式需要围绕轴旋转或反转。

由于我也使用鼠标交互来移动3D空间,我还需要坐标系旋转和反转。而且我不想翻转或旋转正在显示的实际数组,因为这将需要更多的代码(我认为)。

the crosshairs on each image can be controlled by the mouse, if I just flipped or rotated the array then the mouse coordinates would also need to be flipped and rotated. Since not all images of the brain are gonna be oriented in the same way, I can't make a permanent fix. I'd would rather have an elegant way for the user to rotate and flip the image however they please. In this situation, the left image would ideally be rotated 90 degrees, and the middle image would be reflected across its middle horizontal axis, but the cross hairs and the coordinate system needs to be transformed as well for everything to remain consistent.

0 个答案:

没有答案
相关问题