8000 Merge pull request #17117 from anntzer/textbox · matplotlib/matplotlib@b306a8e · GitHub
[go: up one dir, main page]

Skip to content

Commit b306a8e

Browse files
authored
Merge pull request #17117 from anntzer/textbox
TextBox improvements.
2 parents 3297484 + b833a2b commit b306a8e

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

examples/widgets/textbox.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
import numpy as np
1717
import matplotlib.pyplot as plt
1818
from matplotlib.widgets import TextBox
19+
20+
1921
fig, ax = plt.subplots()
20-
plt.subplots_adjust(bottom=0.2)
22+
fig.subplots_adjust(bottom=0.2)
23+
2124
t = np.arange(-2.0, 2.0, 0.001)
22-
s = t ** 2
23-
initial_text = "t ** 2"
24-
l, = plt.plot(t, s, lw=2) # make a plot for the math expression "t ** 2"
25+
l, = ax.plot(t, np.zeros_like(t), lw=2)
2526

2627

2728
def submit(expression):
@@ -33,12 +34,15 @@ def submit(expression):
3334
"""
3435
ydata = eval(expression)
3536
l.set_ydata(ydata)
36-
ax.set_ylim(np.min(ydata), np.max(ydata))
37+
ax.relim()
38+
ax.autoscale_view()
3739
plt.draw()
3840

39-
axbox = plt.axes([0.1, 0.05, 0.8, 0.075])
40-
text_box = TextBox(axbox, 'Evaluate', initial=initial_text)
41+
42+
axbox = fig.add_axes([0.1, 0.05, 0.8, 0.075])
43+
text_box = TextBox(axbox, "Evaluate")
4144
text_box.on_submit(submit)
45+
text_box.set_val("t ** 2") # Trigger `submit` with the initial string.
4246

4347
plt.show()
4448

lib/matplotlib/widgets.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,12 @@ def _rendercursor(self):
761761
# and save its dimensions, draw the real text, then put the cursor
762762
# at the saved dimensions
763763

764+
# This causes a single extra draw if the figure has never been rendered
765+
# yet, which should be fine as we're going to repeatedly re-render the
766+
# figure later anyways.
767+
if self.ax.figure._cachedRenderer is None:
768+
self.ax.figure.canvas.draw()
769+
764770
text = self.text_disp.get_text() # Save value before overwriting it.
765771
widthtext = text[:self.cursor_index]
766772
self.text_disp.set_text(widthtext or ",")

0 commit comments

Comments
 (0)
0