我使用两个图形,它们是保险丝。它是散点图的热图保险丝,但热图没有颜色条,我想显示此颜色。当我尝试这样做时,它给我一个错误:
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)