本文整理汇总了Python中matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg.tostring_argb方法的典型用法代码示例。如果您正苦于以下问题:Python FigureCanvasQTAgg.tostring_argb方法的具体用法?Python FigureCanvasQTAgg.tostring_argb怎么用?Python FigureCanvasQTAgg.tostring_argb使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg
的用法示例。
在下文中一共展示了FigureCanvasQTAgg.tostring_argb方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DrawingPad_Painter
# 需要导入模块: from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg [as 别名]
# 或者: from matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg import tostring_argb [as 别名]
#.........这里部分代码省略.........
self.hbox_layout.addWidget(self.canvas, 5)
self.line_width = 12.0
# self.ctrl_pnl_layout = QVBoxLayout()
# #a button to clear the figure
# self.clean_btn = QPushButton('Clear')
# self.ctrl_pnl_layout.addWidget(self.clean_btn)
#
# self.hbox_layout.addLayout(self.ctrl_pnl_layout, 1)
self.setLayout(self.hbox_layout)
self.drawing = False
self.create_event_handler()
return
def create_event_handler(self):
self.canvas_button_clicked_cid = self.canvas.mpl_connect('button_press_event', self.on_canvas_mouse_clicked)
self.canvas_button_released_cid = self.canvas.mpl_connect('button_release_event', self.on_canvas_mouse_released)
self.canvas_motion_notify_cid = self.canvas.mpl_connect('motion_notify_event', self.on_canvas_mouse_move)
return
def on_canvas_mouse_clicked(self, event):
# print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
# event.button, event.x, event.y, event.xdata, event.ydata)
self.drawing = True
# create a new line if we are drawing within the area
if event.xdata is not None and event.ydata is not None and self.curr_line is None and self.curr_traj is None:
self.curr_line, = self.ax_painter.plot([event.xdata], [event.ydata], '-k', linewidth=self.line_width)
self.curr_traj = [np.array([event.xdata, event.ydata])]
self.canvas.draw()
return
def on_canvas_mouse_released(self, event):
# print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
# event.button, event.x, event.y, event.xdata, event.ydata)
self.drawing = False
# store finished line and trajectory
# print self.curr_traj
self.lines.append(self.curr_line)
self.traj_pnts.append(self.curr_traj)
self.curr_traj = None
self.curr_line = None
return
def on_clean(self, event):
print 'clean the canvas...'
#clear everything
for line in self.ax_painter.lines:
self.ax_painter.lines.remove(line)
self.lines = []
if self.curr_line is not None:
self.ax_painter.lines.remove(self.curr_line)
self.curr_line = None
self.canvas.draw()
self.traj_pnts = []
self.curr_traj = None
self.drawing = False
return
def on_canvas_mouse_move(self, event):
if self.drawing:
# print 'In movement: x=',event.x ,', y=', event.y,', xdata=',event.xdata,', ydata=', event.ydata
if event.xdata is not None and event.ydata is not None and self.curr_line is not None and self.curr_traj is not None:
#append new data and update drawing
self.curr_traj.append(np.array([event.xdata, event.ydata]))
tmp_curr_data = np.array(self.curr_traj)
self.curr_line.set_xdata(tmp_curr_data[:, 0])
self.curr_line.set_ydata(tmp_curr_data[:, 1])
self.canvas.draw()
return
def plot_trajs_helper(self, trajs):
tmp_lines = []
for traj in trajs:
tmp_line, = self.ax_painter.plot(traj[:, 0], traj[:, 1], '-.g', linewidth=self.line_width)
tmp_lines.append(tmp_line)
self.canvas.draw()
#add these tmp_lines to lines record
self.lines = self.lines + tmp_lines
return
def get_traj_data(self):
return self.traj_pnts
def get_image_data(self):
"""
Get the deposited image
"""
w,h = self.canvas.get_width_height()
buf = np.fromstring ( self.canvas.tostring_argb(), dtype=np.uint8 )
buf.shape = ( w, h, 4 )
# canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
buf = np.roll ( buf, 3, axis = 2 )
return buf