python读取plt文件吗_用Python读取文件并绘制CDF
為了完整起見(jiàn),您還應(yīng)考慮:重復(fù):您可以在數(shù)據(jù)中多次擁有同一點(diǎn)。
點(diǎn)之間可以有不同的距離
點(diǎn)可以浮動(dòng)
您可以使用numpy.histogram,以這樣的方式設(shè)置箱子邊緣,即每個(gè)箱子只收集一個(gè)點(diǎn)的所有出現(xiàn)。
您應(yīng)該保留density=False,因?yàn)楦鶕?jù)文檔:Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen
您可以規(guī)范化每個(gè)bin中的元素?cái)?shù)除以數(shù)據(jù)大小。import numpy as np
import matplotlib.pyplot as plt
def cdf(data):
data_size=len(data)
# Set bins edges
data_set=sorted(set(data))
bins=np.append(data_set, data_set[-1]+1)
# Use the histogram function to bin the data
counts, bin_edges = np.histogram(data, bins=bins, density=False)
counts=counts.astype(float)/data_size
# Find the cdf
cdf = np.cumsum(counts)
# Plot the cdf
plt.plot(bin_edges[0:-1], cdf,linestyle='--', marker="o", color='b')
plt.ylim((0,1))
plt.ylabel("CDF")
plt.grid(True)
plt.show()
例如,使用以下數(shù)據(jù):#[ 0. 0. 0.1 0.1 0.2 0.2 0.3 0.3 0.4 0.4 0.6 0.8 1. 1.2]
data = np.concatenate((np.arange(0,0.5,0.1),np.arange(0.6,1.4,0.2),np.arange(0,0.5,0.1)))
cdf(data)
你會(huì)得到:
還可以對(duì)cdf進(jìn)行插值以獲得連續(xù)函數(shù)(使用線性插值或三次樣條曲線):import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
def cdf(data):
data_size=len(data)
# Set bins edges
data_set=sorted(set(data))
bins=np.append(data_set, data_set[-1]+1)
# Use the histogram function to bin the data
counts, bin_edges = np.histogram(data, bins=bins, density=False)
counts=counts.astype(float)/data_size
# Find the cdf
cdf = np.cumsum(counts)
x = bin_edges[0:-1]
y = cdf
f = interp1d(x, y)
f2 = interp1d(x, y, kind='cubic')
xnew = np.linspace(0, max(x), num=1000, endpoint=True)
# Plot the cdf
plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
plt.legend(['data', 'linear', 'cubic'], loc='best')
plt.title("Interpolation")
plt.ylim((0,1))
plt.ylabel("CDF")
plt.grid(True)
plt.show()
總結(jié)
以上是生活随笔為你收集整理的python读取plt文件吗_用Python读取文件并绘制CDF的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 准时制 jit 减少库存
- 下一篇: springboot 物联网_Confl