File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616import numpy as np
1717import matplotlib .pyplot as plt
1818from matplotlib .widgets import TextBox
19+
20+
1921fig , ax = plt .subplots ()
20- plt .subplots_adjust (bottom = 0.2 )
22+ fig .subplots_adjust (bottom = 0.2 )
23+
2124t = 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
2728def 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" )
4144text_box .on_submit (submit )
45+ text_box .set_val ("t ** 2" ) # Trigger `submit` with the initial string.
4246
4347plt .show ()
4448
Original file line number Diff line number Diff 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 "," )
You can’t perform that action at this time.
0 commit comments