Search code examples
pythonmatplotliblabelcolorbar

How to set distance between colorbar and its label


I have a problem of overlapping colorbar and its label as can be seen below:

enter image description here

Code:

import matplotlib.pyplot as plt

true = [1,2,3,4,5,6,7,8,9,10]
pred = [1.02, 2.1, 2.99, 4, 5.01, 5.98, 7, 8.1, 9, 10.1]
std = [0.001, 0.002, 0.002, 0.001, 0.003, 0.004, 0.004, 0.001, 0.002, 0.004]

   
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(1,1,1)
cmap = 'coolwarm'
my_plot = ax.scatter(true, pred, c=std, cmap=cmap, edgecolors='black', lw=1)
ax.set_xlabel('Observed', fontsize=18)
ax.set_ylabel('Predicted', fontsize=18)
cbar = fig.colorbar(my_plot)
cbar.set_label('standard deviation', rotation=270)
plt.axis('equal') 

Is there any way that I can increase distance between colorbar and its label?


Solution

  • Simply add pad to the label :

    cbar.set_label('standard deviation', rotation=270, labelpad=15)
    

    Result :

    Plot with padded label