Mamed
Mamed

Reputation: 772

How to plot based on values of 2 columns

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:

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

Upvotes: 0

Views: 59

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150735

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

Upvotes: 2

baccandr
baccandr

Reputation: 1130

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))

Upvotes: 1

Related Questions