qwwwsser 2024-04-09 21:13 采纳率: 100%
浏览 12
已结题

Python为什么画不出图啊

import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

# 定义Logistic模型函数
def logistic_model(t, r, K):
    return K / (1 + np.exp(-r * (t - t0)))

# 准备数据
t_data = np.array([2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022])
y_data = np.array([810828, 845459, 879391, 909091, 927292, 935952, 939678, 945879, 972785, 993595, 1025308, 1078989, 1123270, 1164790, 1209140, 1247510, 1268250])

# 初始参数猜测值
r_guess = 0.01
K_guess = 1500000
p0 = [r_guess, K_guess]

# 使用curve_fit进行拟合
popt, pcov = curve_fit(logistic_model, t_data, y_data, p0=p0)

# 从popt中提取最佳拟合参数r和K,并赋予正确的名称
r_estimated, K_estimated = popt

# 打印最佳拟合参数
print(f"Estimated growth rate (r): {r_estimated:.4f}")
print(f"Estimated carrying capacity (K): {K_estimated}")

# 使用最佳拟合参数绘制拟合曲线
t_fit = np.linspace(min(t_data), max(t_data), 300)  # 生成更细致的时间点
y_fit = logistic_model(t_fit, r_estimated, K_estimated)  # 计算拟合的因变量值

# 绘制数据点和拟合曲线
plt.scatter(t_data, y_data, label='Data Points')
plt.plot(t_fit, y_fit, label=f'Fitted Logistic Curve (r={r_estimated:.4f}, K={K_estimated:.0f})')
plt.xlabel('Year')
plt.ylabel('Population')
plt.title('Logistic Growth Model Fit to Population Data')
plt.legend()
plt.show()

为什么画不出图啊

  • 写回答

5条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2024-04-11 02:17
    关注

    【以下回答由 GPT 生成】

    在您的代码中,参数t0在logistic_model函数中被使用,但是在函数中未进行定义。这会导致代码在调用curve_fit函数时出现问题,从而无法成功拟合数据和绘制图形。
    
    为了解决这个问题,您需要在logistic_model函数中定义t0参数并传递给该函数。可以将t0作为logistic_model的第三个参数,然后在curve_fit和调用logistic_model时将其传递进去。下面是更新后的代码:
    
    ```python
    import numpy as np
    from scipy.optimize import curve_fit
    import matplotlib.pyplot as plt
    
    # 定义Logistic模型函数
    def logistic_model(t, r, K, t0):
        return K / (1 + np.exp(-r * (t - t0)))
    
    # 准备数据
    t_data = np.array([2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022])
    y_data = np.array([810828, 845459, 879391, 909091, 927292, 935952, 939678, 945879, 972785, 993595, 1025308, 1078989, 1123270, 1164790, 1209140, 1247510, 1268250])
    
    # 初始参数猜测值
    r_guess = 0.01
    K_guess = 1500000
    t0_guess = 2005
    p0 = [r_guess, K_guess, t0_guess]
    
    # 使用curve_fit进行拟合
    popt, pcov = curve_fit(logistic_model, t_data, y_data, p0=p0)
    
    # 从popt中提取最佳拟合参数r、K和t0
    r_estimated, K_estimated, t0_estimated = popt
    
    # 打印最佳拟合参数
    print(f"Estimated growth rate (r): {r_estimated:.4f}")
    print(f"Estimated carrying capacity (K): {K_estimated}")
    print(f"Estimated inflection point (t0): {t0_estimated}")
    
    # 使用最佳拟合参数绘制拟合曲线
    t_fit = np.linspace(min(t_data), max(t_data), 300)  # 生成更细致的时间点
    y_fit = logistic_model(t_fit, r_estimated, K_estimated, t0_estimated)  # 计算拟合的因变量值
    
    # 绘制数据点和拟合曲线
    plt.scatter(t_data, y_data, label='Data Points')
    plt.plot(t_fit, y_fit, label=f'Fitted Logistic Curve (r={r_estimated:.4f}, K={K_estimated:.0f}, t0={t0_estimated:.0f})')
    plt.xlabel('Year')
    plt.ylabel('Population')
    plt.legend()
    plt.show()
    

    通过上述更改,您的代码应该能够成功拟合数据并绘制出Logistic曲线图形了。祝您顺利! ```



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 4月24日
  • 已采纳回答 4月16日
  • 创建了问题 4月9日

悬赏问题

  • ¥15 微信小程序 前端页面内容搜索
  • ¥15 cpu是如何判断当前指令已经执行完毕,然后去执行下条指令的
  • ¥15 C++Codeinject远线程注入
  • ¥15 安装visual studio2022时visualstudiosetup启动不了,闪退。问题代号0x0和0x1389
  • ¥30 java spring boot2.5.3版本websocket连不上
  • ¥15 angular js调外部链接查看pdf
  • ¥15 openFOAM DPMFoam
  • ¥15 将查询到的值,赋值到table指定行中
  • ¥50 docker容器内部启动shell脚本多命令
  • ¥15 请问python的selenium怎么设置referer