I am trying to add a colorbar to a chart that has a secondary y-axis. A minimal-ish example of my difficulties is given below. The trouble is that I cannot add a colorbar to an AxesSubplot object, and adding it directly as plt.colorbar puts it on top of my graph- see how it overlaps the rightmost y-axis. How can I fix this? Cheers.
import matplotlib.pyplot as plt
import numpy as np
import scipy.interpolate
time=[]
temp=[]
temp_f=[]
height=[]
for ti in np.arange( 0, 10, 0.5 ):
for te in np.arange( -12, 12, 0.5 ):
height.append( np.sqrt(1+ti)/(1+te*te)+ti*te/12.5 )
time.append(ti)
temp.append(te)
temp_f.append( 32+9.0*te/5)
time=np.array(time)
temp=np.array(temp)
temp_f=np.array(temp_f)
height=np.array(height)
xi, yi = np.linspace(time.min(), time.max(),25), np.linspace(temp.min(), temp.max(), 25)
xi, yi = np.meshgrid(xi, yi)
rbf = scipy.interpolate.Rbf(time, temp, height, function='linear')
zi = rbf(xi, yi)
fig = plt.figure()
ax1 = plt.gca()
ax1.set_xlabel( "Time (s)" )
ax1.set_ylabel( "Celsius" )
ax2 = ax1.twinx()
ax2.set_ylabel( "Fahrenheit" )
ax2.set_ylim( [temp_f.min(), temp_f.max()] )
img = ax1.imshow( zi, vmin=height.min(), vmax=height.max(), origin='lower',
extent=[time.min(), time.max(), temp.min(), temp.max()],
aspect='auto', cmap='YlOrRd')
plt.colorbar(img, label=r"Height (cm)",format='%1.1f', ax=ax1)
plt.show()