X Tutup
Skip to content

Make CheckButtons based on subplots automatically #6309

@BTWS

Description

@BTWS

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup