如何在matplotlib

时间:2017-03-24 15:13:21

标签: python-3.x matplotlib

我目前有一个脚本,它将绘制一个相对频率的直方图,给出一个熊猫系列。代码是:

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

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

1 个答案:

答案 0 :(得分:2)

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

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