0
df = 

     id_easy  latitude  longitude  cluster
0         a1   45.1076     7.4920        0
0         b1   45.1064     7.5013        0
1         c1   44.9498     7.5010        1
1         d1   44.9656     7.5084        1
2         e1   45.0846     7.5277        2
2         f1   45.1650     7.7950        2

I want to visualize:

  • longitude and latitude values of id_easy a1 b1 of cluster 0 in different colors and the rest of the data gray
  • longitude and latitude values of id_easy c1 d1 of cluster 1 in different colors and the rest of the data gray
  • longitude and latitude values of id_easy e1 f1 of cluster 2 in different colors and the rest of the data gray

I have this:

for index_in_red in df.index.unique():
    my_dpi=96
    plt.figure(figsize=(600/my_dpi, 400/my_dpi), dpi=my_dpi)

    plt.plot(df.loc[df.index != index_in_red,'longitude'],df.loc[df.index != index_in_red,'latitude'] ,
             color='silver', marker='o',linestyle='',linewidth=50, markersize=2)

    plt.plot(df.loc[index_in_red,'longitude'],df.loc[index_in_red,'latitude']  ,
              color='maroon',marker='o',linestyle='',linewidth=2, markersize=3)

    plt.show()

But it gives me all values of id_easy in maroon color, but I want them to be in different color

Desired output:

For every values of id_easy I have different color

For every values of id_easy I have different color

2
  • Do you want 3 plots for different clusters? Otherwise how would you see the rest of the data gray? Commented Oct 3, 2019 at 13:23
  • @QuangHoang I added desired output. 3 different plots, different color for every couple of id_easy in given cluster 0 (data of clusters 1 and 2 will be gray) Commented Oct 3, 2019 at 13:32

2 Answers 2

2

This can be done with seaborn:

for cluster in df.cluster.unique():

    # mask the cluster of interest
    is_cluster = df.cluster.eq(cluster)

    # plot the other clusters in gray
    ax = df[~is_cluster].plot.scatter(x='latitude',y='longitude', c='gray')

    # plot the cluster of interest in different colors
    sns.scatterplot(data=df[is_cluster],
                    x='latitude', 
                    y='longitude',
                    hue='id_easy',
                    ax=ax)

    # you can do other stuff with ax here
    # ax.set_title(...)...

    plt.show()

At the end, you will have 3 plots:

enter image description here enter image description here enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Your question is not perfectly clear to me, is this what you are looking for?

enter image description here

in case this is the code I used:

df2=df.set_index('cluster')

fig, axs = plt.subplots(figsize=(10, 12), nrows=3,ncols=1,constrained_layout=True)

for clus in df2.index.unique():
    axs[clus].plot(df2.latitude,df2.longitude,'o',color='grey',markersize=12)
    axs[clus].plot(df2.loc[clus].latitude.iloc[0],df2.loc[clus].longitude.iloc[0],'o',markersize=12)
    axs[clus].plot(df2.loc[clus].latitude.iloc[1],df2.loc[clus].longitude.iloc[1],'o',markersize=12)

    axs[clus].set_xlabel('Latitude')
    axs[clus].set_ylabel('Longitude')
    axs[clus].set_title('Cluster '+str(clus))

1 Comment

Instead of 2 blue dots, I want 2 different colored dots

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.