Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions examples/widgets/textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox


fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
fig.subplots_adjust(bottom=0.2)

t = np.arange(-2.0, 2.0, 0.001)
s = t ** 2
initial_text = "t ** 2"
l, = plt.plot(t, s, lw=2) # make a plot for the math expression "t ** 2"
l, = ax.plot(t, np.zeros_like(t), lw=2)


def submit(expression):
Expand All @@ -33,12 +34,15 @@ def submit(expression):
"""
ydata = eval(expression)
l.set_ydata(ydata)
ax.set_ylim(np.min(ydata), np.max(ydata))
ax.relim()
ax.autoscale_view()
plt.draw()

axbox = plt.axes([0.1, 0.05, 0.8, 0.075])
text_box = TextBox(axbox, 'Evaluate', initial=initial_text)

axbox = fig.add_axes([0.1, 0.05, 0.8, 0.075])
text_box = TextBox(axbox, "Evaluate")
text_box.on_submit(submit)
text_box.set_val("t ** 2") # Trigger `submit` with the initial string.

plt.show()

Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,12 @@ def _rendercursor(self):
# and save its dimensions, draw the real text, then put the cursor
# at the saved dimensions

# This causes a single extra draw if the figure has never been rendered
# yet, which should be fine as we're going to repeatedly re-render the
# figure later anyways.
if self.ax.figure._cachedRenderer is None:
self.ax.figure.canvas.draw()

text = self.text_disp.get_text() # Save value before overwriting it.
widthtext = text[:self.cursor_index]
self.text_disp.set_text(widthtext or ",")
Expand Down