8000 overhaul of the overhaul · matplotlib/matplotlib@d7696fe · GitHub
[go: up one dir, main page]

Skip to content

Commit d7696fe

Browse files
committed
overhaul of the overhaul
1 parent ce43564 commit d7696fe

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

examples/api/two_scales.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,20 @@
2222
data1 = np.exp(t)
2323
data2 = np.sin(2 * np.pi * t)
2424

25-
# Create a pair of twin axes (ax1 and ax2) that share the same x-axis
2625
fig, ax1 = plt.subplots()
27-
ax2 = ax1.twinx() # create the second `Axes` instance
2826

29-
# Plot a different set of data on each axes
30-
for ax, data, c in ((ax1, data1, "red"), (ax2, data2, "blue")):
31-
ax.plot(t, data, color=c)
32-
# Color the y-axis (both label and tick labels) accordingly to the data
33-
ax.yaxis.label.set_color(c)
34-
for tl in ax.get_yticklabels():
35-
tl.set_color(c)
36-
37-
# Label both axes
27+
color = 'tab:red'
3828
ax1.set_xlabel('time (s)')
39-
ax1.set_ylabel('exp')
40-
ax2.set_ylabel('sin') # NB: we already took care of the x-label with ax1
29+
ax1.set_ylabel('exp', color=color)
30+
ax1.plot(t, data1, color=color)
31+
ax1.tick_params(axis='y', labelcolor=color)
32+
33+
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
34+
35+
color = 'tab:blue'
36+
ax2.set_ylabel('sin', color=color) # we already handled the x-label with ax1
37+
ax2.plot(t, data2, color=color)
38+
ax2.tick_params(axis='y', labelcolor=color)
4139

4240
fig.tight_layout() # otherwise the right y-label is slightly clipped
4341
plt.show()

0 commit comments

Comments
 (0)
0