matplotlib绘制交互显示按钮

首先使用相应的matplotlib来操作,达到的效果是按钮可以控制是否显示某条线。

绘制bar图形的

在这里插入图片描述
请添加图片描述

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.bar(np.arange(len(t)), height=s0, width=2, color='black', label='1 Hz')
l1 = ax.bar(np.arange(len(t)), height=s1, width=2, color='red', label='2 Hz')
l2 = ax.bar(np.arange(len(t)), height=s2, width=2, color='green', label='3 Hz')

fig.subplots_adjust(left=0.2)

lines_by_label = {l.get_label(): l for l in [l0, l1, l2]}
line_colors = ['black', 'red', 'green']

rax = fig.add_axes([0.05, 0.4, 0.1, 0.15])

ret = [True, True, True]
col = [l0, l1, l2] #, l1, l2]
for i in range(len(col)):
    for j in col[i]:
        ret[i] = ret[i] & j.get_visible()

check = CheckButtons(
    ax=rax,
    labels = lines_by_label.keys(),
    actives = ret,
    label_props={'color': line_colors},
    frame_props={'edgecolor': line_colors},
    check_props={'facecolor': line_colors},
)

def callback(label):
    ln = lines_by_label[label]
    for j in ln:
        j.set_visible(not j.get_visible())
        j.figure.canvas.draw_idle()

check.on_clicked(callback)
plt.show()

绘制直方图的

在这里插入图片描述

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.hist(s0, bins=10, color='black', label='1 Hz')
_, __, l1 = ax.hist(s1, bins=10, color='red', label='2 Hz')
_, __, l2 = ax.hist(s2, bins=30, color='green', label='3 Hz')

fig.subplots_adjust(left=0.2)

lines_by_label = {l.get_label(): l for l in [l0, l1, l2]}
line_colors = ['black', 'red', 'green']

rax = fig.add_axes([0.05, 0.4, 0.1, 0.15])

ret = [True, True, True]
col = [l0, l1, l2] #, l1, l2]
for i in range(len(col)):
    for j in col[i]:
        ret[i] = ret[i] & j.get_visible()

check = CheckButtons(
    ax=rax,
    labels = lines_by_label.keys(),
    actives = ret,
    label_props={'color': line_colors},
    frame_props={'edgecolor': line_colors},
    check_props={'facecolor': line_colors},
)

def callback(label):
    ln = lines_by_label[label]
    for j in ln:
        j.set_visible(not j.get_visible())
        j.figure.canvas.draw_idle()

check.on_clicked(callback)
plt.show()

绘制散点图的

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.scatter(t, s0, visible=False, lw=2, color='black', label='1 Hz')
l1 = ax.scatter(t, s1, lw=2, color='red', label='2 Hz')
l2 = ax.scatter(t, s2, lw=2, color='green', label='3 Hz')
fig.subplots_adjust(left=0.2)

lines_by_label = {l.get_label(): l for l in [l0, l1, l2]}
line_colors = ['black', 'red', 'green']

# Make checkbuttons with all plotted lines with correct visibility
rax = fig.add_axes([0.05, 0.4, 0.1, 0.15])
check = CheckButtons(
    ax=rax,
    labels=lines_by_label.keys(),
    actives=[l.get_visible() for l in lines_by_label.values()],
    label_props={'color': line_colors},
    frame_props={'edgecolor': line_colors},
    check_props={'facecolor': line_colors},
)
def callback(label):
    ln = lines_by_label[label]
    ln.set_visible(not ln.get_visible())
    ln.figure.canvas.draw_idle()

check.on_clicked(callback)
plt.show()

https://stackoverflow.com/questions/76959480/how-to-use-pyplot-widgets-checkbuttons-for-the-bar-plot/76960324#76960324

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九是否随机的称呼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值