When I click on the buttons to change the graph they become invisible, but are still usable. How do you keep them always displayed on the window? I know it's plt.clf()
that clears the content, but how to place buttons that will always stay under the graph?
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
def plot1(event):
plt.clf()
plt.scatter(liste_x,liste_graphe_1,s=5,c="red")
plt.draw()
def plot2(event):
plt.clf()
plt.scatter(liste_x,liste_graphe_2,s=5,c="green")
plt.draw()
liste_x=[1,2,3]
liste_graphe_1=[1,2,3]
liste_graphe_2=[3,2,1]
fig,ax=plt.subplots()
plt.scatter(liste_x,liste_graphe_1,s=5,c="red")
axBtn1=plt.axes([0.0,0.0,0.2,0.05])
Btn1=Button(axBtn1,label="graphe_1",color="grey",hovercolor="yellow")
Btn1.on_clicked(plot1)
axBtn2=plt.axes([0.2,0.0,0.2,0.05])
Btn2=Button(axBtn2,label="graphe_2",color="grey",hovercolor="yellow")
Btn2.on_clicked(plot2)
plt.show()
Buttons are placed, they change the graph as expected. I tried to take off plt.clf()
, but the graphs are not replaced without this function.
You have to use ax.
instead of plt.
.
First you have to use it to create plot ax.scatter(...)
Next you have to use it to remove plot ax.clear()
# PEP8: use space after `,` around `=`, etc.
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
def plot1(event):
ax.clear()
ax.scatter(liste_x, liste_graphe_1, s=5, c="red")
plt.draw()
def plot2(event):
ax.clear()
ax.scatter(liste_x, liste_graphe_2, s=5, c="green")
plt.draw()
liste_x = [1, 2, 3]
liste_graphe_1 = [1, 2, 3]
liste_graphe_2 = [3, 2, 1]
fig, ax = plt.subplots()
ax.scatter(liste_x, liste_graphe_1, s=5, c="red")
ax_btn1 = plt.axes([0.0, 0.0, 0.2, 0.05]) # PEP8: `lower_case_names` for variables
btn1 = Button(ax_btn1, label="graphe_1", color="grey", hovercolor="yellow")
btn1.on_clicked(plot1)
ax_btn2 = plt.axes([0.2, 0.0, 0.2, 0.05])
btn2 = Button(ax_btn2, label="graphe_2", color="grey", hovercolor="yellow")
btn2.on_clicked(plot2)
plt.show()