Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 44d46ed

Browse files
committed
DOC: add offset axes to secondary_axes
1 parent d9d5077 commit 44d46ed

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

examples/subplots_axes_and_figures/secondary_axis.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,13 @@ def inverse(x):
106106

107107
###########################################################################
108108
# A final example translates np.datetime64 to yearday on the x axis and
109-
# from Celsius to Fahrenheit on the y axis:
110-
109+
# from Celsius to Fahrenheit on the y axis. Note the addition of a
110+
# third y axis, and that it can be placed using a float for the
111+
# location argument
111112

112113
dates = [datetime.datetime(2018, 1, 1) + datetime.timedelta(hours=k * 6)
113114
for k in range(240)]
114-
temperature = np.random.randn(len(dates))
115+
temperature = np.random.randn(len(dates)) * 4 + 6.7
115116
fig, ax = plt.subplots(constrained_layout=True)
116117

117118
ax.plot(dates, temperature)
@@ -147,6 +148,21 @@ def fahrenheit_to_celsius(x):
147148
'right', functions=(celsius_to_fahrenheit, fahrenheit_to_celsius))
148149
secax_y.set_ylabel(r'$T\ [^oF]$')
149150

151+
152+
def celsius_to_anomaly(x):
153+
return (x - np.mean(temperature))
154+
155+
156+
def anomaly_to_celsius(x):
157+
return (x + np.mean(temperature))
158+
159+
160+
# document use of a float for the position:
161+
secax_y2 = ax.secondary_yaxis(
162+
1.2, functions=(celsius_to_anomaly, anomaly_to_celsius))
163+
secax_y2.set_ylabel(r'$T - \overline{T}\ [^oC]$')
164+
165+
150166
plt.show()
151167

152168
#############################################################################

0 commit comments

Comments
 (0)