nalfahel
nalfahel

Reputation: 145

changing cell width in heatmap with an even number of ticks on the y axis

I have two time series. The first one ranges from 2000 to 2018 (so 19 years, 12 months each) and the second one ranges from 1995 to 2018 (so 24 years, 12 months each). I am using the same code to plot two heatmaps. The only difference is the number of ticks in the y-axis (# of years). Everything works fine for the first time series but for the second one, the cells width in the first and last row is cut in half... See codes for both time series and associated output below:

Time series 1:

dim=np.arange(1,13,1)

#creating the heat map for precipitation profile:

fig, ax = plt.subplots(figsize=(20,8))
heatmap = ax.imshow(ym_precp, cmap="viridis", aspect=0.5, clim=[0,200])
ax.set_xlabel("Months", fontsize=15)
ax.set_ylabel("Year Index", fontsize=15)
ax.set_title("Heat Map of the year to year precipitation patterns [mm]", fontsize=18)
ax.set_xticks(range(0,12))
ax.set_xticklabels(dim, rotation=45, ha='center', minor=False)
ax.set_yticks(range(0,19,1))
ax.grid(False)
plt.colorbar(heatmap, label = 'precipitation in mm')
fig.show()

output:

Time series 2:

fig, ax = plt.subplots(figsize=(20,8))
heatmap = ax.imshow(ym_precp, cmap="viridis", aspect=0.5, clim=[0,80])
ax.set_xlabel("Months", fontsize=15)
ax.set_ylabel("Year Index", fontsize=15)
ax.set_title("Heat Map of the year to year precipitation patterns [mm]", fontsize=18)
ax.set_xticks(range(0,12))
ax.set_xticklabels(dim, rotation=45, ha='center', minor=False)
ax.set_yticks(range(0,24,1))
ax.grid(False)
plt.colorbar(heatmap, label = 'precipitation in mm')
fig.show()

output:

Upvotes: 0

Views: 33

Answers (1)

nalfahel
nalfahel

Reputation: 145

So... I solved the problem by finding a work around which involves two modifications of the code above: 1) instead of defining my heatmap as heatmap = ax.imshow(..) i use heatmap = plt.pcolor(values, cmap=cmap, norm=norm) where norm = colors.BoundaryNorm(bounds, cmap.N)

2) to define y-ticks I use: ax.set_y_sticks(np.arange(0.5,24.5,1)) instead of ax.set_yticks(range(0,24,1)) as previously.

Below is my code, with some add-ins to improve the my heatmap layout...

#picking colors and ticks bounds for the colorbar:
cmap = colors.ListedColormap(["black",'indigo','navy','b','cornflowerblue', 'lightskyblue','lavender'])
bounds=[0, 3, 5, 8, 12, 15, 20,30]
norm = colors.BoundaryNorm(bounds, cmap.N)

#creating the heat map for precipitation profile:
fig, ax = plt.subplots(figsize=(15,8))
heatmap=plt.pcolor(ym_dry, cmap=cmap, norm=norm, edgecolor='white')
ax.set_xlabel("Months", fontsize=20)
ax.set_ylabel("Year Index", fontsize=20)
ax.set_title("Monthly total of dry days", fontsize=24)
plt.xticks(np.arange(0.5, 12.5, 1))
ax.set_xticklabels(dim, rotation=45, ha='center', minor=False)
ax.set_yticks(np.arange(0.5,24.5,1))
ax.set_yticklabels(dim2)
ax.grid(False)
ax.add_patch(Rectangle((-0.5, 15), 12.5, 5, fill=True, lw=3, alpha=0.8, color = "grey"))
plt.colorbar(heatmap, norm=norm, cmap = cmap, ticks=[0, 3, 5, 8, 10, 12, 15, 20, 25,  30], boundaries=bounds, spacing='proportional')
fig.text(0.84, 0.5, "Monthly total of dry days", fontsize=18, va="center", 
        ha="center", rotation=-90) 
ax.tick_params(labelsize=16)
fig.show()

output can be found here

Upvotes: 0

Related Questions