且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将颜色栏添加到现有轴

更新时间:2023-12-01 20:32:04

颜色条必须具有自己的轴.但是,您可以创建与上一个轴重叠的轴.然后使用cax kwarg告诉fig.colorbar使用新轴.

The colorbar has to have its own axes. However, you can create an axes that overlaps with the previous one. Then use the cax kwarg to tell fig.colorbar to use the new axes.

例如:

import numpy as np
import matplotlib.pyplot as plt

data = np.arange(100, 0, -1).reshape(10, 10)

fig, ax = plt.subplots()
cax = fig.add_axes([0.27, 0.8, 0.5, 0.05])

im = ax.imshow(data, cmap='gist_earth')
fig.colorbar(im, cax=cax, orientation='horizontal')
plt.show()