[BUG] After multiple scans and plots, heatmap becomes 3% smaller each scan (fixed) #10

Merged
iamdoubz merged 1 commits from heatmap_shrink_fix into main 2026-05-01 16:44:58 -05:00
Showing only changes of commit afbc6f77e9 - Show all commits
+10 -5
View File
@@ -63,10 +63,12 @@ def render_heatmap(
if fig is None or ax is None: if fig is None or ax is None:
fig, ax = plt.subplots(figsize=(10, 7)) fig, ax = plt.subplots(figsize=(10, 7))
fig.subplots_adjust(right=0.88) # permanent right margin for the colorbar
# Remove any colorbars left over from previous renders. # Remove any axes that aren't the main plot (stale colorbars, etc.).
# Colorbars attach to the figure, not the axes, so ax.clear() doesn't touch them. # We then recreate a fixed-position colorbar axes below so matplotlib never
for ax_obj in fig.axes[1:]: # axes[0] is the main plot; everything else is a colorbar # steals space from ax — that is what caused ax to shrink on every redraw.
for ax_obj in fig.axes[1:]:
ax_obj.remove() ax_obj.remove()
ax.clear() ax.clear()
@@ -115,14 +117,17 @@ def render_heatmap(
) )
if show_colorbar: if show_colorbar:
cbar = fig.colorbar(img_heatmap, ax=ax, fraction=0.03, pad=0.02) # Add a fixed-position axes for the colorbar. Using cax= instead of
# ax= means matplotlib draws into this dedicated axes without
# touching ax at all — no space is stolen, ax never shrinks.
cax = fig.add_axes([0.91, 0.15, 0.02, 0.70]) # [left, bottom, width, height]
cbar = fig.colorbar(img_heatmap, cax=cax)
cbar.set_label("Signal Strength (dBm)", fontsize=10) cbar.set_label("Signal Strength (dBm)", fontsize=10)
cbar.ax.tick_params(labelsize=8) cbar.ax.tick_params(labelsize=8)
cbar.ax.text(1.4, -90, "Poor", transform=cbar.ax.get_yaxis_transform(), cbar.ax.text(1.4, -90, "Poor", transform=cbar.ax.get_yaxis_transform(),
fontsize=7, color='gray', va='bottom') fontsize=7, color='gray', va='bottom')
cbar.ax.text(1.4, -30, "Excellent", transform=cbar.ax.get_yaxis_transform(), cbar.ax.text(1.4, -30, "Excellent", transform=cbar.ax.get_yaxis_transform(),
fontsize=7, color='gray', va='top') fontsize=7, color='gray', va='top')
# Black swatch at the very bottom representing "no signal found"
cbar.ax.text(1.4, -95, "No signal", transform=cbar.ax.get_yaxis_transform(), cbar.ax.text(1.4, -95, "No signal", transform=cbar.ax.get_yaxis_transform(),
fontsize=7, color='black', va='top', fontsize=7, color='black', va='top',
bbox=dict(facecolor='white', edgecolor='none', pad=1.5)) bbox=dict(facecolor='white', edgecolor='none', pad=1.5))