Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do text / number input fields exist in matplotlib?

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.

like image 979
Martin Thoma Avatar asked Aug 10 '14 17:08

Martin Thoma


People also ask

How to add text inside the plot in Matplotlib?

The matplotlib.pyplot.text () function is used to add text inside the plot. The syntax adds text at an arbitrary location of the axes. It also supports mathematical expressions. Syntax: matplotlib.pyplot.text (x, y, s, fontdict=None, **kwargs) fontdict – optional parameter. It overrides the default text properties

Does Matplotlib have a font manager?

Matplotlib includes its own matplotlib.font_manager (thanks to Paul Barrett), which implements a cross platform, W3C compliant font finding algorithm. The user has a great deal of control over text properties (font size, font weight, text location and color, etc.) with sensible defaults set in the rc file .

What is Matplotlib good for?

Matplotlib has extensive text support, including support for mathematical expressions, truetype support for raster and vector outputs, newline separated text with arbitrary rotations, and unicode support. Because it embeds fonts directly in output documents, e.g., for postscript or PDF, what you see on the screen is what you get in the hardcopy.

How do I change the text labelling in Matplotlib?

All the labelling in this tutorial can be changed by manipulating the matplotlib.font_manager.FontProperties method, or by named kwargs to set_xlabel Finally, we can use native TeX rendering in all text objects and have multiple lines:


2 Answers

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()
like image 62
QuadmasterXLII Avatar answered Oct 08 '22 23:10

QuadmasterXLII


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.

like image 24
Ffisegydd Avatar answered Oct 08 '22 22:10

Ffisegydd