2017-03-24 10 views
1

私は現在、pandasシリーズの場合、相対頻度のヒストグラムをプロットするスクリプトを用意しています。コードは次のとおりです。matplotlibのヒストグラムにcdfをプロットする方法

enter image description here

私がしたいことはCDFを示す線と、このプロットをオーバーレイすることであるが、二次に対してプロット:それはこのようになりますプロットを作成し

def to_percent3(y, position): 
    s = str(100 * y) 
    if matplotlib.rcParams['text.usetex'] is True: 
     return s + r'$\%$' 
    else: 
     return s + '%' 

df = pd.read_csv('mycsv.csv') 

waypointfreq = df['Waypoint Frequency(Secs)'] 
cumfreq = df['Waypoint Frequency(Secs)'] 
perctile = np.percentile(waypointfreq, 95) # claculates 95th percentile 
bins = np.arange(0,perctile+1,1) # creates list increasing by 1 to 96th percentile 
plt.hist(waypointfreq, bins = bins, normed=True) 
formatter = FuncFormatter(to_percent3) #changes y axis to percent 
plt.gca().yaxis.set_major_formatter(formatter) 
plt.axis([0, perctile, 0, 0.03]) #Defines the axis' by the 95th percentile and 10%Relative frequency 
plt.xlabel('Waypoint Frequency(Secs)') 
plt.xticks(np.arange(0, perctile, 15.0)) 
plt.title('Relative Frequency of Average Waypoint Frequency') 
plt.grid(True) 
plt.show() 

軸。私はコマンドを使用して、累積グラフを作成することができることを知っている:

waypointfreq = df['Waypoint Frequency(Secs)'] 
perctile = np.percentile(waypointfreq, 95) # claculates 90th percentile 
bins = np.arange(0,perctile+5,1) # creates list increasing by 2 to 90th percentile 
plt.hist(waypointfreq, bins = bins, normed=True, histtype='stepfilled',cumulative=True) 
formatter = FuncFormatter(to_percent3) #changes y axis to percent 
plt.gca().yaxis.set_major_formatter(formatter) 
plt.axis([0, perctile, 0, 1]) #Defines the axis' by the 90th percentile and 10%Relative frequency 
plt.xlabel('Waypoint Frequency(Secs)') 
plt.xticks(np.arange(0, perctile, 15.0)) 
plt.title('Cumulative Frequency of Average Waypoint Frequency') 
plt.grid(True) 
plt.savefig(r'output\4 Cumulative Frequency of Waypoint Frequency.png', bbox_inches='tight') 
plt.show() 

しかし、これではなく、前の1以上の、別々のグラフ上にプロットされます。助けや洞察力があれば感謝します。

答えて

0

たぶん、このコードスニペットができます:

import numpy as np 
from scipy.integrate import cumtrapz 
from scipy.stats import norm 
from matplotlib import pyplot as plt 

n = 1000 
x = np.linspace(-3,3, n) 
data = norm.rvs(size=n) 
data = data + abs(min(data)) 
data = np.sort(data) 

cdf = cumtrapz(x=x, y=data) 
cdf = cdf/max(cdf) 

fig, ax = plt.subplots(ncols=1) 
ax1 = ax.twinx() 
ax.hist(data, normed=True, histtype='stepfilled', alpha=0.2) 
ax1.plot(data[1:],cdf) 

あなたのCDFが滑らかでない場合は、あなたが

enter image description here

分布を合うことができます
関連する問題