python-3.x 导入错误:无法从"matplotlib.patches"导入名称"Patches"

2uluyalo  于 2023-02-01  发布在  Python
关注(0)|答案(1)|浏览(236)

我尝试播放视频中的图,但收到以下错误-ImportError:无法从“matplotlib. patches”导入名称“Patches”
ImportError
我的代码:

from colour import Color
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.patches as mpatches
from matplotlib.patches import Patches as mpatches

plt.figure(figsize=(12,5))
plt.scatter(X, Y, color='blue')

#DC
plt.scatter(X[0], Y[0],color='red', marker='D')
plt.annotate("DC",(X[0]-1,Y[0]-5.5))

#imprimir as rotas
for r in range(len(rotas)):
    for n in range(len(rotas[r])-1):
        i = rotas[r][n]
        j = rotas[r][n+1]
        plt.plot([X[i],X[j]], [Y[i],Y[j]], color=Color(r), alpha=0.4)
    
for r in range(len(temp_acum)):
for n in range(len(temp_acum[r])):
    i = rotas[r][n]
    plt.annotate('$q_{%d}=%d$ |$t_{%d}=%d$'  %(i, q[i], i, temp_acum[r][n]), (X[i]+1,Y[i]))
    
patch = [mpatches.Patch(color=Color(n), label="Veiculo "+str(truck[n])+"|cap="+str(Q[truck[n]])) for n in range(len(truck))]
plt.legend(handles=patch,loc='best')
plt.xlabel('Distância X')
plt.ylabel('Distância Y')
plt.title('CVRPTW | Problema de Roteamento de Veículos com Janela de Tempos')
     
plt.show()
ar7v8xwq

ar7v8xwq1#

正如@马特在评论中所说:
是的,应该是

from matplotlib.patches import Patch as mpatch

它是Patch而不是Patches
链接到文档https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Patch.html

相关问题