[英]Matplotlib, How to update graph using text box and button widgets?
I want to create 3 line plots and use text boxes and buttons to enter new values to update the graph.我想创建 3 个线图并使用文本框和按钮输入新值来更新图形。 I would also like to be able to select the line that I want to update.
我还希望能够选择要更新的行。
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()
I can get the graph to update if there is only one text box.如果只有一个文本框,我可以让图表更新。 For example, the user only inputs a y-value.
例如,用户只输入一个 y 值。 However, in this case, there are 2 text boxes so each text box needs to wait for the other text box to be filled up and the graph is updated using the submit button.
但是,在这种情况下,有 2 个文本框,因此每个文本框都需要等待另一个文本框被填满,并使用提交按钮更新图形。 I would also want to be able to select which line I want to update which I am unsure of how to do.
我还希望能够选择我想更新的行,但我不确定该怎么做。
I am not sure what you want to do with your plot but I hope this modification helps you我不确定你想用你的情节做什么,但我希望这个修改对你有帮助
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.