Matplotlib, How to update graph using text box and button widgets?

Viewed 700

I want to create 3 line plots and use text boxes and buttons to enter new values to update the graph. 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. 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. I would also want to be able to select which line I want to update which I am unsure of how to do.

Output of my current graph

1 Answers

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()
Related