Python 如何在pyplot中重命名图例

Python 如何在pyplot中重命名图例,python,matplotlib,Python,Matplotlib,如何将此特定图例中的0和1值分别更改为“否”和“是” 导入matplotlib.pyplot作为plt 将numpy作为np导入 plt.contourf(x1,x2,regr.predict(np.array([x1.ravel(),x2.ravel()]).T).重塑(x1.shape),alpha=0.75,cmap=ListedColormap('red','green')) 对于枚举中的i,j(np.unique(y_集)): plt.scatter(x_set[y_set==j,0]

如何将此特定图例中的0和1值分别更改为“否”和“是”

导入matplotlib.pyplot作为plt
将numpy作为np导入
plt.contourf(x1,x2,regr.predict(np.array([x1.ravel(),x2.ravel()]).T).重塑(x1.shape),alpha=0.75,cmap=ListedColormap('red','green'))
对于枚举中的i,j(np.unique(y_集)):
plt.scatter(x_set[y_set==j,0],x_set[y_set==j,1],c=ListedColormap(('black','white'))(i),label=j)
plt.legend()

import matplotlib.patches as mpatches
yes_instance = mpatches.Patch(color='white', label='Yes')
no_instance = mpatches.Patch(color='black', label='No')
plt.legend(handles=[yes_instance, no_instance])
修正了它

import matplotlib.patches as mpatches
yes_instance = mpatches.Patch(color='white', label='Yes')
no_instance = mpatches.Patch(color='black', label='No')
plt.legend(handles=[yes_instance, no_instance])
这是你的建议: 只需确定
plt.scatter(…,label=['No','Yes'])的标签即可

导入matplotlib.pyplot作为plt
将numpy作为np导入
plt.contourf(x1,x2,regr.predict(np.array([x1.ravel(),x2.ravel()]).T).重塑(x1.shape),alpha=0.75,cmap=ListedColormap('red','green'))
对于枚举中的i,j(np.unique(y_集)):
plt.scatter(x_set[y_set==j,0],x_set[y_set==j,1],c=ListedColormap(('black','white'))(i),label=['No','Yes')
plt.legend()
以下是您的建议: 只需确定
plt.scatter(…,label=['No','Yes'])的标签即可

导入matplotlib.pyplot作为plt
将numpy作为np导入
plt.contourf(x1,x2,regr.predict(np.array([x1.ravel(),x2.ravel()]).T).重塑(x1.shape),alpha=0.75,cmap=ListedColormap('red','green'))
对于枚举中的i,j(np.unique(y_集)):
plt.scatter(x_set[y_set==j,0],x_set[y_set==j,1],c=ListedColormap(('black','white'))(i),label=['No','Yes')
plt.legend()

plt.legend([“是”、“否”)
?它说“legend不支持“Y”实例。可以使用代理艺术家。”而图例就消失了。您可以提供用于此绘图的示例数据点吗?@amin数据来源于此:代码解决了您的问题吗?如我所见,我需要完整的先前代码来重新生成x1、x2和y_集。
plt.legend([“是”、“否”)
?它说“legend不支持“y”实例。可以使用代理艺术家。”图例消失了。您可以提供用于此绘图的示例数据点吗?@amin数据来源于此:代码是否解决了您的问题?如我所见,我需要前面的全部代码来重新生成x1、x2和y_集。