绘制CDF的平滑曲线

时间:2018-11-13 13:58:57

标签: python

我正在尝试绘制两个df的CDF。我正在使用以下代码

public function scopeWithPoints(Builder $builder, array $years = [])
{
    return $builder->withCount([
        'attendedCourses as points' => function($query) use($years) {
            $query->select(DB::raw('COALESCE(SUM(courses.points), 0)'));

            if(!empty($years)) {
                $query->whereIn(DB::raw('YEAR(courses.end_date)'), $years);
            }
        }
    ]);
}

plt.show()

我得到这张图:

enter image description here

此图似乎是正确的,但是,我想在末端没有垂直90度线的情况下得到它,又如何将y轴切为1?

谢谢

1 个答案:

答案 0 :(得分:0)

摆脱最后掉到零的路径就像不使用pyplot.hist一样简单。相反,我将使用numpy.histogram并生成我自己的CDF点,以使用pyplot.plot生成简单的折线图:

#generate bins and bin edges
A, Aedges = np.histogram(df['A'], bins = 1000)
B, Bedges = np.histogram(df['B'], bins = 1000)
#cumsum and normalize to get cdf rather than pdf
A = np.cumsum(A)
A /= A[-1]
B = np.cumsum(B)
B /= B[-1]
#convert bin edges to bin centers
Acenters = (Aedges[:-1]+Aedges[1:])/2
Bcenters = (Bedges[:-1]+Bedges[1:])/2
#plot a simple line plot instead of a histogram
plt.plot(A, Acenters)
plt.plot(B, Bcenters)

如果您要自定义情节周围的刺/框:这是一个示例,我复制自matplotlib cheat sheet,我一直这样做:

隐藏顶部和右侧棘

x = np.linspace(-np.pi, np.pi, 800)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y, label='Sine', color='red')
ax.set_axis_bgcolor('#e5e5e5')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_position(
('outward',10))
ax.spines['bottom'].set_position(
('outward',10))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# do the ax.grid() after setting ticks
ax.grid(b=True, which='both',
color='white', linestyle='-',
linewidth=1.5)
ax.set_axisbelow(True)
ax.legend(loc='best', frameon=False)
fig.savefig('filename.png', dpi=125)

enter image description here

如果您使用的是pyplot而不是专用的图形/轴组合,则可以使用axplt获得plt.gca()