python 绘制拟合曲线并加指定点标识的实现
作者:ukakasu 时间:2023-07-25 20:29:51
python 绘制拟合曲线并加指定点标识
import os
import numpy as np
from scipy import log
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import math
from sklearn.metrics import r2_score
# 字体
plt.rcParams['font.sans-serif']=['SimHei']
# 拟合函数
def func(x, a, b):
# y = a * log(x) + b
y = x/(a*x+b)
return y
# 拟合的坐标点
x0 = [2, 4, 8, 10, 24, 28, 32, 48]
y0 = [6.66,8.35,10.81,11.55,13.63,13.68,13.69,13.67]
# 拟合,可选择不同的method
result = curve_fit(func, x0, y0,method='trf')
a, b = result[0]
# 绘制拟合曲线用
x1 = np.arange(2, 48, 0.1)
#y1 = a * log(x1) + b
y1 = x1/(a*x1+b)
x0 = np.array(x0)
y0 = np.array(y0)
# 计算r2
y2 = x0/(a*x0+b)
#y2 = a * log(x0) + b
r2 = r2_score(y0, y2)
#plt.figure(figsize=(7.5, 5))
# 坐标字体大小
plt.tick_params(labelsize=11)
# 原数据散点
plt.scatter(x0,y0,s=30,marker='o')
# 横纵坐标起止
plt.xlim((0, 50))
plt.ylim((0, round(max(y0))+2))
# 拟合曲线
plt.plot(x1, y1, "blue")
plt.title("标题",fontsize=13)
plt.xlabel('X(h)',fontsize=12)
plt.ylabel('Y(%)',fontsize=12)
# 指定点,y=9时求x
p = round(9*b/(1-9*a),2)
#p = b/(math.log(9/a))
p = round(p, 2)
# 显示坐标点
plt.scatter(p,9,s=20,marker='x')
# 显示坐标点横线、竖线
plt.vlines(p, 0, 9, colors = "c", linestyles = "dashed")
plt.hlines(9, 0, p, colors = "c", linestyles = "dashed")
# 显示坐标点坐标值
plt.text(p, 9, (float('%.2f'% p),9),ha='left', va='top', fontsize=11)
# 显示公式
m = round(max(y0)/10,1)
print(m)
plt.text(48, m, 'y= x/('+str(round(a,2))+'*x+'+str(round(b,2))+')', ha='right',fontsize=12)
plt.text(48, m, r'$R^2=$'+str(round(r2,3)), ha='right', va='top',fontsize=12)
# True 显示网格
# linestyle 设置线显示的类型(一共四种)
# color 设置网格的颜色
# linewidth 设置网格的宽度
plt.grid(True, linestyle = "--", color = "g", linewidth = "0.5")
plt.show()
来源:https://blog.csdn.net/ukakasu/article/details/81502096
标签:python,曲线,标识
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python使用技巧-查找文件
2021-11-10 18:48:21
python脚本实现验证码识别
2022-02-03 09:41:30
![](https://img.aspxhome.com/file/2023/4/79664_0s.jpg)
python绘制棉棒图的方法详解
2021-11-06 06:24:58
![](https://img.aspxhome.com/file/2023/8/107818_0s.png)
手把手教你安装Windows版本的Tensorflow
2021-07-12 07:03:15
![](https://img.aspxhome.com/file/2023/6/84066_0s.png)
node.js中的fs.realpathSync方法使用说明
2024-05-13 09:59:04
python中split()函数的用法详解
2022-01-13 14:52:06
![](https://img.aspxhome.com/file/2023/0/134660_0s.png)
利用Python爬虫给孩子起个好名字
2022-04-25 23:40:29
![](https://img.aspxhome.com/file/2023/2/69422_0s.png)
Python基础之tkinter图形化界面学习
2023-09-03 00:47:06
JS实战篇之收缩菜单表单布局
2024-04-18 09:47:43
DIV+CSS高度自适应网页代码实例
2008-09-20 08:00:00
![](https://img.aspxhome.com/file/UploadPic/20089/20/20089208546752s.jpg)
php生成curl命令行的方法
2023-07-23 22:19:42
Python可视化工具如何实现动态图表
2023-05-22 19:21:27
![](https://img.aspxhome.com/file/2023/0/81710_0s.gif)
使用git config --global设置用户名和邮件问题
2022-02-14 11:01:08
![](https://img.aspxhome.com/file/2023/3/132293_0s.png)
微信小程序实现搜索框功能
2024-04-16 10:31:05
![](https://img.aspxhome.com/file/2023/4/136904_0s.jpg)
JS+DIV实现拖动效果
2023-07-02 05:19:37
![](https://img.aspxhome.com/file/2023/4/137004_0s.gif)
python实现线程池的方法
2023-03-10 14:08:06
Python 将 QQ 好友头像生成祝福语的实现代码
2022-01-11 20:29:33
![](https://img.aspxhome.com/file/2023/2/104922_0s.png)
python入门之井字棋小游戏
2021-12-11 11:35:36
![](https://img.aspxhome.com/file/2023/2/123632_0s.jpg)
完全卸载mysql(停止服务、卸载相关程序、删除注册表
2024-01-24 12:53:40
![](https://img.aspxhome.com/file/2023/0/115330_0s.jpg)
虚拟环境下搭建一个Django项目
2023-06-15 12:30:39
![](https://img.aspxhome.com/file/2023/8/61168_0s.png)