Python 从数据帧在散点Matplotlib中创建标签
我有数据帧(dfcell),如下面的示例所示: 这个数据帧有104行,我需要在散点图中显示 列Python 从数据帧在散点Matplotlib中创建标签,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有数据帧(dfcell),如下面的示例所示: 这个数据帧有104行,我需要在散点图中显示 列location\u cell\u lng和location\u cell\u lat是笛卡尔坐标轴,class\u m是目标 是我干的 plt.scatter(dfcell['location_cell_lng'], dfcell['location_cell_lat'], c=dfcell['class_m']) 我不能将图例放在范围为0到5的列class_m中 我尝试了下面链接中的代码,但没有
location\u cell\u lng
和location\u cell\u lat
是笛卡尔坐标轴,class\u m
是目标
是我干的
plt.scatter(dfcell['location_cell_lng'], dfcell['location_cell_lat'], c=dfcell['class_m'])
我不能将图例放在范围为0到5的列class_m中
我尝试了下面链接中的代码,但没有成功
如何执行此操作?将label=“mylabel”
作为参数添加到plt.scatter
然后调用plt.legend()
方法
# dummy data for classes
class_m = [ 0 for x in range(5)] + [ 1 for x in range(5)]
# create dataframe
dfcell = pd.DataFrame({"location_cell_lng":range(10),"location_cell_lat":range(10),"class_m":class_m})
# create a dictionary to color the different classes
color_dict = {0:"red",1:"blue"}
# create column with colors
dfcell["colors"] = dfcell["class_m"].apply(color_dict.get)
# plot
plt.scatter(dfcell['location_cell_lng'], dfcell['location_cell_lat'], c=dfcell['colors'],label="mylabel")
plt.legend()
嗨,帕特里克,谢谢你的回答!但是我需要根据目标值(class_m)使用不同的颜色。我编辑了答案,以显示如何根据不同的类使用颜色。