@@ -23,39 +23,31 @@ def two_scales(ax1, time, data1, data2, c1, c2):
23
23
Parameters
24
24
----------
25
25
ax1 : axis
26
- Axis to put two scales on
26
+ Axis to share a second scale with.
27
27
28
28
time : array-like
29
- x-axis values for both datasets
29
+ x-axis values for both datasets.
30
30
31
- data1: array-like
32
- Data for left hand scale
31
+ data1, data2 : array-like
32
+ Data for respectively the left and the right hand scale.
33
33
34
- data2 : array-like
35
- Data for right hand scale
36
-
37
- c1 : color
38
- Color for line 1
39
-
40
- c2 : color
41
- Color for line 2
34
+ c1, c2 : color
35
+ Color for respectively the left and the right hand scale.
42
36
43
37
Returns
44
38
-------
45
- ax1 : axis
46
- Original axis
47
- ax2 : axis
48
- New twin axis
39
+ ax1, ax2 : tuple of axis instances
40
+ Respectively the original axis and its new twin axis.
49
41
50
42
"""
51
- ax2 = ax1 .twinx ()
52
-
53
- ax1 .plot (time , data1 , color = c1 )
54
- ax1 .set_xlabel ('time (s)' )
55
- ax1 .set_ylabel ('exp' )
43
+ ax2 = ax1 .twinx () # create a second axes that shares the same x-axis
56
44
57
- ax2 .plot (time , data2 , color = c2 )
58
- ax2 .set_ylabel ('sin' )
45
+ for ax , data , c in ((ax1 , data1 , c1 ), (ax2 , data2 , c2 )):
46
+ ax .plot (time , data , color = c )
47
+ # Color the y-axis (both label and tick labels)
48
+ ax .yaxis .label .set_color (c )
49
+ for t in ax .get_yticklabels ():
50
+ t .set_color (c )
59
51
60
52
return ax1 , ax2
61
53
@@ -64,18 +56,14 @@ def two_scales(ax1, time, data1, data2, c1, c2):
64
56
s1 = np .exp (t )
65
57
s2 = np .sin (2 * np .pi * t )
66
58
67
- # Create axes
59
+ # Create axes and plot the mock data onto them
68
60
fig , ax = plt .subplots ()
69
61
ax1 , ax2 = two_scales (ax , t , s1 , s2 , 'r' , 'b' )
70
62
71
- # Change color of each axis
72
- def color_y_axis (ax , color ):
73
- """Color your axes."""
74
- for t in ax .get_yticklabels ():
75
- t .set_color (color )
76
- return None
77
- color_y_axis (ax1 , 'r' )
78
- color_y_axis (ax2 , 'b' )
63
+ # Label both axes
64
+ ax1 .set_xlabel ('time (s)' )
65
+ ax1 .set_ylabel ('exp' )
66
+ ax2 .set_ylabel ('sin' ) # NB: we already took care of the x-label with ax1
79
67
80
- fig .tight_layout () # otherwise y-labels are slightly clipped
68
+ fig .tight_layout () # otherwise the y-labels are slightly clipped
81
69
plt .show ()
0 commit comments