本文目錄一覽:
- 1、請問用python處理excel數據繪製曲線圖能不能做成一個類似的小軟體?
- 2、如何使用Python繪製光滑實驗數據曲線
- 3、怎樣用python畫對數圖
- 4、Python如何畫函數的曲線
- 5、python 怎麼畫與其他方法進行比較的ROC曲線?
- 6、怎麼用python繪圖
請問用python處理excel數據繪製曲線圖能不能做成一個類似的小軟體?
可以啊,先確定好需要操作excel的哪些動作,用到哪些函數,然後通過python調用excel介面,實現對excel的操作;還有一種是只是讀取excel裡面的數據,然後通過matplotlib等第三方庫,繪製數據曲線圖。
如何使用Python繪製光滑實驗數據曲線
樓主的問題是否是「怎樣描繪出沒有數據點的位置的曲線」,或者是「x在某個位置時,即使沒有數據,我也想知道他的y值是多少,好繪製曲線」。這就是個預測未知數據的問題。
傳統的方法就是回歸,python的scipy可以做。流行一點的就是機器學習,python的scikit-learn可以做。
但問題在於,僅由光強能預測出開路電壓嗎(當然,有可能可以預測。)?就是你的圖1和圖2的曲線都不能說是不可能發生的情況吧,所以想預測開路電壓值還需引入其他影響因子。這樣你才能知道平滑曲線到底應該像圖1還是圖2還是其他樣子。
如果是單因子的話,從散點圖觀察,有點像 y = Alnx + B,用線性回歸模型確定A,B的值就可以通過x預測y的值,從而繪製平滑的曲線了。
怎樣用python畫對數圖
1、用python畫出log1.5(x),log(2x),log(3x)
[python] view plain copy
import numpy as np
import math
import matplotlib.pyplot as plt
x=np.arange(0.05,3,0.05)
y1=[math.log(a,1.5)for a in x]
y2=[math.log(a,2)for a in x]
y3=[math.log(a,3)for a in x]
plot1=plt.plot(x,y1,’-g’,label=”log1.5(x)”)
plot2=plt.plot(x,y2,’-r’,label=”log2(x)”)
plot3=plt.plot(x,y3,’-b’,label=”log3(x)”)
plt.legend(loc=’lower right’)
plt.show()
2、輸出結果
Python如何畫函數的曲線
輸入以下代碼導入我們用到的函數庫。
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(0,5,0.1);
y=np.sin(x);
plt.plot(x,y)
採用剛才代碼後有可能無法顯示下圖,然後在輸入以下代碼就可以了:
plt.show()
python 怎麼畫與其他方法進行比較的ROC曲線?
使用sklearn的一系列方法後可以很方便的繪製處ROC曲線,這裡簡單實現以下。
主要是利用混淆矩陣中的知識作為繪製的數據(如果不是很懂可以先看看這裡的基礎):
tpr(Ture Positive Rate):真陽率 圖像的縱坐標
fpr(False Positive Rate):陽率(偽陽率) 圖像的橫坐標
mean_tpr:累計真陽率求平均值
mean_fpr:累計陽率求平均值
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import StratifiedKFold
iris = datasets.load_iris()
X = iris.data
y = iris.target
X, y = X[y != 2], y[y != 2] # 去掉了label為2,label只能二分,才可以。
n_samples, n_features = X.shape
# 增加雜訊特徵
random_state = np.random.RandomState(0)
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
cv = StratifiedKFold(n_splits=6) #導入該模型,後面將數據劃分6份
classifier = svm.SVC(kernel=’linear’, probability=True,random_state=random_state) # SVC模型 可以換作AdaBoost模型試試
# 畫平均ROC曲線的兩個參數
mean_tpr = 0.0 # 用來記錄畫平均ROC曲線的信息
mean_fpr = np.linspace(0, 1, 100)
cnt = 0
for i, (train, test) in enumerate(cv.split(X,y)): #利用模型劃分數據集和目標變數 為一一對應的下標
cnt +=1
probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test]) # 訓練模型後預測每條樣本得到兩種結果的概率
fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1]) # 該函數得到偽正例、真正例、閾值,這裡只使用前兩個
mean_tpr += np.interp(mean_fpr, fpr, tpr) # 插值函數 interp(x坐標,每次x增加距離,y坐標) 累計每次循環的總值後面求平均值
mean_tpr[0] = 0.0 # 將第一個真正例=0 以0為起點
roc_auc = auc(fpr, tpr) # 求auc面積
plt.plot(fpr, tpr, lw=1, label=’ROC fold {0:.2f} (area = {1:.2f})’.format(i, roc_auc)) # 畫出當前分割數據的ROC曲線
plt.plot([0, 1], [0, 1], ‘–‘, color=(0.6, 0.6, 0.6), label=’Luck’) # 畫對角線
mean_tpr /= cnt # 求數組的平均值
mean_tpr[-1] = 1.0 # 坐標最後一個點為(1,1) 以1為終點
mean_auc = auc(mean_fpr, mean_tpr)
plt.plot(mean_fpr, mean_tpr, ‘k–‘,label=’Mean ROC (area = {0:.2f})’.format(mean_auc), lw=2)
plt.xlim([-0.05, 1.05]) # 設置x、y軸的上下限,設置寬一點,以免和邊緣重合,可以更好的觀察圖像的整體
plt.ylim([-0.05, 1.05])
plt.xlabel(‘False Positive Rate’)
plt.ylabel(‘True Positive Rate’) # 可以使用中文,但需要導入一些庫即字體
plt.title(‘Receiver operating characteristic example’)
plt.legend(loc=”lower right”)
plt.show()
怎麼用python繪圖
你可以使用numpy和matplotlab這兩個庫來實現的你功能。
你的圖可以參考:
import matplotlib
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
def to_percent(y, position):
# Ignore the passed in position. This has the effect of scaling the default
# tick locations.
s = str(100 * y)
# The percent symbol needs escaping in latex
if matplotlib.rcParams[‘text.usetex’] == True:
return s + r’$\%$’
else:
return s + ‘%’
x = randn(5000)
# Make a normed histogram. It’ll be multiplied by 100 later.
plt.hist(x, bins=50, normed=True)
# Create the formatter using the function to_percent. This multiplies all the
# default labels by 100, making them all percentages
formatter = FuncFormatter(to_percent)
# Set the formatter
plt.gca().yaxis.set_major_formatter(formatter)
plt.show()
最主要的就是x軸和y軸的處理,我按照對數算了一下你提供的數據,好像和這個圖效果不一樣。
如果解決了您的問題請採納!
如果未解決請繼續追問
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/151686.html