1. 程式人生 > >Python/Matplotlib光滑曲線畫圖---Scipy庫函式使用

Python/Matplotlib光滑曲線畫圖---Scipy庫函式使用

Spline interpolation requires two essential steps: (1) a spline representation of the curve is computed, and (2) the spline is evaluated at the desired points. In order to find the spline representation, there are two different ways to represent a curve and obtain (smoothing) spline coefficients: directly and parametrically. The direct method finds the spline representation of a curve in a two- dimensional plane using the function 

splrep. The first two arguments are the only ones required, and these provide the xx and yy components of the curve. The normal output is a 3-tuple, (t,c,k)(t,c,k) , containing the knot-points, tt , the coefficients cc and the order kk of the spline. The default spline order is cubic, but this can be changed with the input keyword, k.

For curves in NN -dimensional space the function splprep allows defining the curve parametrically. For this function only 1 input argument is required. This input is a list of NN -arrays representing the curve in NN -dimensional space. The length of each array is the number of curve points, and each array provides one component of the N

N -dimensional data point. The parameter variable is given with the keyword argument, u, which defaults to an equally-spaced monotonic sequence between 00 and 11 . The default output consists of two objects: a 3-tuple, (t,c,k)(t,c,k) , containing the spline representation and the parameter variable u.u.

The keyword argument, s , is used to specify the amount of smoothing to perform during the spline fit. The default value of ss is s=m2ms=m−2m where mm is the number of data-points being fit. Therefore, if no smoothing is desired a value of s=0s=0 should be passed to the routines.

Once the spline representation of the data has been determined, functions are available for evaluating the spline (splev) and its derivatives (splevspalde) at any point and the integral of the spline between any two points ( splint). In addition, for cubic splines ( k=3k=3 ) with 8 or more knots, the roots of the spline can be estimated ( sproot). These functions are demonstrated in the example that follows.

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from scipy import interpolate

Cubic-spline

>>> x = np.arange(0, 2*np.pi+np.pi/4, 2*np.pi/8)
>>> y = np.sin(x)
>>> tck = interpolate.splrep(x, y, s=0)
>>> xnew = np.arange(0, 2*np.pi, np.pi/50)
>>> ynew = interpolate.splev(xnew, tck, der=0)
>>> plt.figure()
>>> plt.plot(x, y, 'x', xnew, ynew, xnew, np.sin(xnew), x, y, 'b')
>>> plt.legend(['Linear', 'Cubic Spline', 'True'])
>>> plt.axis([-0.05, 6.33, -1.05, 1.05])
>>> plt.title('Cubic-spline interpolation')
>>> plt.show()
../_images/interpolate-3_00_00.png

Derivative of spline

>>> yder = interpolate.splev(xnew, tck, der=1)
>>> plt.figure()
>>> plt.plot(xnew, yder, xnew, np.cos(xnew),'--')
>>> plt.legend(['Cubic Spline', 'True'])
>>> plt.axis([-0.05, 6.33, -1.05, 1.05])
>>> plt.title('Derivative estimation from spline')
>>> plt.show()
../_images/interpolate-3_01_00.png

Integral of spline

>>> def integ(x, tck, constant=-1):
...     x = np.atleast_1d(x)
...     out = np.zeros(x.shape, dtype=x.dtype)
...     for n in range(len(out)):
...         out[n] = interpolate.splint(0, x[n], tck)
...     out += constant
...     return out
>>> yint = integ(xnew, tck)
>>> plt.figure()
>>> plt.plot(xnew, yint, xnew, -np.cos(xnew), '--')
>>> plt.legend(['Cubic Spline', 'True'])
>>> plt.axis([-0.05, 6.33, -1.05, 1.05])
>>> plt.title('Integral estimation from spline')
>>> plt.show()
../_images/interpolate-3_02_00.png

Roots of spline

>>> interpolate.sproot(tck)
array([3.1416])

Notice that sproot failed to find an obvious solution at the edge of the approximation interval, 

相關推薦

Python/Matplotlib光滑曲線畫圖---Scipy函式使用

Spline interpolation requires two essential steps: (1) a spline representation of the curve is computed, and (2) the spline is evaluated at the desired

python進階】自定義函式

在python使用過程中,在不同的專案裡,常常會出現頻繁地自定義同一個函式的情況。為了解決這個問題,我們可以選擇建立一個自定義的庫,並將其新增到系統路徑中 具體操作如下: 在任意位置新建一個專案my_libs,並在其中新建python檔案my_lib1和m

Python】Windows下安裝scipy步驟

概述 由於學習需要,需要安裝scipy庫。scipy庫在Windows下使用pip安裝失敗,所以需要尋找安裝包進行安裝,下面是記錄步驟。 開發環境 win10 x64 Python 3.5.1 安裝scipy步驟 首先到這裡下載相關擴充套件

[python]pycharm畫圖插件matplotlib、numpy、scipy的下載與安裝

pytho 插件 免費 matplot RM AR ID sdn baidu 最近在用pycharm學習python語言,不得不感嘆python語言的強大與人性化! 但對於使用pycharm畫圖(較復雜的圖)就要用到幾個插件了,即matplotlib、numpy和scipy

python開發學習記錄--Numpy、ScipyMatplotlib、Scikit-learn等的安裝

其實很簡單,在git bash中,輸入: [email protected] MINGW64 /d/SoftWare/Python/Python37/Scripts $ pip install numpy $ pip install matplotlib 看到Successful

Python--Matplotlib函式文件API

在此將Python中Matplotlib的庫函式進行總結,方便大家的學習查閱。 本文轉自https://www.cnblogs.com/TensorSense/p/6795995.html plt.savefig(‘test’, d

python--matplotlib使用2

.org 屬性 from ria ont 大小 itl 詳細 utf 功能:python畫2D圖 直接上代碼,註釋很詳細! 1 #-*- coding:utf-8 -*- 2 3 from numpy import * 4 import matplotlib.p

Python數據可視化-Matplotlib

img use class 一個 pri style randint degree spl 折線圖繪制: import pandas as pd unrate = pd.read_csv(‘unrate.csv‘) unrate[‘DATE‘] = pd.to_date

python中的turtle函式簡單使用

      參考案例: import turtle d=0 for i in range(4): turtle.fd(200) #或者寫成turtle.forward(200) d =d+90

Ubuntu14.04 Python 2.7 安裝scipy

Ubuntu14.04 Python 2.7 安裝scipy 庫各種問題 Ubuntu14.04 Python 2.7 安裝numpy 和scipy 庫時,測試numpy 沒有問題,測試scipy 出現問題。 按照下面幾個連結進行了調整: ubuntu 14.04配置python安裝第三

使用Python matplotlib做動態曲線

point clas sin 參考 fig 如何使用 tput 運行圖 href 今天看到“Python實時監控CPU使用率”的教程: https://www.w3cschool.cn/python3/python3-ja3d2z2g.html 自己也學習如何使用Pytho

Requests函式的學習(玩轉python網路爬蟲)

一、請求方式 HTTP常用的請求方式是GET和POST,Requests對此區分兩種不同的請求方式。 (1)GET請求 Requests的GET請求分為兩種:不帶引數和帶引數。判斷URL是否帶有引數,通過對“?”進行判斷,“?”表示帶有引數。 import requests # 第一

Python視覺化中的Matplotlib繪圖(1.畫圖,網格,子圖,畫正餘弦圖,座標軸界限,畫圓,)

1.一張基本的圖示包含的元素;    · x軸和y軸 以及他們的刻度線 、標籤、繪圖區域 import matplotlib.pyplot as plt # 匯入繪圖模組 import numpy as np # 匯入需要生成資料的num

Matplotlib畫圖----資訊熵函式影象

最近在看GAN和VAE的過程中,發現資訊熵的概念多次出現,就又複習了一下。為了更形象的瞭解資訊熵,在網上找資訊熵函式影象的過程中,發現只有二元的資訊熵影象,出於好奇,畫了就畫了一下三元的。 E

python函式畫圖以及匿名函式lambda使用.md

python隱函式畫圖以及匿名函式lambda使用 一.匿名函式lambda使用 因為一會畫隱函式畫圖想用一下lambda匿名函式,所以就在這裡學習一下其用法,本質上來講lambda就是把函式換了中說法,其應用場景可以用在一些簡單函式的定義上,比如你想定義一個比較大小的函式,而該

python: matplotlib常用畫圖流程及命令

        在matplot中,整幅影象為一個figure物件,在figure物件中包含一個或者多個axes物件,,每個axes物件都是一個擁有自己座標系統的繪圖區域。關係如下: 圖形的組成結構如下: 畫圖流程: 開始->

Python查詢函式文件

python強大的一個原因就是有豐富的第三方庫可以使用,省去了我們造輪子的精力,將注意力主要放在處理問題上。但是有時想要檢視所引用的庫都有哪些方法,具體引數該怎麼用時,來回百度,卻不一定能找到詳細的文件。 其實python提供了一個檢視本地文件的方法。通過pyt

python matplotlib 雙Y軸 以及 曲線標誌位置

import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 5, 0.1) # 生成一個numpy陣列物件,0到5,間隔為0.1 y1 = np.sin(x) y2 = np.cos(x) p

Python畫ROC曲線 matplotlib 顏色、標記、線條引數控制

在分類模型中,ROC曲線和AUC值經常作為衡量一個模型擬合程度的指標。最近在建模過程中需要作出模型的ROC曲線,參考了sklearn官網的教程和部落格。現在將自己的學習過程總結如下,希望對初次接觸的同學有所幫助。PS:網上的例子實在是晦澀難懂,在折騰了一下午之後

python 中opencv, PIL, skimage, scipy matplotlib中的細節說明

0. 寫作目的 好記性不如爛筆頭。、 1. 有關size返回的說明 tensorflow中需要的tensor型別為: (N, H, W, C)其中N是batch 大小,H是height, W是wi