python畫曲線圖
① 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 turtle畫斐波那契螺旋曲線
我把矩形和圓弧都用不同顏色填充了,你按照自己的需求修改一下吧,你的題目說的不清楚。
#Python3.6
#使用turtle繪制Fibonacci螺旋
defdraw_fibonacci(x):
#F0=1
#F1=1
#Fn=F(n-1)+F(n-2)
#產生斐波那契數列,用於查表
#像這種計算復雜性指數增長的計算,不要寫個函數去每次求一個數
#最好的辦法是,按照規律寫出查找表,用查找的方法來得到數據
f_list=[]
foriinrange(x):
ifi==0:
f_list.append(1)
elifi==1:
f_list.append(1)
else:
f_list.append(f_list[i-1]+f_list[i-2])
#像素比例
f0=50
#設置畫筆屬性
turtle.pensize(5)
turtle.pencolor("black")
turtle.penup()
turtle.home()
turtle.pendown()
foriinrange(0,len(f_list)):
#繪制速度,1~10個不同速度等級,小於1或者大於10立即繪制
turtle.speed(1)
turtle.pendown()
#繪制矩形
ifi==0:
fill_color="black"
else:
fill_color=(random.random(),random.random(),random.random())
turtle.fillcolor(fill_color)
turtle.begin_fill()
turtle.forward(f_list[i]*f0)
turtle.left(90)
turtle.forward(f_list[i]*f0)
turtle.left(90)
turtle.forward(f_list[i]*f0)
turtle.left(90)
turtle.forward(f_list[i]*f0)
turtle.left(90)
turtle.end_fill()
#繪制圓弧
fill_color=(random.random(),random.random(),random.random())
turtle.fillcolor(fill_color)
ifi==0:
turtle.forward(f_list[i]*f0/2)
turtle.begin_fill()
turtle.circle(f_list[i]*f0/2,360)
turtle.end_fill()
#移動到一下起點
turtle.forward(f_list[i]*f0/2)
continue
else:
turtle.begin_fill()
turtle.circle(f_list[i]*f0,90)
turtle.left(90)
turtle.forward(f_list[i]*f0)
turtle.left(90)
turtle.forward(f_list[i]*f0)
turtle.end_fill()
#移動到一下起點
turtle.speed(0)
turtle.penup()
turtle.left(90)
turtle.forward(f_list[i]*f0)
turtle.left(90)
turtle.forward(f_list[i]*f0)
turtle.done()
if__name__=="__main__":
draw_fibonacci(6)
下面是我跑出來的結果:
④ python如何對曲線圖進行延伸
用pylab模塊的plot函數pylab.plot(x,y)其中xy都是數組就能畫出以x,y中元素為坐標的折線圖。
⑤ python怎麼畫曲線圖
# encoding=utf-8
import matplotlib.pyplot as plt
from pylab import * #支持中文
mpl.rcParams['font.sans-serif'] = ['SimHei']
names = ['5', '10', '15', '20', '25']
x = range(len(names))
y = [0.855, 0.84, 0.835, 0.815, 0.81]
y1=[0.86,0.85,0.853,0.849,0.83]
#plt.plot(x, y, 'ro-')
#plt.plot(x, y1, 'bo-')
#pl.xlim(-1, 11) # 限定橫軸的范圍
#pl.ylim(-1, 110) # 限定縱軸的范圍
plt.plot(x, y, marker='o', mec='r', mfc='w',label=u'y=x^2曲線圖')
plt.plot(x, y1, marker='*', ms=10,label=u'y=x^3曲線圖')
plt.legend() # 讓圖例生效
plt.xticks(x, names, rotation=45)
plt.margins(0)
plt.subplots_adjust(bottom=0.15)
plt.xlabel(u"time(s)鄰居") #X軸標簽
plt.ylabel("RMSE") #Y軸標簽
plt.title("A simple plot") #標題
plt.show()
⑥ python Turtle如何繪制任意弧度的曲線
要畫弧線自然需要用到正餘弦函數
⑦ 請問用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怎麼畫曲線
打開Python,使用import導入numpy和matplotlib.pyplot模塊。輸入函數數據,然後使用plt.show()展示繪制的圖像即可。
⑩ python怎麼畫折線圖
一、環境准備
linux ubuntu 下需安裝下面三個包:
Numpy, Scipy,Matplotlib
分別輸入下面的代碼進行安裝:
[plain]view plain
pipinstallnumpy
pipinstallscipy
sudoapt-getinstallpython-matplotlib
python
>>>importpylab
importnumpyasnp
importmatplotlib.pyplotasplt
x=[0,1]
y=[0,1]
plt.figure()
plt.plot(x,y)
plt.savefig("easyplot.jpg")
測試是否安裝成功
[html]view plain
如果沒有報錯則安裝成功
二、開始畫圖
1. 畫最簡單的直線圖
代碼如下:
[python]view plain
結果如下: