matplotlib—学习笔记

概念

matplotlib是受MATLAB的启发构建的。matplotlib有一套完全仿照MATLAB的函数形式的绘图接口,在matplotlib.pyplot模块中。
在这里插入图片描述
在这里插入图片描述

绘图背景设置

在绘图机构中,figure创建窗口,subplot创建子图。所有的绘图只能在子图上进行。plt表示当前子图,若没有就创建一个子图。

  • Figure 代表一个绘制面板,其中可以包含多个Axes(即多个图表)。
  • Axes 表示一个图表,一个Axes包含:titlek,xaxis,yaxis。
  • Axis 坐标轴
    pyplot基本绘图流程主要分为3个部分:
    在这里插入图片描述

设置figure背景图像

import matplotlib.pyplot as plt
import cv2
bg_img = cv2.imread(img_dir,  cv2.IMREAD_UNCHANGED)
fig = plt.figure()
fig.figimage(bg_img)

在这里插入图片描述

设置axes背景颜色

ax = plt.gca()
ax.patch.set_facecolor('none')
plt.imshow(img)
plt.show()

在这里插入图片描述

plot函数

函数

plt.plot(x, y, format_string, **kwargs)

参数

  • x x轴数据
  • y y轴数据
  • format_string 由颜色字符、风格字符和标记字符三部分组成。
    在这里插入图片描述
    在这里插入图片描述
  • **kwargs
    • color 控制颜色 color=‘green’
    • linestyle 线条风格 linestyle=‘dashed’
    • marker 标记风格 marker=‘o’
    • markerfacecolor 标记颜色 markerfacecolor=‘blue’
    • markersize 标记尺寸 markersize=‘20’

遇到的问题

  • 写法不同功能相同
fig, ax = plt.subplots()
ax.plot(250, 260, c='blue', marker='o', markersize=6)
ax.plot(250, 260, 'bo',  markersize=6)
  • 注意差异造成不同的结果
fig, ax = plt.subplots()
ax.plot(250, 260, 'blue', markersize=6)
plt.show()

在这里插入图片描述

fig, ax = plt.subplots()
ax.plot((250, 260), 'blue', markersize=6)
plt.show()

在这里插入图片描述

fig, ax = plt.subplots()
ax.plot(250, 260, 'bo', markersize=6)
plt.show()

在这里插入图片描述

鼠标事件

class event_frame:
    def __init__(self, ax, org_img, cur_img, text_markersize=6):
        #axes
        self.ax_orig = ax[0]
        self.ax_result = ax[1]
        #image
        self.org_img = org_img
        self.tmp_img = org_img
        self.blue_img = np.zeros_like(org_img)
        self.blue_img[:, :, 2] = 255
        self.all_mask = np.where(cur_img[:, :, 3] == 255, 255, 0)
        self.cur_img = cur_img
        #button and textbox
        self.bt_press_flag = 1  # add 1 sum 0
        self.text_markersize = text_markersize
        self.draw_bt_add()
        self.draw_bt_sum()
        self.draw_textbox()
        #mouse
        self.mouse_press = None
        self.cidpress = ax[0].figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.cidrelease = ax[0].figure.canvas.mpl_connect('button_release_event', self.on_release)
        self.cidmotion = ax[0].figure.canvas.mpl_connect('motion_notify_event', self.on_motion)

        #init show
        alpha_mask = np.where(self.all_mask == 255, 0.7, 1)
        alpha = np.expand_dims(alpha_mask, 2)
        self.tmp_img = alpha * self.org_img + (1 - alpha) * self.blue_img
        self.ax_orig.imshow(self.tmp_img.astype(np.int))
        self.ax_result.imshow(self.cur_img)

    #button draw and button event
    def on_bt_add_press(self, event):
        self.bt_press_flag = 1

    def draw_bt_add(self):
        global bt_add
        pt_add = plt.axes([0.5, 0.03, 0.1, 0.05])
        bt_add = Button(pt_add, 'add')
        bt_add.on_clicked(self.on_bt_add_press)

    def on_bt_sum_press(self, event):
        self.bt_press_flag = 0

    def draw_bt_sum(self):
        global bt_sum
        pt_sum = plt.axes([0.8, 0.03, 0.1, 0.05])
        bt_sum = Button(pt_sum, 'sum')
        bt_sum.on_clicked(self.on_bt_sum_press)

    def get_bt_press_flag(self):
        return self.bt_press_flag

    #textbox draw and textbox event
    def draw_textbox(self):
        global text_box
        axbox = plt.axes([0.2, 0.03, 0.1, 0.05])
        text_box = TextBox(axbox, 'brush pixels', initial=str(self.text_markersize))
        text_box.on_submit(self.submit)

    def submit(self, text):
        self.text_markersize= eval(text)

    def get_textbox_value(self):
        return self.text_markersize

    #mouse event
    def on_press(self, event):
        if event.inaxes != self.ax_orig.axes:
            return
        self.mouse_press = 1
        mask = self.create_circular_mask(self.org_img.shape[0], self.org_img.shape[1], [event.xdata, event.ydata],
                                    self.get_textbox_value())
        if self.get_bt_press_flag() == 1:
            self.all_mask = np.where(mask, 255, self.all_mask)
            self.cur_img[:,:,3] = np.where(mask[:,:], 255, self.cur_img[:,:,3])
        if self.get_bt_press_flag() == 0:
            self.all_mask = np.where(mask, 0, self.all_mask)
            self.cur_img[:,:,3] = np.where(mask[:,:], 0, self.cur_img[:,:,3])

        alpha_mask = np.where(self.all_mask == 255, 0.7, 1)
        alpha = np.expand_dims(alpha_mask, 2)
        self.tmp_img = alpha * self.org_img + (1 - alpha) * self.blue_img

        self.ax_orig.cla()
        self.ax_orig.imshow(self.tmp_img.astype(np.int))
        self.ax_orig.figure.canvas.draw()

        self.ax_result.cla()
        self.ax_result.patch.set_facecolor('none')
        self.ax_result.imshow(self.cur_img)
        self.ax_result.figure.canvas.draw()

    def on_motion(self, event):
        if event.inaxes != self.ax_orig.axes:
            return
        if self.mouse_press is None:
            return
        else:
            mask = self.create_circular_mask(self.org_img.shape[0], self.org_img.shape[1], [event.xdata, event.ydata],
                                        self.get_textbox_value())
            if self.get_bt_press_flag() == 1:
                self.all_mask = np.where(mask, 255, self.all_mask)
                self.cur_img[:, :, 3] = np.where(mask[:,:], 255, self.cur_img[:,:,3])

            if self.get_bt_press_flag() == 0:
                self.all_mask = np.where(mask, 0, self.all_mask)
                self.cur_img[:, :, 3] = np.where(mask[:,:], 0, self.cur_img[:,:,3])

            alpha_mask = np.where(self.all_mask == 255, 0.7, 1)
            alpha = np.expand_dims(alpha_mask, 2)
            self.tmp_img = alpha * self.org_img + (1 - alpha) * self.blue_img

            self.ax_orig.cla()
            self.ax_orig.imshow(self.tmp_img.astype(np.int))
            self.ax_orig.figure.canvas.draw()

            self.ax_result.cla()
            self.ax_result.patch.set_facecolor('none')
            self.ax_result.imshow(self.cur_img)
            self.ax_result.figure.canvas.draw()

    def on_release(self, event):
        if event.inaxes != self.ax_orig.axes:
            return
        self.mouse_press = None

    def create_circular_mask(self, h, w, center=None, radius=None):

        if center is None:  # use the middle of the image
            center = [int(w / 2), int(h / 2)]
        if radius is None:  # use the smallest distance between the center and image walls
            radius = min(center[0], center[1], w - center[0], h - center[1])

        Y, X = np.ogrid[:h, :w]
        dist_from_center = np.sqrt((X - center[0]) ** 2 + (Y - center[1]) ** 2)

        mask = dist_from_center <= radius
        return mask

    def get_img(self):
        return self.cur_img

    

参考资料

python数据可视化系列教程——matplotlib绘图全解
Python matplotlib高级绘图详解
matplotlib命令与格式:设置栅格,axes脊柱(坐标轴),背景颜色
Matplotlib 学习笔记
Python的知识点 plt.plot()函数细节
Matplotlib系列—pyplot的plot( )函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值