如何在python中使用2D直方图计算容器的平均值?我有x轴和y轴的温度范围,我试图用相应温度的箱子绘制闪电概率.我正在读取csv文件中的数据,我的代码是这样的:
filename = 'Random_Events_All_Sorted_85GHz.csv'
df = pd.read_csv(filename)
min37 = df.min37
min85 = df.min85
verification = df.five_min_1
#Numbers
x = min85
y = min37
H = verification
#Estimate the 2D histogram
nbins = 4
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
#Rotate and flip H
H = np.rot90(H)
H = np.flipud(H)
#Mask zeros
Hmasked = np.ma.masked_where(H==0,H)
#Plot 2D histogram using pcolor
fig1 = plt.figure()
plt.pcolormesh(xedges,yedges,Hmasked)
plt.xlabel('min 85 GHz PCT (K)')
plt.ylabel('min 37 GHz PCT (K)')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Probability of Lightning (%)')
plt.show() …Run Code Online (Sandbox Code Playgroud) 我有两个列表,我试图做一个形式y = a*e ^(bx)之间的指数拟合.我使用的方法与此处的第二个答案类似,但结果与我使用excel测试时所知的结果不符.这是我的代码:
import numpy as np
from scipy.optimize import curve_fit
exp_constants = [62.5, 87.5, 112.5, 137.5, 162.5, 187.5, 212.5, 237.5, 262.5, 287.5]
means = [211.94, 139.30, 80.09, 48.29, 26.94, 12.12, 3.99, 1.02, 0.09, 0.02]
def func(x1, a, b):
return a * np.exp(b * x1)
popt, pcov = curve_fit(func, exp_constants, means)
Run Code Online (Sandbox Code Playgroud)
返回时popt[0],popt[1]我分别得到3.222e-127和1.0.但是,使用excel检查时,正确的指数方程应为y = 7231.3e ^( - 0.04x).我对curve_fit方法不是很熟悉,我的代码中是否存在某些东西或者是否有更好的方法来获得正确的指数拟合?
编辑:以下是使用以下代码创建的图:
plt.figure()
plt.plot(exp_constants, means, 'ko', label="Data")
plt.plot(exp_constants, func(exp_constants, *popt), 'r-', label="Fitted Curve")
plt.legend()
plt.show
Run Code Online (Sandbox Code Playgroud)
