Matplotlib: Add color legend to scatter plot

丶灬走出姿态 提交于 2021-01-29 14:21:35

问题


Have table as:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

list_1=[['AU',152,474.0],
        ['CA',440,482.0],
       ['DE',250,564.0,],
       ['ES',707,549.0,],
       ['FR',1435,551.0,],
       ['GB',731,555.0,],
       ['IT',979,600.0,],
       ['NDF',45041,357.0,],
       ['NL',247,542.0,],
       ['PT',83,462.0,],
       ['US',20095,513.0,],
       ['other',3655,526.0,]]
labels=['country_destination','num_users','avg_hours_spend']
df=pd.DataFrame(list_1,columns=labels)
df=df.set_index('country_destination')
df

country_destination num_users   avg_hours_spend 
AU                     152        474.0
CA                     440        482.0
DE                     250        564.0
ES                     707        549.0
FR                     1435       551.0
GB                     731        555.0
IT                     979        600.0
NDF                    45041      357.0
NL                     247        542.0
PT                     83         462.0
US                     20095      513.0
other                  3655       526.0

I need to make scatter plot:

y = df['avg_hours_spend']
x = df['num_users']
N=12
colors = np.random.rand(N)
plt.scatter(x, y,c=colors)

plt.title('Web Sessions Data of Users')
plt.xlabel('No.Of.Users')
plt.ylabel('Mean Hours Users Spends on the Website')
plt.legend()
plt.show()

Scatter plot where each color is different country

Needed: I want to make big circles and add legend in the right side when for each country will be different color. How ?


回答1:


In matplotlib, you can add a different scatter point for each country (i.e. each level of your dataframe's index), and set the s argument to whatever you want (since you want larger points, I added s=100:

for i, row in df.iterrows():
    plt.scatter(x=row.num_users, y=row.avg_hours_spend, label=i, s=100)

plt.title("Web Sessions Data of Users")
plt.xlabel("No.Of.Users")
plt.ylabel("Mean Hours Users Spends on the Website")
plt.legend()
plt.show()

You can achieve a similar result with a different syntax with seaborn:

import seaborn as sns

ax = sns.scatterplot(
    x="num_users",
    y="avg_hours_spend",
    hue="country_destination",
    s=100,
    data=df.reset_index(),
)

ax.set_title("Web Sessions Data of Users")
ax.set_xlabel("No.Of.Users")
ax.set_ylabel("Mean Hours Users Spends on the Website")



来源:https://stackoverflow.com/questions/60199067/matplotlib-add-color-legend-to-scatter-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!