def _draw_heatmap(self) -> None:
"""Renders the heatmap cells, labels, and numbers on the canvas, centered and expanded."""
self.canvas.delete("all")
if not self.correlation_mat:
return
num_rows = len(self.correlation_mat)
num_cols = len(self.correlation_mat[0]) if num_rows > 0 else 0
# Get the current container size
container_width = self.canvas.winfo_width()
container_height = self.canvas.winfo_height()
# If the canvas hasn't been rendered yet, use reasonable defaults
if container_width <= 1 or container_height <= 1:
container_width = self.canvas.winfo_reqwidth()
container_height = self.canvas.winfo_reqheight()
# Calculate available space for cells after accounting for labels
available_width = container_width - self.label_margin_x
available_height = container_height - self.label_margin_y
# Calculate optimal cell size to fill available space
cell_width = available_width / num_cols if num_cols > 0 else 0
cell_height = available_height / num_rows if num_rows > 0 else 0
# Use the smaller dimension to keep cells square
self.cell_size = min(cell_width, cell_height)
# Calculate actual heatmap dimensions
heatmap_width = num_cols * self.cell_size
heatmap_height = num_rows * self.cell_size
# Calculate centering offsets
x_offset = self.label_margin_x + (available_width - heatmap_width) / 2
y_offset = (available_height - heatmap_height) / 2
# Set scroll region to accommodate the entire heatmap
total_width = heatmap_width + self.label_margin_x
total_height = heatmap_height + self.label_margin_y
self.canvas.config(scrollregion=(0, 0, total_width, total_height))
# Draw cells and text
for row_idx in range(num_rows):
for col_idx in range(num_cols):
value = self.correlation_mat[row_idx][col_idx]
# Calculate coordinates with centering offsets
x1 = col_idx * self.cell_size + x_offset
y1 = row_idx * self.cell_size + y_offset
x2 = x1 + self.cell_size
y2 = y1 + self.cell_size
# Get color
r, g, b = self._value_to_rgb(value)
fill_color = self._rgb_to_hex(r, g, b)
text_color = self._get_contrasting_text_color(r, g, b)
# Draw cell
self.canvas.create_rectangle(x1, y1, x2, y2, fill=fill_color, outline=self.motif_shadow)
# Draw text value
font_size = max(int(self.cell_size / 5), 8) # Adjust font size based on cell size
font = (self.font_text[0], font_size)
self.canvas.create_text((x1 + x2) / 2, (y1 + y2) / 2,
text=f"{value:.2f}", fill=text_color,
font=font)
# Draw labels with adjusted positions
self._draw_labels(num_rows, num_cols, x_offset, y_offset)
Would you like me to explain or break down this code?
def _draw_heatmap(self) -> None:
"""Renders the heatmap cells, labels, and numbers on the canvas, centered and expanded."""
self.canvas.delete("all")
if not self.correlation_mat:
return
num_rows = len(self.correlation_mat)
num_cols = len(self.correlation_mat[0]) if num_rows > 0 else 0
# Get the current container size
container_width = self.canvas.winfo_width()
container_height = self.canvas.winfo_height()
# If the canvas hasn't been rendered yet, use reasonable defaults
if container_width <= 1 or container_height <= 1:
container_width = self.canvas.winfo_reqwidth()
container_height = self.canvas.winfo_reqheight()
# Calculate available space for cells after accounting for labels
available_width = container_width - self.label_margin_x
available_height = container_height - self.label_margin_y
# Calculate optimal cell size to fill available space
cell_width = available_width / num_cols if num_cols > 0 else 0
cell_height = available_height / num_rows if num_rows > 0 else 0
# Use the smaller dimension to keep cells square
self.cell_size = min(cell_width, cell_height)
# Calculate actual heatmap dimensions
heatmap_width = num_cols * self.cell_size
heatmap_height = num_rows * self.cell_size
# Calculate centering offsets
x_offset = (available_width - heatmap_width) / 2
y_offset = (available_height - heatmap_height) / 2
# Set scroll region to accommodate the entire heatmap
total_width = heatmap_width + self.label_margin_x
total_height = heatmap_height + self.label_margin_y
self.canvas.config(scrollregion=(0, 0, total_width, total_height))
# Draw cells and text
for row_idx in range(num_rows):
for col_idx in range(num_cols):
value = self.correlation_mat[row_idx][col_idx]
# Calculate coordinates with centering offsets
x1 = col_idx * self.cell_size + self.label_margin_x + x_offset
y1 = row_idx * self.cell_size + y_offset
x2 = x1 + self.cell_size
y2 = y1 + self.cell_size
# Get color
r, g, b = self._value_to_rgb(value)
fill_color = self._rgb_to_hex(r, g, b)
text_color = self._get_contrasting_text_color(r, g, b)
# Draw cell
self.canvas.create_rectangle(x1, y1, x2, y2, fill=fill_color, outline=self.motif_shadow)
# Draw text value
font_size = max(int(self.cell_size / 5), 8) # Adjust font size based on cell size
font = (self.font_text[0], font_size)
self.canvas.create_text((x1 + x2) / 2, (y1 + y2) / 2,
text=f"{value:.2f}", fill=text_color,
font=font)
# Draw labels with the correct offsets
self._draw_labels(num_rows, num_cols, x_offset, y_offset)
This should now work correctly with your _draw_labels function, passing the proper x_offset and y_offset parameters.
def _draw_heatmap(self) -> None:
"""Renders the heatmap cells, labels, and numbers on the canvas."""
self.canvas.delete("all")
if not self.correlation_mat:
return
num_rows = len(self.correlation_mat)
num_cols = len(self.correlation_mat[0]) if num_rows > 0 else 0
# Get the current container size
container_width = self.canvas.winfo_width()
container_height = self.canvas.winfo_height()
# If the canvas hasn't been rendered yet, use reasonable defaults
if container_width <= 1 or container_height <= 1:
container_width = self.canvas.winfo_reqwidth()
container_height = self.canvas.winfo_reqheight()
# Calculate optimal cell size to fill available space
available_width = container_width - self.label_margin_x
available_height = container_height - self.label_margin_y
cell_width = available_width / num_cols if num_cols > 0 else 0
cell_height = available_height / num_rows if num_rows > 0 else 0
# Use the smaller dimension to keep cells square
self.cell_size = min(cell_width, cell_height)
# Calculate canvas size
canvas_width = num_cols * self.cell_size + self.label_margin_x
canvas_height = num_rows * self.cell_size + self.label_margin_y
self.canvas.config(width=canvas_width, height=canvas_height, scrollregion=(0, 0, canvas_width, canvas_height))
# Draw cells and text
for row_idx in range(num_rows):
for col_idx in range(num_cols):
value = self.correlation_mat[row_idx][col_idx]
# Calculate coordinates
x1 = col_idx * self.cell_size + self.label_margin_x
y1 = row_idx * self.cell_size
x2 = x1 + self.cell_size
y2 = y1 + self.cell_size
# Get color
r, g, b = self._value_to_rgb(value)
fill_color = self._rgb_to_hex(r, g, b)
text_color = self._get_contrasting_text_color(r, g, b)
# Draw cell
self.canvas.create_rectangle(x1, y1, x2, y2, fill=fill_color, outline=self.motif_shadow)
# Draw text value
font_size = max(int(self.cell_size / 5), 8) # Adjust font size based on cell size
font = (self.font_text[0], font_size)
self.canvas.create_text((x1 + x2) / 2, (y1 + y2) / 2,
text=f"{value:.2f}", fill=text_color,
font=font)
# Draw Y-axis (Row) Labels directly
for i in range(num_rows):
label = self.labels[i]
x = self.label_margin_x - 5
y = i * self.cell_size + self.cell_size / 2
font = self.font_bold if label == self.highlighted_instrument else self.font_normal
self.canvas.create_text(x, y, text=label, anchor=tk.E, font=font)
# Draw X-axis (Column) Labels directly
for i in range(num_cols):
label = self.labels[i]
x = i * self.cell_size + self.cell_size / 2 + self.label_margin_x
y = num_rows * self.cell_size + 5
font = self.font_bold if label == self.highlighted_instrument else self.font_normal
dist = 60
dist = dist + len(label)
self.canvas.create_text(x, y+dist, text=label, anchor=tk.N, angle=90, font=font)
This approach integrates the label drawing directly into the main drawing function, eliminating the need for a separate _draw_labels method with offset parameters. The heatmap will now be sized to fit the available space while maintaining square cells.