Python交互式选择工具,如MATLAB中的
我试图从MATLAB切换到python,但现在我有一些我自己无法解决的问题。我在pyqt中使用Qt designer设计了一个GUI(用于分析某些神经元),所有可视化都在Qt的matplotlib小部件中完成(它包含在pythonxy中),但现在我需要一些工具,如MATLAB中的工具,用于交互选择(不仅在图像上,而且在绘图上),这些工具与集成在Qt GUI中的matplotlib一起工作:Python交互式选择工具,如MATLAB中的,python,qt,matlab,matplotlib,pyqt,Python,Qt,Matlab,Matplotlib,Pyqt,我试图从MATLAB切换到python,但现在我有一些我自己无法解决的问题。我在pyqt中使用Qt designer设计了一个GUI(用于分析某些神经元),所有可视化都在Qt的matplotlib小部件中完成(它包含在pythonxy中),但现在我需要一些工具,如MATLAB中的工具,用于交互选择(不仅在图像上,而且在绘图上),这些工具与集成在Qt GUI中的matplotlib一起工作: imline 无礼地 伊梅利斯 徒手 imrect(在pyqt GUI中不起作用) ginput(在注释
- imline
- 无礼地
- 伊梅利斯
- 徒手
- imrect(在pyqt GUI中不起作用)李>
- ginput(在注释matplotlib\blocking_input.py文件中的命令self.fig.show()后,我可以直接在myMatplotlibWidget.figure.ginput()上调用ginput)
感谢您提供有关交互式工具的帮助,您可能想看看ipython笔记本或其他ipython应用程序 ipython qt控制台: ipython笔记本电脑:
好的,我自己实现了集成在Qt GUI中的matplotlib imline。。。现在,imrect等都很容易实现。如果有人需要imrect等等,我会更新代码。下面是我的imline代码:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import time
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt
class Imline(QObject):
'''
Plot interactive line
'''
def __init__(self, plt, image = None, scale = 1, *args, **kwargs):
'''
Initialize imline
'''
super(Imline, self).__init__(None)
# set plot
self.__plt = plt
self.scale = scale
# initialize start and end points
self.startX = None
self.startY = None
self.endX = None
self.endY = None
# initialize line2d
self.__line2d = None
self.mask = None
# store information to generate mask
if(image is not None):
height, width = image.shape
else:
height = None
width = None
self.__width = width
self.__height = height
# set signals and slots
self.__c1 = self.__plt.figure.canvas.mpl_connect('button_press_event', self.__mousePressEvent)
self.__c2 = self.__plt.figure.canvas.mpl_connect('motion_notify_event', self.__mouseMoveEvent)
self.__c3 = self.__plt.figure.canvas.mpl_connect('button_release_event', self.__mouseReleaseEvent)
self.imlineEventFinished = SIGNAL('imlineEventFinished')
def __mousePressEvent(self, event):
'''
Starting point
'''
# get xy data
xdata = event.xdata
ydata = event.ydata
# check if mouse is outside the figure
if((xdata is None) | (ydata is None) | (self.startX is not None) | (self.startY is not None) | (self.endX is not None) | (self.endY is not None)):
return
# start point
self.startX = xdata
self.startY = ydata
def __mouseMoveEvent(self, event):
'''
Draw interactive line
'''
# get xy data
xdata = event.xdata
ydata = event.ydata
# check if mouse is outside the figure
if((xdata is None) | (ydata is None) | (self.startX is None) | (self.startY is None) | (self.endX is not None) | (self.endY is not None)):
return
# remove line
if(self.__line2d is not None):
self.__line2d[0].remove()
# set x, t
x = [self.startX, xdata]
y = [self.startY, ydata]
# plot line
self.__plt.axes.hold(True)
xlim = self.__plt.axes.get_xlim()
ylim = self.__plt.axes.get_ylim()
self.__line2d = self.__plt.axes.plot(x, y, color = [1, 0, 0])
self.__plt.axes.set_xlim(xlim)
self.__plt.axes.set_ylim(ylim)
# update plot
self.__plt.draw()
self.__plt.show()
def __mouseReleaseEvent(self, event):
'''
End point
'''
# get xy data
xdata = event.xdata
ydata = event.ydata
# check if mouse is outside the figure
if((xdata is None) | (ydata is None) | (self.endX is not None) | (self.endY is not None)):
return
# remove line
if(self.__line2d is not None):
self.__line2d[0].remove()
self.endX = xdata
self.endY = ydata
P = np.polyfit([self.startX, self.endX], [self.startY, self.endY],1 )
self.__m = P[0]
self.__q = P[1]
# update plot
self.__plt.draw()
self.__plt.show()
# disconnect the vents
self.__plt.figure.canvas.mpl_disconnect(self.__c1)
self.__plt.figure.canvas.mpl_disconnect(self.__c2)
self.__plt.figure.canvas.mpl_disconnect(self.__c3)
# emit SIGNAL
self.emit(SIGNAL('imlineEventFinished'))
def createMask(self):
'''
Create mask from painted line
'''
# check height width
if((self.__height is None) | (self.__width is None)):
return None
# initialize mask
mask = np.zeros((self.__height, self.__width))
# get m q
m = self.__m
q = self.__q
print m, q
# get points
startX = np.int(self.startX)
startY = np.int(self.startY)
endX = np.int(self.endX)
endY = np.int(self.endY)
# ensure startX < endX
tempStartX = startX
if(startX > endX):
startX = endX
endX = tempStartX
# ensure startY < endY
tempStartY = startY
if(startY > endY):
startY = endY
endY = tempStartY
# save points
self.startX = startX
self.endX = endX
self.startY = startY
self.endY = endY
# intialize data
xData = np.arange(startX, endX)
yData = np.arange(startY, endY)
# scan on x
for x in xData:
row = round(m*x + q)
if(row < startY):
row = startY
if(row > endY):
row = endY
mask[row, x] = 1
# scan on y
for y in yData:
col = round((y - q) / m)
if(col < startX):
col = startX
if(col > endX):
col = endX
mask[y, col] = 1
# get boolean mask
mask = mask == 1
# return boolean mask
return mask
从PyQt4.QtCore导入*
从PyQt4.QtGui导入*
导入时间
将matplotlib导入为mpl
将matplotlib.pyplot作为plt导入
将numpy作为np导入
导入scipy.optimize作为选项
类Imline(QObject):
'''
绘制交互线
'''
定义初始值(self,plt,image=None,scale=1,*args,**kwargs):
'''
初始化imline
'''
超级(Imline,self)。\uuuu init\uuuu(无)
#设定图
自紧力=自紧力
self.scale=scale
#初始化起点和终点
self.startX=无
self.startY=无
self.endX=无
self.endY=None
#初始化line2d
self.\uuu line2d=无
self.mask=None
#存储信息以生成掩码
如果(图像不是无):
高度、宽度=image.shape
其他:
高度=无
宽度=无
自身宽度=宽度
自身高度=高度
#设置信号和插槽
self.\uuuc1=self.\uu plt.figure.canvas.mpl\u connect('按钮按下事件',self.\uuuu鼠标按下事件)
self.\uuuc2=self.\uuu plt.figure.canvas.mpl\u connect('motion\u notify\u event',self.\uu mouseMoveEvent)
self.\uuuc3=self.\uuuplt.figure.canvas.mpl\u connect('button\u release\u event',self.\uuuuu mouseRelease event)
self.imlineEventFinished=信号('imlineEventFinished')
def__鼠标压力事件(自身、事件):
'''
起点
'''
#获取xy数据
扩展数据=event.xdata
ydata=event.ydata
#检查鼠标是否在图外
如果((扩展数据为无)|(ydata为无)|(self.startX不是无)|(self.startY不是无)|(self.endX不是无)|(self.endY不是无)):
返回
#起点
self.startX=扩展数据
self.startY=ydata
def__mouseMoveEvent(self,event):
'''
绘制交互线
'''
#获取xy数据
扩展数据=event.xdata
ydata=event.ydata
#检查鼠标是否在图外
如果((扩展数据为无)|(ydata为无)|(self.startX为无)|(self.startY为无)|(self.endX不是无)|(self.endY不是无)):
返回
#拆线
如果(self.\u line2d不是无):
self.\uu line2d[0]。删除()
#集合x,t
x=[self.startX,扩展数据]
y=[self.startY,ydata]
#绘图线
自校准轴保持(真)
xlim=self.\u plt.axes.get\u xlim()
ylim=self.\u plt.axes.get\u ylim()
self.\uuuu line2d=self.\uuuuu plt.axes.plot(x,y,颜色=[1,0,0])
自校准轴设置校准(xlim)
自校准轴设置ylim(ylim)
#更新图
自绘制
self.\uu plt.show()
def _mouseReleaseEvent(自身,事件):
'''
终点
'''
#获取xy数据
扩展数据=event.xdata
ydata=event.ydata
#检查鼠标是否在图外
如果((扩展数据为无)|(ydata为无)|(self.endX不是无)|(self.endY不是无)):
返回
#拆线
如果(self.\u line2d不是无):
self.\uu line2d[0]。删除()
self.endX=扩展数据
self.endY=ydata
P=np.polyfit([self.startX,self.endX],[self.startY,self.endY],1)
self.\uu m=P[0]
self.\uuq=P[1]
#更新图
自绘制
self.\uu plt.show()
#断开通风口
self.\u plt.figure.canvas.mpl\u断开连接(self.\u c1)
self.\u plt.figure.canvas.mpl\u断开连接(self.\u c2)
self.\u plt.figure.canvas.mpl\u断开连接(self.\u c3)
#发射信号
self.emit(信号('imlineEventFinished'))
def createMask(自):
'''
从绘制的线创建遮罩
'''
#检查高度和宽度
如果((自身高度为无);(自身宽度为无)):
一无所获
#初始化掩码
掩码=np.零((自高度、自宽度))
#得到mq
m=自我
from matplotlib import pyplot as plt
class LineBuilder:
def __init__(self, line):
self.line = line
self.xs = list(line.get_xdata())
self.ys = list(line.get_ydata())
self.cid = line.figure.canvas.mpl_connect('button_press_event', self)
def __call__(self, event):
print('click', event)
if event.inaxes!=self.line.axes: return
self.xs.append(event.xdata)
self.ys.append(event.ydata)
self.line.set_data(self.xs, self.ys)
self.line.figure.canvas.draw()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0]) # empty line
linebuilder = LineBuilder(line)
plt.show()