File tree Expand file tree Collapse file tree 2 files changed +17
-7
lines changed Expand file tree Collapse file tree 2 files changed +17
-7
lines changed Original file line number Diff line number Diff line change 16
16
import numpy as np
17
17
import matplotlib .pyplot as plt
18
18
from matplotlib .widgets import TextBox
19
+
20
+
19
21
fig , ax = plt .subplots ()
20
- plt .subplots_adjust (bottom = 0.2 )
22
+ fig .subplots_adjust (bottom = 0.2 )
23
+
21
24
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 )
25
26
26
27
27
28
def submit (expression ):
@@ -33,12 +34,15 @@ def submit(expression):
33
34
"""
34
35
ydata = eval (expression )
35
36
l .set_ydata (ydata )
36
- ax .set_ylim (np .min (ydata ), np .max (ydata ))
37
+ ax .relim ()
38
+ ax .autoscale_view ()
37
39
plt .draw ()
38
40
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" )
41
44
text_box .on_submit (submit )
45
+ text_box .set_val ("t ** 2" ) # Trigger `submit` with the initial string.
42
46
43
47
plt .show ()
44
48
Original file line number Diff line number Diff line change @@ -761,6 +761,12 @@ def _rendercursor(self):
761
761
# and save its dimensions, draw the real text, then put the cursor
762
762
# at the saved dimensions
763
763
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
+
764
770
text = self .text_disp .get_text () # Save value before overwriting it.
765
771
widthtext = text [:self .cursor_index ]
766
772
self .text_disp .set_text (widthtext or "," )
You can’t perform that action at this time.
0 commit comments