Python Matplotlib,如何使用文本框和按钮小部件更新图形?
我想创建3个线条图,并使用文本框和按钮输入新值以更新图形。我还希望能够选择我要更新的行Python Matplotlib,如何使用文本框和按钮小部件更新图形?,python,pandas,matplotlib,Python,Pandas,Matplotlib,我想创建3个线条图,并使用文本框和按钮输入新值以更新图形。我还希望能够选择我要更新的行 import matplotlib.pyplot as plt from matplotlib.widgets import TextBox, Button import pandas as pd # Create figure fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.2) # I used pandas to read data fro
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox, Button
import pandas as pd
# Create figure
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
# I used pandas to read data from a csv file
# but in this case I will just use dummy values as example
d1 = pd.DataFrame({
"X": [i for i in range(10)],
"Y": [i for i in range(10)]
})
d2 = pd.DataFrame({
"X": [i for i in range(10)],
"Y": [i*2 for i in range(10)]
})
d3 = pd.DataFrame({
"X": [i for i in range(10)],
"Y": [i ** 2 for i in range(10)]
})
# Plot the data
ax.plot(d1["X"], d1["Y"])
ax.plot(d2["X"], d2["Y"])
ax.plot(d3["X"], d3["Y"])
# Show all x ticks
plt.xticks(d1["X"])
# Handles x value text box input
def textx(text):
pass
# Handles y value text box input
def texty(text):
pass
# Handles submit button
def submit(event):
pass
# Text box to input x value
axbox1 = fig.add_axes([0.1, 0.1, 0.5, 0.05])
x_textbox = TextBox(axbox1, "New x value")
x_textbox.on_submit(textx)
# Text box to input y value
axbox2 = fig.add_axes([0.1, 0.05, 0.5, 0.05])
y_textbox = TextBox(axbox2, "New y value")
y_textbox.on_submit(texty)
# Submit button
axbox3 = fig.add_axes([0.81, 0.05, 0.1, 0.075])
submit_button = Button(axbox3, "Submit!")
submit_button.on_clicked(submit)
plt.show()
如果只有一个文本框,我可以更新图表。例如,用户仅输入y值。但是,在这种情况下,有两个文本框,因此每个文本框都需要等待另一个文本框填满,并且使用submit按钮更新图形。我也希望能够选择哪一行我想更新,我不知道如何做
我不确定你想对你的情节做什么,但我希望这次修改能对你有所帮助
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox, Button
# Create figure
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
# I used pandas to read data from a csv file
# but in this case I will just use dummy values as example
X = [];Y1 = [];Y2 = [];Y3 = []
for i in range(10):
X.append(i)
Y1.append(i)
Y2.append(i*2)
Y3.append(i**2)
# Plot the data
ax.plot(X,Y1,X,Y2,X,Y3)
# Handles submit button
def submit(event):
print("yes")
print("x =", x_textbox.text)
print("y =", y_textbox.text)
x = int(x_textbox.text)
y = y_textbox.text
X = [];Y1 = [];Y2 = [];Y3 = []
if x not in ["", None]:
for i in range(x):
X.append(i)
Y1.append(i-1)
Y2.append(i*3)
Y3.append(i**3)
#fig.pop(0)
ax.lines.pop(0)
ax.lines.pop(0)
ax.lines.pop(0)
ax.plot(X,Y1,X,Y2,X,Y3)
# Text box to input x value
axbox1 = fig.add_axes([0.1, 0.1, 0.5, 0.05])
x_textbox = TextBox(axbox1, "New X")
# Text box to input y value
axbox2 = fig.add_axes([0.1, 0.05, 0.5, 0.05])
y_textbox = TextBox(axbox2, "New Y")
# Submit button
axbox3 = fig.add_axes([0.81, 0.05, 0.1, 0.075])
submit_button = Button(axbox3, "Submit!")
submit_button.on_clicked(submit)
plt.show()