当前位置: 首页>>代码示例>>Python>>正文


Python FigureCanvasQTAgg.tostring_argb方法代码示例

本文整理汇总了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
开发者ID:navigator8972,项目名称:vae_assoc,代码行数:104,代码来源:drawing_pad.py


注:本文中的matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg.tostring_argb方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。