I have a heatmap fuse to a scatterplot and I want to change the value of the colorbar ticks. The colorbar take heatmap value(0 to 1100) but I want to have a colorbar with temperature values(6,33). How can I do that without changing the apparence of the heatmap. I try to use clim
but it change the apparence of the heatmap. Here's the code:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
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))
im = ax1.imshow(heatmap, interpolation='bicubic', cmap='hot', origin='lower') #bicubic
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(im, ax=ax1, shrink=0.73)
#im.set_clim(temperature_number_list.min(), temperature_number_list.max())
plt.show()
and here's the result:
When I use clim, im.set_clim(temperature_number_list.min(), temperature_number_list.max())
I get this result(it's a result that I DON'T want):
I found a solution, it's a tricky one but if you have any other option that's better than mine I will keep this topic open.
I create a fake heatmap with the same dimension as the first one but I put my range in the fake heatmap. so here's the code:
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)
#create fake heatmap
colorbar_parameter_by_temperature = []
for i in range(len(heatmap)):
colorbar_parameter_by_temperature.append([0]*len(heatmap[0]))
colorbar_parameter_by_temperature[i][0]=temperature_number_list.max()
colorbar_parameter_by_temperature[i][1]=temperature_number_list.min()
fig, ax1 = plt.subplots(figsize=(7,7))
#im = ax1.imshow(heatmap, interpolation='bicubic', cmap='hot', origin='lower') #bicubic
im2 = ax1.imshow(heatmap2, interpolation='spline16', cmap='hot', origin='lower')
im = ax1.imshow(heatmap, interpolation='spline16', 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(im2, ax=ax1, shrink=0.73)
plt.show()
here's the result:
[[33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0], [33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0], [33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0], [33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0], [33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0], [33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0], [33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0], [33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0], [33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0], [33.3, 2.2, 0, 0, 0, 0, 0, 0, 0, 0]]