条形图上的Python曲线拟合

编程入门 行业动态 更新时间:2024-10-06 01:39:41
本文介绍了条形图上的Python曲线拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在条形图上拟合曲线?

How do I fit a curve on a barplot?

我有一个方程,扩散方程,它有一些未知的参数,这些参数使曲线变大,变高等.另一方面,我有一个来自模拟的小样图.我想在小图中拟合曲线,并找到曲线的最佳参数,我该怎么做?

I have an equation, the diffusion equation, which has some unknown parameters, these parameters make the curve larger, taller, etc. On the other hand I have a barplot coming from a simulation. I would like to fit the curve on the barplot, and find the best parameters for the curve, how can I do that?

这是我通过手动拟合"获得的结果,因此基本上我将所有参数手动更改了几个小时.但是,有没有办法用python做到这一点?

This is what I obtained by 'manual fitting', so basically I changed manually all the parameters for hours. However is there a way to do this with python?

为简单起见,假设我有以下代码:

To make it simple, imagine I have the following code:

import matplotlib.pyplot as plt list1 = [] for i in range(-5,6): list1.append(i) width = 1/1.5 list2 = [0,0.2,0.6,3.5,8,10,8,3.5,0.6,0.2,0] plt.bar(list1,list2,width) plt.show() T = 0.13 xx = np.arange(-6,6,0.01) yy = 5*np.sqrt(np.pi)*np.exp(-((xx)**2)/(4*T))*scipy.special.erfc((xx)/(2*np.sqrt(T))) + np.exp(-((xx)**2)/(4*T)) plt.plot(xx,yy) plt.show()

很显然,这里的拟合非常困难,但是无论如何,有没有什么函数或函数可以让我找到等式的最佳系数:(其中T是已知的)

Clearly the fitting here would be pretty hard, but anyway, is there any function or such that allows me to find the best coefficients for the equation: (where T is known)

y = A*np.sqrt(np.pi*D)*np.exp(-((x-E)**2)/(4*D*T))*scipy.special.erfc((x-E)/(2*np.sqrt(D*T))) + 300*np.exp(-((x-E)**2)/(4*D*T))

这与已经提出的问题和scipy文档示例不同.在后者中,"xdata"是相同的,而在我看来,它可能是,也可能不是.此外,我还可以绘制此曲线拟合,该曲线未在文档中显示.条形的高度不是x的函数!因此,我的xdata不是我的ydata的功能,这与文档中的内容不同. 要了解我的意思,请尝试对文档中的代码进行一些更改以使其进入我的示例,请尝试以下操作:

This is different from the already asked question and from the scipy documentation example. In the latter the 'xdata' is the same, while in my case it might and might not be. Furthermore I would also be able to plot this curve fitting, which isn't shown on the documentation. The height of the bars is not a function of the x's! So my xdata is not a function of my ydata, this is different from what is in the documentation. To see what I mean try to change the code in the documentation a little bit, to fall into my example, try this:

def func(x,a,b,c): return a * np.exp(-b * x) + c xdata = np.linspace(0,4,50) y = func(xdata, 2.5, 1.3, 0.5) ydata = [1,6,3,4,6,7,8,5,7,0,9,8,2,3,4,5] popt, pcov = curve_fit(func,xdata,ydata)

如果运行此命令,它将不起作用.原因是我有ydata 16个元素和函数50个.发生这种情况是因为y从xdata中获取值,而ydata从另一组x值中获取值,此处未知.

if you run this, it doesn't work. The reason is that I have 16 elements for the ydata and 50 for the function. This happens because y takes values from xdata, while ydata takes values from another set of x values, which is here unknown.

谢谢

推荐答案

我坚持认为这个问题是重复的.这是使用curve_fit的典型工作流程的简短示例.让我知道您是否仍然认为情况有所不同.

I stand by my thinking that this question is a duplicate. Here is a brief example of the typical workflow using curve_fit. Let me know if you still think that your situation is different.

import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt # bar plot data list1 = range(-5, 6) list2 = [0, 0.2, 0.6, 3.5, 8, 10, 8, 3.5, 0.6, 0.2, 0] width = 1/1.5 plt.bar(list1, list2, width, alpha=0.75) # fit bar plot data using curve_fit def func(x, a, b, c): # a Gaussian distribution return a * np.exp(-(x-b)**2/(2*c**2)) popt, pcov = curve_fit(func, list1, list2) x = np.linspace(-5, 5, 100) y = func(x, *popt) plt.plot(x + width/2, y, c='g')

更多推荐

条形图上的Python曲线拟合

本文发布于:2023-07-18 08:11:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142722.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条形   图上   曲线   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!