zoukankan      html  css  js  c++  java
  • matplotlib按钮控制图像显示

    需求

    写一个GUI勾选不同的复选框展示不同的图形叠加效果

    实现

    调用matplotlib内置widgets实现对象visibility控制

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import CheckButtons
    
    t = np.arange(0.0, 2.0, 0.01)
    s0 = np.sin(2*np.pi*t)
    s1 = np.sin(4*np.pi*t)
    s2 = np.sin(6*np.pi*t)
    
    fig, ax = plt.subplots()
    l0, = ax.plot(t, s0, visible=False, lw=2, color='k', label='2 Hz')
    l1, = ax.plot(t, s1, lw=2, color='r', label='4 Hz')
    l2, = ax.plot(t, s2, lw=2, color='g', label='6 Hz')
    plt.subplots_adjust(left=0.2)
    
    lines = [l0, l1, l2]
    
    # Make checkbuttons with all plotted lines with correct visibility
    rax = plt.axes([0.05, 0.4, 0.1, 0.15])
    labels = [str(line.get_label()) for line in lines]
    visibility = [line.get_visible() for line in lines]
    check = CheckButtons(rax, labels, visibility)
    
    
    def func(label):
        index = labels.index(label)
        lines[index].set_visible(not lines[index].get_visible())
        plt.draw()
    
    check.on_clicked(func)
    
    plt.show()
    

    效果图

    参考

    Check Buttons — Matplotlib 3.3.1 documentation

  • 相关阅读:
    “数学题”——传钱
    kafka笔记——入门介绍
    SpringBoot集成Dubbo+Zookeeper
    MySql基本语法
    动态规划
    总结
    Java反射
    软件清单
    Java IO操作
    Spring Boot AOP的使用
  • 原文地址:https://www.cnblogs.com/azureology/p/13667048.html
Copyright © 2011-2022 走看看