我有如下示例所示的DataFrame (dfcell):
这个DataFrame有104行,我需要在散点图中显示。
列location_cell_lng
和location_cell_lat
是笛卡尔坐标轴,class_m
是目标。
我就这么做了
plt.scatter(dfcell['location_cell_lng'], dfcell['location_cell_lat'], c=dfcell['class_m'])
我不能将legend放到范围从0到5的列class_m中。
我尝试了下面的链接中的代码,但https://matplotlib.org/gallery/lines_bars_and_markers/scatter_with_legend.html#sphx-glr-gallery-lines-bars-and-markers-scatter-with-legend-py没有成功
我该怎么做呢?
发布于 2020-04-26 12:35:37
将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()
https://stackoverflow.com/questions/61440782
复制相似问题