Do text and / or number input fields exist for matplotlib
?
I have seen the widget Slider
, but that is something different. I want a simple number input field.
Do text and / or number input fields exist for matplotlib
?
I have seen the widget Slider
, but that is something different. I want a simple number input field.
You are looking for the TextBox interactive widget, which was added in 2.1:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
t = np.arange(-2.0, 2.0, 0.001)
ydata = t ** 2
initial_text = "t ** 2"
l, = plt.plot(t, ydata, lw=2)
def submit(text):
ydata = eval(text)
l.set_ydata(ydata)
ax.set_ylim(np.min(ydata), np.max(ydata))
plt.draw()
axbox = plt.axes([0.1, 0.05, 0.8, 0.075])
text_box = TextBox(axbox, 'Evaluate', initial=initial_text)
text_box.on_submit(submit)
plt.show()
There currently exists no widgets that could be used to enter numbers as text. If you had a small selection of discrete numbers then you could use a RadioButton or you could use a slider as you've already suggested.
Your best would be to build a full GUI using Tkinter. This would allow you to add whatever GUI elements you need. It's also possible to embed matplotlib graphs in Tkinter, as shown in the two examples here and here.
I like QuadmasterXLII's answer and have something to add. if using this with the update slider system you can use this to get the inner text.
# text box
t_box = plt.axes([0.05, 0.95, 0.8, 0.04])
file_text_box = TextBox(t_box, 'text', initial=init_txt)
def update(val)
my_var = text_box.text_disp._text