plt.close() 或 plt.clf() 对图像没有影响

问题描述

我用 plt.savefig() 保存我的图像。

  1. 但在那之后 plt.close() 不起作用,它也显示了以前的图片。能否请您指出错误
  2. 我无法避免 nx.all_simple_paths(g,source=point1.point,target =point2.point),如果两个点都不在图中。

这是我的views.py

    point1 = get_object_or_404(Point,pk=pk1)
    point2 = get_object_or_404(Point,pk=pk2)
    g = nx.Graph()
    g2 = nx.Graph()
    data = []
    content = {'name':None,'points': None,'sum_line':0,"sum_cable": 0}
    for form in Form61KLS.objects.all():
        g.add_edge(form.point1.point,form.point2.point,weight=form.total_length_line)
        g2.add_edge(form.point1.point,weight=form.total_length_cable)
        g.add_node(form.point1.point,pos=(1,20))
        g.add_node(form.point2.point,20))
       
    path = []
    for p in nx.all_simple_paths(g,target=point2.point):
        path.append(p)
    for p in path:
        path_length = nx.path_weight(g,p,weight='weight')
        path_length1 = nx.path_weight(g2,weight='weight')
        finish_total = copy.deepcopy(content)
        finish_total['name'] = "Варианты:"
        finish_total['points'] = p
        finish_total['sum_line'] = path_length
        finish_total['sum_cable'] = path_length1
        data.append(finish_total)
    for p in nx.all_simple_edge_paths(g,target=point2.point):
        for t in p:
            total = copy.deepcopy(content)
            total['name'] = "Разбивка:"
            total['points'] = t
            path_length = nx.path_weight(g,t,weight='weight')
            path_length1 = nx.path_weight(g2,weight='weight')
            total['sum_line'] = path_length
            total['sum_cable'] = path_length1
            data.append(total)

    pos = nx.spring_layout(g)
    labels = nx.get_edge_attributes(g,'weight')
    nx.draw_networkx_edge_labels(g,pos,edge_labels=labels,font_size=8,font_color='blue',font_family='sans-serif',font_weight='normal',horizontalalignment='center',verticalalignment='center')
    nx.draw(g,pos=pos,node_size=250,node_color='blue',linewidths=1,font_size=7,edge_color='y',with_labels=True)
    if os.path.exists(BASE_DIR + "/mediafiles/files/graph.png"):
        os.remove(BASE_DIR + "/mediafiles/files/graph.png")
    plt.savefig(BASE_DIR + "/mediafiles/files/graph.png")
    plt.clf()
    # plt.close()

    return JsonResponse(data,safe=False)```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)