python - 在 Python 中从 matplotlib 中的散点图绘制曲线?

标签 python graph matplotlib

enter image description here

我的问题:

我如何通过这些数据绘制一条曲线,从而描述该图的方程式..

我通过以下代码生成了这个散点图,但我无法弄清楚如何为这些数据生成一个方程并同时在这个图上绘制相应的曲线。请帮忙!

def draw(data,xlabel,ylabel):
    print('length of data : ',len(data))
    x,y = [],[]
    for i in data:
        x.append((i[1]))
        y.append((i[0]))
    plt.scatter(x, y,marker=r'o',color='b')
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.show()

基本上我想要这样的东西:enter image description here

最佳答案

您必须执行曲线拟合程序,这在数学上称为回归问题。在您的情况下,数据似乎或多或少呈指数增长,但您可以通过 scipy.optimize.curve_fit

拟合任意函数

http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

关于python - 在 Python 中从 matplotlib 中的散点图绘制曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24974436/

相关文章:

python - QTreeWidgetItem.addChild() 在某些情况下不起作用?

python - 使用电子邮件验证用户帐户 (Google App Engine)

python - python 中的曲线拟合

python - 使用 timedelta 系列绘制直方图

Python Pandas Group By 错误 'Index' 对象没有属性 'labels'

python - matlab 的 fminsearch 的 numpy/scipy 模拟

javascript - 导出 Highcharts 时无法根据需要禁用图例标题/启用图例标题

algorithm - 基于两个指标(距离、成本)的图形中的最佳(折衷)路径

database-design - 分区加权有向图(基于键/值数据库)

Python Matplotlib 在一张图片中制作动画条形图和绘图