Reputation: 751
With the following code, it places a label on top of the colorbar, but it also places the y-label on the top as well. I would ideally want one label on top of the colorbar, and one on the right (as shown here with '# of contacts').
import matplotlib.pyplot as plt
import numpy as np
number = 100
number_ticks = 9
x_input = np.array([np.linspace(-1.,3.,100),]*number)
y_input = np.array([np.arange(0.,100.,1.),]*number).transpose()
output = np.array([np.linspace(0.,1.0,100),]*number)
bounds=np.linspace(0.0,1.0,number_ticks)
cntr1 = plt.contourf(x_input, y_input, output, vmin = 0.0, vmax = 1.0, levels = bounds)
cbar1 = plt.colorbar()
cbar1.set_label(r"$\bf{title}$", labelpad=-40, y=1.03, rotation=270)
cbar1.ax.set_ylabel(r"$\bf{y_label}$", labelpad=-40, y=1.03, rotation=0)
plt.show()
The desired output is both a label above the colorbar as well as to the right of it. The above code only places both labels in the same position (i.e. above). The y-label is not going to the side of the colorbar as I would like (as it does in the link I've shared).
Upvotes: 2
Views: 5719
Reputation: 4151
You could use the Axes.set_title
method:
import matplotlib.pyplot as plt
import numpy as np
number = 100
number_ticks = 9
x_input = np.array([np.linspace(-1.,3.,100),]*number)
y_input = np.array([np.arange(0.,100.,1.),]*number).transpose()
output = np.array([np.linspace(0.,1.0,100),]*number)
bounds=np.linspace(0.0,1.0,number_ticks)
cntr1 = plt.contourf(x_input, y_input, output, vmin = 0.0, vmax = 1.0, levels = bounds)
cbar1 = plt.colorbar()
cbar1.ax.set_title(r"$\bf{title}$")
cbar1.ax.set_ylabel(r"$\bf{y_label}$", labelpad=20, rotation=270)
plt.show()
I think ColorbarBase.set_label
is equivalent to cbar1.ax.set_ylabel
. And Axes.set_label
is to specify a legend entry.
Upvotes: 5