Python 对绘图中的配件设置限制

Python 对绘图中的配件设置限制,python,numpy,matplotlib,curve-fitting,Python,Numpy,Matplotlib,Curve Fitting,我有一个数据图,并对其应用了线性拟合,但是,我不知道为什么,但拟合线与数据本身相差甚远。我将如何对这条线施加限制,使它正好适合我的数据,最好使数据也成为图表的焦点 图形输出和代码如下: 正在使用的实际数据: logts=[-inf 2.89037176 3.58351894 3.98898405 4.49980967 4.68213123 4.83628191 4.9698133 5.08759634 5.19295685 5.28826703 5.37527841 5.45532112 5.

我有一个数据图,并对其应用了线性拟合,但是,我不知道为什么,但拟合线与数据本身相差甚远。我将如何对这条线施加限制,使它正好适合我的数据,最好使数据也成为图表的焦点

图形输出和代码如下:

正在使用的实际数据:

logts=[-inf 2.89037176 3.58351894 3.98898405 4.49980967 4.68213123 4.83628191 4.9698133 5.08759634 5.19295685 5.28826703 5.37527841 5.45532112 5.52942909 5.59842196 5.7235851 5.78074352 5.83481074 5.9348942 6.02586597 6.06842559 6.10924758 6.1484683 6.22257627 6.25766759 6.32435896 6.38687932 6.41673228 6.44571982 6.50128967 6.52795792 6.5539334 6.71901315 6.78219206]

安培=[77.78630383833547, 62.92926582239441, 63.84025706577048, 55.489066870438165, 38.60797989548756, 40.771390484048545, 14.679073842876978, 33.95959972488966, 29.41960790300141, 32.93241034391399, 30.927428194781815, 31.086396885182356, 21.52771899125612, 4.27684299160886, 6.432975528727562, 7.500376934048583, 18.730555740591637, 4.355896959987761, 11.677509915219987、12.865482314301719、0.6120306267606219、12.614420497451556、2.202502975442404、9.447046999592711、4.0688197216393425、0.546672901996845、1.12780050608008251、2.2030852358874635、2.202804718915858、0.57266686033587、0.5465322281618783、0.5185100682386、0.57595056759、[759]

注意到我犯了一个错误,我设法让图表更新了一点,但现在拟合完全失败了。

我还将上述代码更新为新版本。

使用xlim和ylim


这里有两个问题-输入错误和-inf的存在。 首先是打字错误——你把logt和amps分别归因于y和x,这与你的实际情况相符,而事实恰恰相反。 其次,fit例程无法很好地处理日志转换时间数组中存在的-inf。我们可以使用logts[1:]手动排除第一个点

样本输出:


如果你能发布一些有帮助的示例数据。全部完成。还添加了代码更新/graphHi Kim,谢谢你的回复。我遵循了代码,但这只是打印了一个图形,在该范围内谁的限制是可见的,最佳拟合线仍然是疯狂远离数据的地方,但现在它已经超出了图形的边缘。理想情况下,我是这样做的寻找是获得最佳拟合线以正确绘制的一种方法。这听起来像是模型的一件事,因为最佳拟合线总是假设从数据开始的点开始,并在数据结束的点结束,如果数据点聚集在远离0的位置,则可以将xlim和YLIM从CLO点开始对于你的数据,其他的0,正如我所做的,给我们你正在使用的数据将帮助我们知道你需要什么,因为要么是你的模型没有做你想让它做的,要么是数据聚集在远离我所给的点的地方,记住我给了0,10作为点的示例,你可以使用你喜欢的任何值更新图表/代码和按要求添加样本数据。最佳拟合行现在返回NaN。NaN返回是否可能是由于logts开始处的无穷大,因为ts的第一个值是0。我如何从数据集中删除该点。我不完全确定如何判断logts是列表、数组还是ndarray,如果这很重要?是的,就是这样,这就成功了。比k你!现在来看看为什么我的数据是垃圾…有趣的是,有没有一种简单的方法从图例中删除序列,只显示最佳拟合标签行,即“线性…”而不是“施密特…”图例显示标签参数定义的内容。在你的情况下,删除标签=施密特分析振幅对对数时间部分。
plt.plot(np.log(ts), amps, "1", ms=5, label="Schmitt Analysis (Amplitude against Log(Time))")

##Plot Linear Fit
y1, r, *_ = np.polyfit(amps, np.log(ts), 1, full=True)
f1 = np.poly1d(y1)
plt.plot(amps, f1(amps), label=f"linear ($\chi^2$ = {r[0]:0.2f})")

plt.xlabel("Log(Time)")
plt.ylabel("Amplitude")
plt.title("Schmitt Analysis (Amplitude against Log(Time))")
plt.xlim(0,10)
plt.ylim(-40,80)
plt.legend()

plt.savefig('A_Schmitt.jpg')
    plt.plot(np.log(ts), amps, "1", ms=5, label="Schmitt Analysis (Log(Amplitude) 
    against Time)")
    
    
    y1, r, *_ = np.polyfit(amps, ts, 1, full=True)
    f1 = np.poly1d(y1)
    plt.plot(amps, f1(amps), label=f"linear ($\chi^2$ = {r[0]:0.2f})")
    
    plt.xlabel("Log(Time)")
    plt.ylabel("Amplitude")
    plt.title("Schmitt Analysis (Amplitude against Log(Time)")
    plt.xlim(0, 10)
    plt.ylim(0, 10)
    plt.legend()
    
    plt.savefig('A_Schmitt.jpg'

)
import numpy as np
from matplotlib import pyplot as plt

#recreating your input - seemingly log(ts) is a numpy array
logts = np.asarray([-np.inf, 2.89037176, 3.58351894, 3.98898405, 4.49980967, 4.68213123, 4.83628191, 4.9698133, 5.08759634, 5.19295685, 5.28826703, 5.37527841, 5.45532112, 5.52942909, 5.59842196, 5.7235851, 5.78074352, 5.83481074, 5.9348942, 6.02586597, 6.06842559, 6.10924758, 6.1484683, 6.22257627, 6.25766759, 6.32435896, 6.38687932, 6.41673228, 6.44571982, 6.50128967, 6.52795792, 6.5539334, 6.71901315, 6.78219206])
amps = [77.78630383833547, 62.92926582239441, 63.84025706577048, 55.489066870438165, 38.60797989548756, 40.771390484048545, 14.679073842876978, 33.95959972488966, 29.41960790300141, 32.93241034391399, 30.927428194781815, 31.086396885182356, 21.52771899125612, 4.27684299160886, 6.432975528727562, 7.500376934048583, 18.730555740591637, 4.355896959987761, 11.677509915219987, 12.865482314301719, 0.6120306267606219, 12.614420497451556, 2.2025029753442404, 9.447046999592711, 4.0688197216393425, 0.546672901996845, 1.12780050608251, 2.2030852358874635, 2.202804718915858, 0.5726686031033587, 0.5465322281618783, 0.5185100682386156, 0.575055917739342, 0.5681697592593679]

#plot raw data
plt.plot(logts, amps, "1", ms=5, label="Schmitt Analysis (Amplitude against Log(Time))")

#linear fit excluding the first point that is an outlier
y1, r, *_ = np.polyfit(logts[1:], amps[1:], 1, full=True)
f1 = np.poly1d(y1)

#get min and max of logts excluding nan and inf values
logtsmin = np.floor(np.nanmin(logts[logts != -np.inf]))
logtsmax = np.ceil(np.nanmax(logts[logts != np.inf]))
#evenly spaced x-values for the fit line plot 
xlogts = np.linspace(logtsmin, logtsmax, 1000)
plt.plot(xlogts, f1(xlogts), label=f"linear ($\chi^2$ = {r[0]:0.2f})")

plt.xlabel("Log(Time)")
plt.ylabel("Amplitude")
plt.title("Schmitt Analysis (Amplitude against Log(Time))")
plt.xlim(logtsmin, logtsmax)
plt.legend()

plt.show()