在zfit中将直方图(2D)转换为2D PDF,可以通过以下步骤实现:
import zfit
import numpy as np
import matplotlib.pyplot as plt
x_data = np.random.normal(0, 1, 10000)
y_data = np.random.normal(0, 1, 10000)
hist = zfit.hist2d(x=x_data, y=y_data, bins=(100, 100), range=((-5, 5), (-5, 5)))
obs = hist.axes[0], hist.axes[1]
mean = zfit.Parameter("mean", 0, -5, 5)
sigma_x = zfit.Parameter("sigma_x", 1, 0, 5)
sigma_y = zfit.Parameter("sigma_y", 1, 0, 5)
model = zfit.models.Gauss2D(obs=obs, mean=mean, sigma_x=sigma_x, sigma_y=sigma_y)
extended_hist = hist.create_extended(sampled_values=True)
nll = zfit.loss.UnbinnedNLL(model=model, data=extended_hist)
minimizer = zfit.minimize.Minuit()
result = minimizer.minimize(nll)
x_values = np.linspace(-5, 5, 100)
y_values = np.linspace(-5, 5, 100)
x_mesh, y_mesh = np.meshgrid(x_values, y_values)
values = model.pdf(x=x_mesh, y=y_mesh).numpy()
plt.figure()
plt.pcolormesh(x_mesh, y_mesh, values, shading='auto')
plt.colorbar()
plt.xlabel('x')
plt.ylabel('y')
plt.title('2D PDF')
plt.show()
这样,你就可以在zfit中将直方图(2D)转换为2D PDF,并进行可视化展示。请注意,上述代码仅提供了实现的基本框架,具体细节可能需要根据实际情况进行调整和优化。
领取专属 10元无门槛券
手把手带您无忧上云