Python Matplotlib和checkbuttons自定义
我正在使用matplotlib中的绘图,其中多条线由一个条目表示。特别是在拾取单个图例条目时取消选择或选择多行;为了清晰起见,我从matplotlib文档(和)中的演示开始,并对其进行了一些修改:Python Matplotlib和checkbuttons自定义,python,matplotlib,Python,Matplotlib,我正在使用matplotlib中的绘图,其中多条线由一个条目表示。特别是在拾取单个图例条目时取消选择或选择多行;为了清晰起见,我从matplotlib文档(和)中的演示开始,并对其进行了一些修改: %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 =
%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 notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons[![enter image description here][1]][1]
from matplotlib.patches import Rectangle
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)]
[rec.set_fill(False) for i, rec in enumerate(check.rectangles)]
def func(label):
for index,lab in enumerate(labels):
if lab==label:
l[index].set_visible(not l[index].get_visible())
check.rectangles[index].set_fill(True)
else:
check.rectangles[index].set_fill(False)
plt.draw()
check.on_clicked(func)
plt.show()
多亏了艾琳的贡献和重要性,我解决了第一个问题。对于第二个问题,我找到了一种解决方法,在复选框中输入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()
我认为问题在于问题中4Hz有两条线路,但2Hz只有一条。第一个问题现在已经回答了,谢谢!第二,我认为有问题,因为尝试打开/关闭不同的情节会导致“有趣”的行为。