gpt4 book ai didi

python - Matplotlib 和检查按钮定制

转载 作者:行者123 更新时间:2023-11-30 22:02:37 25 4
gpt4 key购买 nike

我正在 matplotlib 中绘制一个图,其中多行由单个条目表示。特别是在选择单个图例条目时取消选择或选择多行;为了清楚起见,我从 matplotlib 文档中的演示开始( https://matplotlib.org/gallery/widgets/check_buttons.htmlhttps://matplotlib.org/api/widgets_api.html#matplotlib.widgets.CheckButtons ),我只是对其进行了一些修改:

%matplotlib notebook
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)
s3 = 2*np.sin(4*np.pi*t)

fig, ax = plt.subplots()
l0, = ax.plot(t, s0, lw=2,c='r')
l1, = ax.plot(t, s1, lw=2,c='b')
l2, = ax.plot(t, s2, lw=2,c='g')
l3, = ax.plot(t, s3, lw=2,c='b')
plt.subplots_adjust(left=0.2)

rax = plt.axes([0.05, 0.4, 0.1, 0.15])
check = CheckButtons(rax, ('2 Hz', '4 Hz', '6 Hz'), (True, True, True))

#Define colours for rectangles and set them
c = ['r', 'b', 'g']
[rec.set_facecolor(c[i]) for i, rec in enumerate(check.rectangles)]


def func(label):
if label == '2 Hz':
l0.set_visible(not l0.get_visible())
elif label == '4 Hz':
l1.set_visible(not l1.get_visible())
l3.set_visible(not l3.get_visible())
elif label == '6 Hz':
l2.set_visible(not l2.get_visible())
plt.draw()
check.on_clicked(func)

plt.show()

输出是这样的:

matplotlib_checkbuttons_example

我想解决两类问题:

  1. 我不知道如何循环创建“lx”值(我有很多行要创建,我不想写“l0”,“l1”,“l2”...等)
  2. 我希望复选按钮有不同的外观。特别是,我希望在选择时有一个填充线条颜色的矩形,在取消选择时有一个空矩形(白色背景)。

我尝试在文档中寻找帮助,但我是新手,目前陷入困境。有人可以帮我吗?

谢谢

最佳答案

感谢 AILearning 和 ImportanceOfBeingErnest 的贡献,我解决了第一个问题。对于第二个问题,我找到了一种解决方法,方法是在复选框中设置 0 线宽,并仅在线条可见或不可见时激活或停用背景颜色。最终答案是:

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)
s3 = 2*np.sin(4*np.pi*t)

fig, ax = plt.subplots()

l=[]
for si,c,label in zip([s0,s1,s2,s3],['r','b','g','b'],('2 Hz', '4 Hz', '6 Hz','4 Hz')):
l.append(ax.plot(t, si, lw=2,c=c,label=label)[0])


plt.subplots_adjust(left=0.2)
labels = [str(line.get_label()) for line in l]



rax = plt.axes([0.05, 0.4, 0.1, 0.15])
check = CheckButtons(rax, ('2 Hz', '4 Hz', '6 Hz'), (True,True,True))

#Define colours for rectangles and set them
c = ['r', 'b', 'g']
[rec.set_facecolor(c[i]) for i, rec in enumerate(check.rectangles)]
[ll.set_linewidth(0) for l in check.lines for ll in l]

def func(label):
id_lab = [i for i, s in enumerate(check.labels) if label in s._text]
for index,lab in enumerate(labels):
if lab==label:
l[index].set_visible(not l[index].get_visible())
if (l[index].get_visible()):
check.rectangles[id_lab[0]].set_fill(True)
else:
check.rectangles[id_lab[0]].set_fill(False)
plt.draw()

check.on_clicked(func)

plt.show()

关于python - Matplotlib 和检查按钮定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53760696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com