Plotting a graph with different marker colors based on attributes

Question:

I have a df that looks like this:

latitude longitude cod_postal
0 40.244350 -8.443650 3025-151
2 40.626130 -8.645250 3810-419
1 40.626130 -8.645250 3810-498
3 40.626130 -8.645250 3810-419

I want to plot a graph with the lat and lon coordinates (a line plot) where the marker face color is different based on the cod_postal label.

I have come up with this:

color_labels=df['cod_postal'].unique()
    
#list of rgb
rgb_values=sns.color_palette('Set2', 8)

#Assign label to a rgb code
color_map=dict(zip(color_labels, rgb_values))

plt.plot(df['latitude'], df['longitude'], marker='o', mfc=df['cod_postal'].map(color_map)) 

But I get an error saying:

ValueError: RGBA sequence should have length 3 or 4

How can I fix this?
I know that if I was plotting a scatter it would work, but for my case, being a line plot is essential.

Thanks in advance!

EDIT:

Adding a reference picture for my expected output.
enter image description here

Asked By: Beatriz Santos

||

Answers:

plt.plot() is expecting a single color for the mfc parameter.

Using marker="o", it looks like you’re trying to create a scatter plot. If it’s the case, you should use plt.scatter. You can now pass multiple colors to the c= parameter (one for each scatter point).

color_labels=df['cod_postal'].unique()
    
#list of rgb
rgb_values=sns.color_palette('Set2', 8)

#Assign label to a rgb code
color_map=dict(zip(color_labels, rgb_values))

fig, ax = plt.subplots()
# Create scatter plot, with one color 'cod_postal'
plt.scatter(df['latitude'], df['longitude'], c=df['cod_postal'].map(color_map).values) 

enter image description here

items 1, 2, and 3 have the same coordinates so they overlap on the figure.


Edit regarding the reference image provided.

I would then suggest you to do that in 2 steps. First create the scatter plot as explained above. Then create a line plot for the connecting black line.

plt.scatter(df['latitude'], df['longitude'], c=df['cod_postal'].map(color_map).values, zorder=3) 
plt.plot(df['latitude'], df['longitude'], c="k") 

I adjusted the dataframe to make the figure a bit more interesting

df = pd.DataFrame([
    [40.244350, -8.443650, "3025-151"],
    [41.244350, -9.443650, "3025-151"],
    [42.244350, -8.443650, "3025-151"],
    [40.626130, -8.645250, "3810-419"],
    [41.626130, -8.645250, "3810-419"],
], columns=["latitude", "longitude", "cod_postal"])

enter image description here

Answered By: thmslmr
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.