9

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.

1

3 Answers 3

14

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()
Sign up to request clarification or add additional context in comments.

1 Comment

If I understand this correctly, this will indeed be built in starting from version 2.1 (good job!). However, I strongly suggest removing eval(text) from that example, especially since text is typed in by whoever's sitting in front of the computer.
4

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.

2 Comments

I would recommend using QT, looks better ;)
Hah yeah OK I do admit QT looks better. wxpython is another alternative.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.