简体   繁体   English

Python matplotlib绘制新的一行后保留上一行

[英]Python matplotlib keeps the previous line after plotting new one

The method that I have been using to plot the lines are as following: 我一直在绘制线条的方法如下:

def scatter_plot_with_correlation_line(x, y, graph_filepath):
    plt.scatter(x, y)
    axes = plt.gca()
    m, b = np.polyfit(x, y, 1)
    X_plot = np.linspace(axes.get_xlim()[0],axes.get_xlim()[1],100)
    plt.plot(X_plot, m*X_plot + b, '-')
    plt.savefig(graph_filepath, dpi=300, format='png', bbox_inches='tight')

The first plot looks fine: 第一个图看起来不错:

在此处输入图片说明

Now in the second plot the previous line is still visible: 现在在第二个图中,前一行仍然可见: 在此处输入图片说明

Since I am using the scatter_plot_with_correlation_line() in a loop the results get worse with every iteration. 由于我在循环中使用scatter_plot_with_correlation_line(),因此每次迭代的结果都会变得更糟。

The following plot is after 10th iteration. 下图是第10次迭代后的结果。 在此处输入图片说明

How can I remove the previous line plotted from the new ones ? 如何从新行中删除前一条行?

Do you want to remove the scatter plot and the line, and then replot them both? 您是否要删除散点图和直线,然后重新绘制它们? if so, you could simply clear the current axes at the beginning of your function using plt.gca().cla() 如果是这样,您可以使用plt.gca().cla()清除函数开头的当前轴plt.gca().cla()

def scatter_plot_with_correlation_line(x, y, graph_filepath):
    plt.gca().cla()
    plt.scatter(x, y)
    axes = plt.gca()
    m, b = np.polyfit(x, y, 1)
    X_plot = np.linspace(axes.get_xlim()[0],axes.get_xlim()[1],100)
    plt.plot(X_plot, m*X_plot + b, '-')
    plt.savefig(graph_filepath, dpi=300, format='png', bbox_inches='tight')

If you only want to remove the line, and retain the previously plotted scatter points, then you could grab a reference to the line2D object as you plot it, and then remove it later: 如果只想删除直线,并保留先前绘制的散点,则可以在绘制它时获取对line2D对象的引用,然后稍后将其删除:

def scatter_plot_with_correlation_line(x, y, graph_filepath):
    plt.scatter(x, y)
    axes = plt.gca()
    m, b = np.polyfit(x, y, 1)
    X_plot = np.linspace(axes.get_xlim()[0],axes.get_xlim()[1],100)

    # Store reference to correlation line. note the comma after corr_line
    corr_line, = plt.plot(X_plot, m*X_plot + b, '-')
    plt.savefig(graph_filepath, dpi=300, format='png', bbox_inches='tight')

    # remove the correlation line after saving the figure, ready for the next iteration
    corr_line.remove()

Try plt.clear() in the beginning or the end of your function. 在函数的开头或结尾尝试plt.clear()。

If it does not work correct in your way: Try to split the scatter plot from the line plot and clear only the line plot. 如果按您的方式无法正常工作:尝试将散点图与折线图分开,仅清除折线图。 ;) ;)

Use a new figure 使用新身材

def scatter_plot_with_correlation_line(...):
    ######################
    f, ax = plt.subplots()
    ######################
    ax.scatter(...)
    ax.plot(...)
    f.savefig(...)

another possibility consists in clearing the lines already drawn from your axis 另一种可能性是清除已经从轴上绘制的线

    ...
    axes = plt.gca()
    axes.lines = []
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM