Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CheckButtons based on subplots automatically #6309

Closed
BTWS opened this issue Apr 16, 2016 · 2 comments
Closed

Make CheckButtons based on subplots automatically #6309

BTWS opened this issue Apr 16, 2016 · 2 comments

Comments

@BTWS
Copy link
Contributor

BTWS commented Apr 16, 2016

How do I get an array similar to graphs, but automatically?

fig, ax = plt.subplots()
l0, = ax.plot(t, s0, visible=True, 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')

graphs = [l0,l1,l2]

My main goal is to make checkbuttons based on the subplots.

Based on widgets example code: check_buttons.py:

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=True, 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')

graphs = [l0,l1,l2] # I don't want to have to do this manually

plt.subplots_adjust(left=0.2,)

legend = ax.legend(loc='upper right', shadow=False)

rax = plt.axes([0.05, 0.4, 0.1, 0.15])

# Make checkbox with same strings as legend
labels = [str(label.get_text()) for label in ax.get_legend().get_texts()]
print labels
check = CheckButtons(rax, labels, (True,)*len(labels))

# I want to make this better
def funcNew(label):
    for lbl in labels:
        if label == lbl:
           graphs[labels.index(label)].set_visible(not graphs[labels.index(label)].get_visible())
    plt.draw()

check.on_clicked(funcNew)

plt.show()

Matplotlib 1.5.1., Python 2.7, Windows 10, Anaconda, PyCharm

@tacaswell
Copy link
Member

This question would be better handled on the mailing list (matplotlib-users@python.org https://mail.python.org/mailman/listinfo/matplotlib-users ).

@BTWS
Copy link
Contributor Author

BTWS commented Apr 17, 2016

I found the answer. It was graphs = ax.get_lines().

# Based on : widgets example code: check_buttons.py
#          : http://matplotlib.org/examples/widgets/check_buttons.html
# -- Make CheckButtons based on subplots automatically --

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()
ax.plot(t, s0, visible=False, lw=2, color='k', label='2 Hz')
ax.plot(t, s1, lw=2, color='r', label='4 Hz')
ax.plot(t, s2, lw=2, color='g', label='6 Hz')
plt.subplots_adjust(left=0.2)

lines = ax.get_lines()

# 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):
    lines[labels.index(label)].set_visible(not lines[labels.index(label)].get_visible())
    plt.draw()

check.on_clicked(func)

plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants