我正在尝试用Python创建一个热图,以反映烘焙过程后从烤箱中出来的实际蛋糕长度与目标蛋糕长度的偏差
实际和目标滤饼长度的原始数据可在我的GitHub -https://github.com/masoomk87/CakeLength上获得
以下代码片段用于绘制目标和实际滤饼长度偏差的热图
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')
这是热图的输出。有趣的是,它在不存在数据的地方绘制热图。图中的红点显示数据的实际存在
我希望热图按照红点绘制(真实的数据存在的地方)
是否可以发现代码中的错误或导致分组统计数据意外工作的输入数据
1条答案
按热度按时间aydmsdu91#
binned_statistic_2d
计算的统计量在示例中为(X,Y)形状,但pcolormesh
假定输入矩阵的行列顺序。只需将Z
转置为pcolormesh
参数:前两个维度(M,N)定义网格数据的行和列。
pcolormesh documentation