Prompt in MPL

I think a prompt could be very useful in MPL, just to build small little GUI’s that only need 1 or 2 boxes.
I also realize it is not easy, and for bigger jobs you want a full GUI environment like wx or Tk anyway, so I understand it when developers set other priorities.

Then again, I would really use it,
Mark

···

From: Matthias Michler <
MatthiasMichler@…361…>

Hello everybody,

Now my question is: Could a prompt be a useful part of matplotlib?

I worked on this some time ago, I never got to the point where I
thought it was ready for production but it is close. There is a
problem if usetex is enabled, because partial tex strings will cause
errors. But you can use it with regular text or plain text.

Give it a whirl -- I don't mind including tit in matplotlib.widgets
if people think it would be usefult. I do think there is a niche for
these kinds of things where people want brain dead simple widgets w/o
having to deal with GUI toolkits.

I'm pasting the example below, and also attaching it in case the lines
get wrapped

# usetex not supported
import matplotlib
matplotlib.rcParams['text.usetex'] = False

import matplotlib.transforms as transforms
import matplotlib.widgets as widgets
from pylab import figure, show, nx

class TextBox(widgets.Widget):
    def __init__(self, ax, s=''):
        self.canvas = ax.figure.canvas
        self.text = ax.text(0.025, 0.2, s,
                            fontsize=14,
                            #verticalalignment='baseline',
                            horizontalalignment='left',
                            transform=ax.transAxes)
        self.ax = ax
        ax.set_yticks([])
        ax.set_xticks([])

        ax.set_navigate(False)
        self.canvas.draw()
        self.canvas.mpl_connect('key_press_event', self.keypress)

        self.region = self.canvas.copy_from_bbox(ax.bbox)

        r = self._get_text_right()
        self.cursor, = ax.plot([r,r], [0.2, 0.8], transform=ax.transAxes)
        self.redraw()

    def redraw(self):
        self.ax.redraw_in_frame()
        self.canvas.blit(self.ax.bbox)

    def keypress(self, event):
        if event.key is not None and len(event.key)>1: return

        t = self.text.get_text()
        if event.key is None: # simulate backspace
            if len(t): newt = t[:-1]
            else: newt = ''
        else:
            newt = t + event.key

        self.text.set_text(newt)

        r = self._get_text_right()
        self.cursor.set_xdata([r,r])
        self.redraw()

    def _get_text_right(self):
        l,b,w,h = self.text.get_window_extent().get_bounds()
        r = l+w+2
        t = b+h
        s = self.text.get_text()
        # adjust cursor position for trailing space
        numtrail = len(s) - len(s.rstrip())
        en = self.ax.get_renderer_cache().points_to_pixels(self.text.get_fontsize())/2.

        r += numtrail*en
        l,b = self.ax.transAxes.inverse_xy_tup((l,b))
        r,t = self.ax.transAxes.inverse_xy_tup((r,t))
        return r

fig = figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.7])
ax.plot([1,2,3])

#rc('text', usetex=1)
fig.text(0.39, 0.875, 'My label: ',
       horizontalalignment='right', verticalalignment='center')
axtext = fig.add_axes([0.4, 0.85, 0.5, 0.05])

box = TextBox(axtext)

show()

textbox2.py (2.27 KB)

···

On 6/5/07, Mark Bakker <markbak@...287...> wrote:

I think a prompt could be very useful in MPL, just to build small little
GUI's that only need 1 or 2 boxes.
I also realize it is not easy, and for bigger jobs you want a full GUI
environment like wx or Tk anyway, so I understand it when developers set
other priorities.
Then again, I would really use it,
Mark

> From: Matthias Michler < MatthiasMichler@...361...>
>
> Hello everybody,
>
> Now my question is: Could a prompt be a useful part of matplotlib?

Typo: "plain text" was meant to be "math text"

Now I remember what really bothered me about this widget, and it
wasn't just the usetex problem. The problem is that mpl has three
different vertical alignment methods for text: top, bottom and center.
None of them are right for a text box: you want baseline. Try typing
"thinking" into the text box and watch what happens when you add and
remove the "g". We do need to support baseline alignment for text, so
if someone has an interest in adding this it would be a very useful
feature, not just for a text box for for text alignment (eg tick
labels) in general.

See the image of the "g" at
http://freetype.sourceforge.net/freetype2/docs/glyphs/glyphs-3.html
for a visual representation -- hwat I am calling the "baseline" they
refer to as the "origin" in that graph. Our default alignment should
be "origin" or "baseline" but we don't have support for that.

JDH

···

On 6/5/07, John Hunter <jdh2358@...287...> wrote:

I worked on this some time ago, I never got to the point where I
thought it was ready for production but it is close. There is a
problem if usetex is enabled, because partial tex strings will cause
errors. But you can use it with regular text or plain text.

I notice the alignment problem.
But it looks like you are close.
On my machine (win32), the ‘enter’ key didn’t work either. It works like a backspace. That sounds like what Matthias reported.
Mark

···

On 6/5/07, John Hunter <jdh2358@…287…> wrote:

On 6/5/07, John Hunter <jdh2358@…287…> wrote:

I worked on this some time ago, I never got to the point where I
thought it was ready for production but it is close. There is a

problem if usetex is enabled, because partial tex strings will cause
errors. But you can use it with regular text or plain text.

Typo: “plain text” was meant to be “math text”

Now I remember what really bothered me about this widget, and it
wasn’t just the usetex problem. The problem is that mpl has three
different vertical alignment methods for text: top, bottom and center.
None of them are right for a text box: you want baseline. Try typing

“thinking” into the text box and watch what happens when you add and
remove the “g”. We do need to support baseline alignment for text, so
if someone has an interest in adding this it would be a very useful

feature, not just for a text box for for text alignment (eg tick
labels) in general.

See the image of the “g” at
http://freetype.sourceforge.net/freetype2/docs/glyphs/glyphs-3.html

for a visual representation – hwat I am calling the “baseline” they
refer to as the “origin” in that graph. Our default alignment should
be “origin” or “baseline” but we don’t have support for that.

JDH