0

I have DataFrame (dfcell) as showed in a sample bellow:

enter image description here

This DataFrame has 104 rows and I need to show in Scatter Plot.

The columns location_cell_lng and location_cell_lat are Cartesian axis and class_m is the target.

I did that

plt.scatter(dfcell['location_cell_lng'], dfcell['location_cell_lat'], c=dfcell['class_m'])

I don't get to put legend to the column class_m that has a range of 0 to 5.

I tried the code in the link bellow, but without success https://matplotlib.org/gallery/lines_bars_and_markers/scatter_with_legend.html#sphx-glr-gallery-lines-bars-and-markers-scatter-with-legend-py

How can I to do this?

1 Answer 1

1

Add label="mylabel" as an argument to plt.scatter then call the plt.legend() method

# 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()
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Patrick, thanks for answer! But I need of different color according the value target (class_m)
I've edited the answer to show how you can color according to the different classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.