Matlibplot Graph平滑低于零

时间:2017-08-08 07:17:19

标签: python plot histogram smoothing

我试图用值来平滑图形,只是一个简单的线图,但是当平滑时,曲线被拉到零以下,但我希望它都是正面的。这将如何运作?

v只是具有正面价值的任何列表,我不想只是使值为正(abs(bincenters))。

def main(v):
    window = Tk()
    x=np.array ([1,2,3,4,5,6,7,8,9,10])

    p = np.array(v)

    fig = Figure(figsize=(15,7))
    a = fig.add_subplot(111)

    med = np.median(p)
    print(med)

    y,binEdges=np.histogram(p,bins=200)
    bincenters = 0.5*(binEdges[1:]+binEdges[:-1])

    maxi = max(y)

    x_smooth = np.linspace(bincenters.min(), bincenters.max(), len(y)*10)
    y_smooth = spline(bincenters, y, x_smooth)

    a.grid(True)
    a.annotate('Median: %s' %med, xy=(med, 0), xytext=(med + max(v)/15, maxi*0.7),
            arrowprops=dict(arrowstyle="->",
                            connectionstyle="arc3"),)

    a.plot(x_smooth,y_smooth,'-',color="#3F5D7D")

    a.set_title ("Histogram for %s"%product, fontsize=16)

    canvas = FigureCanvasTkAgg(fig, master=window1)
    canvas.get_tk_widget().pack()
    b2 = Button(window1, text='Close', bg="#FAAC58", command= lambda : close(window))
    b2.pack(side=LEFT, padx=5, pady=5)
    canvas.draw()
    window.mainloop()

Result, with negative values

1 个答案:

答案 0 :(得分:1)

您可以将y值置于零:

y_smooth = spline(bincenters, y, x_smooth)
y_smooth = np.where(y_smooth > 0, y_smooth, 0)