0

I am trying to create a heatmap in Python for deviation in actual and target cake length that comes out of the oven after the baking process

The raw data for actual and target cake length is available at my GitHub - https://github.com/masoomk87/CakeLength

Below code snippet is used for plotting the heatmap of the deviation in the target and actual cake length

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.pyplot as gspec
import numpy as npr
from scipy.interpolate import griddata
import pyautogui
from scipy import stats


x = pyautogui.size()
width = x.width
height = x.height

path = r'C:\masoom\PycharmProjects\CakeProject\Logs\foo.csv'
df = pd.read_csv(path)

target_cake_length = npr.asarray(df['target_cake_length'].to_list())
actual_cake_length = npr.asarray(df['actual_cake_length'].to_list())

x = target_cake_length
y = actual_cake_length
x_linspace = 20
y_linspace = 20
x_lab = 'Target cake length [cm]'
y_lab = 'Actual cake length [cm]'
title = 'Target - Actual cake length deviation [cm]'

z = x - y

xi = npr.linspace(min(x), max(x), x_linspace)
yi = npr.linspace(min(y), max(y), y_linspace)
ret = stats.binned_statistic_2d(x, y, z, 'mean', bins=[xi, yi])
Z = ret.statistic
X, Y = npr.meshgrid(xi, yi)
fig, ax = plt.subplots(1, 1, figsize=(width / 100., height / 100.), dpi=100)
img = ax.pcolormesh(X, Y, Z)
ax.plot(x, y, 'r.')
cbar = plt.colorbar(img)
plt.xlabel(x_lab)
plt.ylabel(y_lab)
plt.title(title + 'Mean Error =' + str(npr.mean(z)))
img.set_clim(z.min(), z.max())
# fig.clim(z.min(), z.max())
textbox = '\n'.join([
    'Plot description : ',
    'Files used for plotting : ',
])
bbox = dict(boxstyle='square', facecolor='lavender', alpha=0.5)
ax.text(1.3, 1, textbox, fontsize=10, transform=ax.transAxes, bbox=bbox,
        verticalalignment='top')
fig.tight_layout()
mng = plt.get_current_fig_manager()
mng.window.state('zoomed')

This is the output of the heatmap. Interestingly, it plots the heatmap at places where no data exists. The red dots in the plot show the actual presence of data

I would have expected the heatmap to be plotted in accordance with the red dots (where real data exists)

Is it possible to spot the error in the code, or the input data which is causing the binned statistics to work unexpectedly

enter image description here

Masoom Kumar
  • 129
  • 1
  • 8

1 Answers1

1

The statistics calculated by binned_statistic_2d has (X, Y) shape in your example, but pcolormesh assumes row-column order of the input matrix. Just transpose Z in pcolormesh arguments:

img = ax.pcolormesh(X, Y, Z.T)

The first two dimensions (M, N) define the rows and columns of the mesh data.

pcolormesh documentation

Kuroneko
  • 146
  • 6