1414np .random .seed (19680801 )
1515
1616fig , (ax1 , ax2 ) = plt .subplots (2 , figsize = (8 , 6 ))
17- ax1 .set (facecolor = '#FFFFCC' )
1817
1918x = np .arange (0.0 , 5.0 , 0.01 )
2019y = np .sin (2 * np .pi * x ) + 0.5 * np .random .randn (len (x ))
2120
22- ax1 .plot (x , y , '-' )
21+ ax1 .plot (x , y )
2322ax1 .set_ylim (- 2 , 2 )
24- ax1 .set_title ('Press left mouse button and drag to test' )
23+ ax1 .set_title ('Press left mouse button and drag '
24+ 'to select a region in the top graph' )
2525
26- ax2 .set (facecolor = '#FFFFCC' )
27- line2 , = ax2 .plot (x , y , '-' )
26+ line2 , = ax2 .plot ([], [])
2827
2928
3029def onselect (xmin , xmax ):
3130 indmin , indmax = np .searchsorted (x , (xmin , xmax ))
3231 indmax = min (len (x ) - 1 , indmax )
3332
34- thisx = x [indmin :indmax ]
35- thisy = y [indmin :indmax ]
36- line2 .set_data (thisx , thisy )
37- ax2 .set_xlim (thisx [0 ], thisx [- 1 ])
38- ax2 .set_ylim (thisy .min (), thisy .max ())
33+ region_x = x [indmin :indmax ]
34+ region_y = y [indmin :indmax ]
35+ line2 .set_data (region_x , region_y )
36+ ax2 .set_xlim (region_x [0 ], region_x [- 1 ])
37+ ax2 .set_ylim (region_y .min (), region_y .max ())
3938 fig .canvas .draw ()
4039
4140#############################################################################
@@ -47,7 +46,7 @@ def onselect(xmin, xmax):
4746
4847
4948span = SpanSelector (ax1 , onselect , 'horizontal' , useblit = True ,
50- rectprops = dict (alpha = 0.5 , facecolor = 'red ' ))
49+ rectprops = dict (alpha = 0.5 , facecolor = 'tab:blue ' ))
5150# Set useblit=True on most backends for enhanced performance.
5251
5352
0 commit comments