8000 signicant overhaul (of the style) of the example · matplotlib/matplotlib@fdb5900 · GitHub
[go: up one dir, main page]

Skip to content

Commit fdb5900

Browse files
committed
signicant overhaul (of the style) of the example
1 parent 367647e commit fdb5900

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

examples/api/two_scales.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,31 @@ def two_scales(ax1, time, data1, data2, c1, c2):
2323
Parameters
2424
----------
2525
ax1 : axis
26-
Axis to put two scales on
26+
Axis to share a second scale with.
2727
2828
time : array-like
29-
x-axis values for both datasets
29+
x-axis values for both datasets.
3030
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.
3333
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.
4236
4337
Returns
4438
-------
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.
4941
5042
"""
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
5644

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)
5951

6052
return ax1, ax2
6153

@@ -64,18 +56,14 @@ def two_scales(ax1, time, data1, data2, c1, c2):
6456
s1 = np.exp(t)
6557
s2 = np.sin(2 * np.pi * t)
6658

67-
# Create axes
59+
# Create axes and plot the mock data onto them
6860
fig, ax = plt.subplots()
6961
ax1, ax2 = two_scales(ax, t, s1, s2, 'r', 'b')
7062

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
7967

80-
fig.tight_layout() # otherwise y-labels are slightly clipped
68+
fig.tight_layout() # otherwise the y-labels are slightly clipped
8169
plt.show()

0 commit comments

Comments
 (0)
0