【发布时间】:2020-06-28 08:07:19
【问题描述】:
这个link 接受的答案解释了如何绘制二元分类的散点图。但没有解释如何更改标记的默认颜色。所以我写了下面给出的代码来改变标记的颜色
import matplotlib.colors as mcolors
plt.figure(num=0, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
x=df.iloc[:,0:1].values
y=df.iloc[:,1:2].values
z=df.iloc[:,2:3].values
l=plt.scatter(x,y, c=z,cmap = mcolors.ListedColormap(["blue", "red"]),marker='+')
plt.xlabel('Exam 1 score',fontsize=14)
plt.ylabel('Exam 2 score',fontsize=14)
# Turn on the minor TICKS, which are required for the minor GRID
plt.minorticks_on()
# Customize the major grid
plt.grid(which='major', linestyle='-', linewidth='0.5', color='black')
# Customize the minor grid
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='blue')
plt.legend((l,l),("Admitted", "Not Admitted"), loc="upper right")
plt.show()
但是现在我尝试将图例添加为plt.legend((l,l),("Admitted", "Not Admitted"), loc="upper right"),结果如图所示。为此,我得到了here 的帮助,他们绘制了多个散点图,但就我而言,我只有一个散点图。
但如上图所示,图例中两个标记的标记颜色相同。所以我的问题是如何通过在散点图中使用 plt.legend()来添加具有不同标记颜色或不同标记的多个图例?
【问题讨论】:
标签: python matplotlib legend scatter-plot