将颜色条图保存为图像文件

Saving Colorbar Plot as Image File

我正在尝试将 matplotlib.colorbar 图保存为图像文件、jpg 或 png。下面代码中写的两种方式我都试过了
使用 clb.savefig 导致错误:

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

使用 plt.savefig 保存在没有绘图的空白图像中。

import matplotlib.pyplot as plt

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

clb.savefig('name.jpg') 

plt.savefig('name.jpg')

有什么我想念的吗?如何将 Colorbar 图保存为图像?

我明白了。不知道为什么这行得通,而我之前所做的却行不通。我认为这与 Jupyter notebook 显示内容的方式有关。但无论如何,这就是我所做的:

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()