gpt4 book ai didi

python-3.x - 如何在matplotlib中的直方图上绘制cdf

转载 作者:行者123 更新时间:2023-12-02 17:20:38 25 4
gpt4 key购买 nike

我目前有一个脚本,可以在给定的 pandas 系列的情况下绘制相对频率的直方图。代码是:

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()

它生成的图如下所示:

enter image description here

我想要的是用一条显示 cdf 的线覆盖该图,并根据辅助轴绘制。我知道我可以使用以下命令创建累积图:

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()

但是,这是绘制在单独的图表上,而不是绘制在前一个图表上。任何帮助或见解将不胜感激。

最佳答案

也许这个代码片段有帮助:

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

关于python-3.x - 如何在matplotlib中的直方图上绘制cdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43003086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com