Home > Enterprise >  Please help! How can i adjust 2 plot with one button in matplotlib
Please help! How can i adjust 2 plot with one button in matplotlib

Time:12-09

i'm just studied Python in 1 month and have no experience. I'm try to hide/show two of graphs with one Check button in Matplotlib. But with my code, when clicking button, there is only one graph be hidden. Please see my code and show me my mistake.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import math
from matplotlib.widgets import Button, RadioButtons, CheckButtons

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
p = ax.scatter(5,6,7) and ax.scatter(1,2,3, color='red', marker=' ', s=1e2)

lines = [p]
labels = ["Hide/Show"]

def func1(label):
    index = labels.index(label)
    lines[index].set_visible(not lines[index].get_visible())
    fig.canvas.draw()

a = [True]

# xposition, yposition, width, height
ax_check = plt.axes([0, 0.01, 0.25, 0.25])
plot_button = CheckButtons(ax_check, labels, a)
plot_button.on_clicked(func1)

plt.show()

CodePudding user response:

@ D.L Your suggestion is perfect.

Just add another line to the figure and in the function fun1 add a calling of the line 2:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import math
from matplotlib.widgets import Button, RadioButtons, CheckButtons

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
p1 = ax.scatter(5,6,7) 
p2 = ax.scatter(1,2,3, color='red', marker=' ', s=1e2)

lines = [p1, p2]
labels = ["Hide/Show"]

def func1(label):
        index = labels.index(label)
        lines[index].set_visible(not lines[index].get_visible())
        lines[index 1].set_visible(not lines[index 1].get_visible())
        fig.canvas.draw()

a = [True]

# xposition, yposition, width, height
ax_check = plt.axes([0, 0.01, 0.25, 0.25])
plot_button = CheckButtons(ax_check, labels, a)
plot_button.on_clicked(func1)

plt.show()
  • Related