pythonmatplotlibcolorbarsavefig

Saving Colorbar Plot as Image File


I am trying to save a matplotlib.colorbar plot as an image file, jpg or png. I have tried the two ways written in the code below.
Using clb.savefig results in the error:

AttributeError: 'Colorbar' object has no attribute 'savefig'

using plt.savefig saves in a blank image without the plot.

import matplotlib.pyplot as plt

plt.scatter(X,Y,c=Z, cmap='gnuplot2')
clb = plt.colorbar()

clb.savefig('name.jpg') 

plt.savefig('name.jpg')

Is there something I'm missing? How to save a Colorbar plot as an image?


Solution

  • I figured it out. Not sure why this works and what I did earlier didn't work. I think it had something to do with how Jupyter notebook displays things. But anyways here's what I did:

    plt.scatter(X,Y,c=Z, cmap='gnuplot2')
    clb = plt.colorbar()
    clb.set_label('label')
    plt.savefig('name.jpg') #this saves all of what is shown below
    plt.show()