零以下的 Matlibplot 图平滑

Matlibplot Graph Smoothing below zero

我正在尝试用值平滑图表,只是一个简单的线图,但是在平滑时,曲线被拉到零以下,但我希望它全部为正。这将如何运作?

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

您可以将 y 值下限为零:

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