为什么 colorbar 不能配合 heatmap fuse 来 scatter?

Why colorbar cannot work with heatmap fuse to scatter?

我使用两个图形,它们是融合的。这是一个热图融合到一个散点图,但热图没有颜色条,我想显示这种颜色。当我尝试这样做时,它给我一个错误:

RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).

代码如下:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("data/forestfires.csv")

x_number_list = df.X.tolist()
y_number_list = df.Y.tolist()
x_number_list = np.array(x_number_list)
y_number_list = np.array(y_number_list)

area_number_list = df.area.tolist()
area_number_list = [int(round(x+1,0)) for x in area_number_list]
temperature_number_list = df.temp.tolist()
temperature_number_list = np.array(temperature_number_list)

heatmap, xedges, yedges = np.histogram2d(y_number_list, x_number_list, weights=temperature_number_list)
fig, ax1 = plt.subplots(figsize=(7,7))
ax1.imshow(heatmap, interpolation='bicubic', cmap='hot', origin='lower')
ax1.scatter(x_number_list, y_number_list, s=area_number_list, color=(157/255, 173/255, 245/255, 0.9))
ax1.set_ylim(y_number_list.min()-0.5, y_number_list.max()+0.5)
ax1.set_xlim(x_number_list.min()-0.5, x_number_list.max()+0.5)

cb = plt.colorbar()

plt.show()

这是绘图的结果:

我在这个程序中使用的所有数据都是数字。我在 jupyter 上工作,我在 python.

上使用最新版本

您需要指定颜色栏的位置以及颜色栏显示的内容。我会按照以下方式进行。

改变

ax1.imshow(heatmap, interpolation='bicubic', cmap='hot', origin='lower')

im = ax1.imshow(heatmap, interpolation='bicubic', cmap='hot', origin='lower')

然后将颜色条指定为

cb = fig.colorbar(im, ax=ax1)