from tkinter import * import tkinter as tk import mpl_toolkits.axisartist as axisartist import matplotlib.pyplot as plt from scipy.optimize import curve_fit import data as gl_data import numpy as np from X1 import random_points def window(): root_window = Tk() root_window.title('函数拟合') root_window.geometry('900x600') # 设置窗口大小:宽x高,注,此处不能为 "*",必须使用 "x" # 设置主窗口的背景颜色,颜色值可以是英文单词,或者颜色值的16进制数,除此之外还可以使用Tk内置的颜色常量 root_window["background"] = "white" root_window.resizable(0, 0) # 防止用户调整尺寸 label1 = tk.Label(root_window, text="样本数据\n集文件", font=('Times', 8), bg="white", width=13, height=3, # 设置标签内容区大小 padx=0, pady=0, borderwidth=0, ) label1.place(x=10, y=4) # 设置填充区距离、边框宽度和其样式(凹陷式) label3 = tk.Label(root_window, text="", font=('Times', 8), bg="white", fg="black", width=39, height=2, padx=0, pady=0, borderwidth=0, relief="ridge", highlightcolor="blue") label3.place(x=122, y=10) return root_window # if __name__ == '__main__': # root_window = window() # root_window.mainloop() def draw_axis(low, high, step=250): fig = plt.figure(figsize=(4.4, 3.2)) # 设置显示大小 ax = axisartist.Subplot(fig, 111) # 使用axisartist.Subplot方法创建一个绘图区对象ax fig.add_axes(ax) # 将绘图区对象添加到画布中 ax.axis[:].set_visible(False)# 通过set_visible方法设置绘图区所有坐标轴隐藏 ax.axis["x"] = ax.new_floating_axis(0, 0) # 添加新的x坐标轴 ax.axis["x"].set_axisline_style("-|>", size=1.0) # 给x坐标轴加上箭头 ax.axis["y"] = ax.new_floating_axis(1, 0) # 添加新的y坐标轴 ax.axis["y"].set_axisline_style("-|>", size=1.0) # y坐标轴加上箭头 ax.axis["x"].set_axis_direction("bottom") # 设置x、y轴上刻度显示方向 ax.axis["y"].set_axis_direction("left") # 设置x、y轴上刻度显示方向 plt.xlim(low, high) # 把x轴的刻度范围设置 plt.ylim(low, high) # 把y轴的刻度范围设置 ax.set_xticks(np.arange(low, high + 5, step)) # 把x轴的刻度间隔设置 ax.set_yticks(np.arange(low, high + 5, step)) # 把y轴的刻度间隔设置 # plt.show() # if __name__ == '__main__': # draw_axis(-1000, 1000) def selfdata_show(sampleData): num_points = len(sampleData) # 样本数据点的数量 colors = np.random.rand(num_points, 3) # 生成随机颜色 draw_axis(0, 1000) plt.scatter(sampleData[:, 0], sampleData[:, 1], c=colors) # 绘制样本数据点 plt.show() #显示图片 # if __name__ == '__main__': # sampleData = random_points() # 生成样本点 # selfdata_show(sampleData) # 绘制样本数据点 def quadratic_function(x, a, b, c, d): #构造三次函数y = a * X^3 + b * X^2 + C * x + d return a * x ** 3 + b * x **2 + c * x + d def draw_line(low, high, sx, sy): draw_axis(low, high) #绘制坐标轴 popt = [] #初始化curve_fit pcov = [] #初始化curve_fit gl_data.yvals_pow = [] #初始化curve_fit popt, pcov = curve_fit(quadratic_function, sx, sy) # 用curve_fit来对点进行拟合 curve_x = np.arange(low, high) #按照步长生成的一串数字 curve_y = [quadratic_function (i, *popt) for i in curve_x] # 根据x0(按照步长生成的一串数字)来计算y1值 plt.plot(curve_x, curve_y, color='blue', label='Fitted Curve') #绘制拟合曲线 plt.legend() plt.show() #显示函数图像 if __name__ == '__main__': curve_data = [[-375,250],[-750,0],[-1000,-500],[0,0],[375,-250],[750,0],[1000,500]] x = np.array([point[0] for point in curve_data]) # 为二次函数.txt y = np.array([point[1] for point in curve_data]) draw_line(-1000, 1000, x, y) # 下面是一个用于拟合曲线的最小二乘法的Python代码示例: # import numpy as np # import matplotlib.pyplot as plt # # # 定义x和y坐标数据 # x = np.array([1, 2, 3, 4, 5]) # y = np.array([2, 3, 4, 5, 6]) # # # 定义多项式阶数m # m = 2 # # # 构造A矩阵 # A = np.vander(x, m + 1, increasing=True) # # # 计算拟合系数theta # theta = np.linalg.lstsq(A, y, rcond=None)[0] # # # 使用theta和x的幂次计算拟合曲线 # x_fit = np.linspace(x.min(), x.max(), 100) # y_fit = np.polyval(theta[::-1], x_fit) # # # 创建图形对象并绘制拟合曲线和样本点 # fig, ax = plt.subplots() # ax.plot(x_fit, y_fit, label='Fitted curve') # ax.scatter(x, y, label='Data points') # # # 添加标签 # ax.set_xlabel('x') # ax.set_ylabel('y') # ax.set_title('Curve fitting using Least Squares') # # # 显示图形 # plt.legend() # plt.show()