2015-05-19 116 views

回答

2

可以設置插件箱顏色如下,

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) 
l1, = ax.plot(t, s1, lw=2) 
l2, = ax.plot(t, s2, lw=2) 
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'), (False, True, True)) 

#Define colours for rectangles and set them 
c = ['b', 'g', 'r']  
[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()) 
    elif label == '6 Hz': l2.set_visible(not l2.get_visible()) 
    plt.draw() 
check.on_clicked(func) 

plt.show() 

的checkbutton面板具有每個複選框作爲matplotlib.patches.Rectangle對象可以是customised根據需要。

+0

是的!非常感謝@Ed Smith。我很慚愧地說,我已經試圖弄清楚了這一點。 –

+0

很高興它的工作,matplotlib的部件很奇怪,主要是爲了方便而提供的,並且在你可以改變的地方非常有限。幸運的是矩形可以改變。如果你需要做任何事情,而不是簡單的定製,最好去一個完整的gui框架(例如tkinter或者wxpython)。 –

+0

我想我不能再拖延到gui框架的時間了......但是,感謝您的幫助,因爲我可以專注於其他事情。 –