溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python 繪制擬合曲線并加指定點(diǎn)標(biāo)識(shí)的實(shí)現(xiàn)

發(fā)布時(shí)間:2020-09-08 04:23:50 來(lái)源:腳本之家 閱讀:205 作者:ukakasu 欄目:開(kāi)發(fā)技術(shù)

python 繪制擬合曲線并加指定點(diǎn)標(biāo)識(shí)

python 繪制擬合曲線并加指定點(diǎn)標(biāo)識(shí)的實(shí)現(xiàn)

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']

# 擬合函數(shù)
def func(x, a, b):
#  y = a * log(x) + b
  y = x/(a*x+b)
  return y

# 擬合的坐標(biāo)點(diǎn)
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)
# 計(jì)算r2
y2 = x0/(a*x0+b)
#y2 = a * log(x0) + b
r2 = r2_score(y0, y2)  

#plt.figure(figsize=(7.5, 5)) 
# 坐標(biāo)字體大小
plt.tick_params(labelsize=11) 
 # 原數(shù)據(jù)散點(diǎn)
plt.scatter(x0,y0,s=30,marker='o')

# 橫縱坐標(biāo)起止
plt.xlim((0, 50))
plt.ylim((0, round(max(y0))+2))

# 擬合曲線
plt.plot(x1, y1, "blue") 
plt.title("標(biāo)題",fontsize=13) 
plt.xlabel('X(h)',fontsize=12) 
plt.ylabel('Y(%)',fontsize=12) 

# 指定點(diǎn),y=9時(shí)求x
p = round(9*b/(1-9*a),2)
#p = b/(math.log(9/a))
p = round(p, 2)
# 顯示坐標(biāo)點(diǎn)
plt.scatter(p,9,s=20,marker='x')
# 顯示坐標(biāo)點(diǎn)橫線、豎線
plt.vlines(p, 0, 9, colors = "c", linestyles = "dashed")
plt.hlines(9, 0, p, colors = "c", linestyles = "dashed")
# 顯示坐標(biāo)點(diǎn)坐標(biāo)值
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 顯示網(wǎng)格 
# linestyle 設(shè)置線顯示的類(lèi)型(一共四種) 
# color 設(shè)置網(wǎng)格的顏色 
# linewidth 設(shè)置網(wǎng)格的寬度  
plt.grid(True, linestyle = "--", color = "g", linewidth = "0.5")
plt.show()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI