简体   繁体   中英

Why does setting hue in seaborn plot change the size of a point?

The plot I am trying to make needs to achieve 3 things.

  1. If a quiz is taken on the same day with the same score, that point needs to be bigger.
  2. If two quiz scores overlap there needs to be some jitter so we can see all points.
  3. Each quiz needs to have its own color

Here is how I am going about it.

 import seaborn as sns
 import pandas as pd
 data = {'Quiz': [1, 1, 2, 1, 2, 1],
         'Score': [7.5, 5.0, 10, 10, 10, 10],
         'Day': [2, 5, 5, 5, 11, 11],
         'Size': [115, 115, 115, 115, 115, 355]}

 df = pd.DataFrame.from_dict(data)

sns.lmplot(x = 'Day', y='Score', data = df, fit_reg=False, x_jitter = True, scatter_kws={'s': df.Size})
plt.show()

在此处输入图片说明

Setting the hue, which almost does everything I need, results in this.

 import seaborn as sns
 import pandas as pd
 data = {'Quiz': [1, 1, 2, 1, 2, 1],
         'Score': [7.5, 5.0, 10, 10, 10, 10],
         'Day': [2, 5, 5, 5, 11, 11],
         'Size': [115, 115, 115, 115, 115, 355]}

 df = pd.DataFrame.from_dict(data)

sns.lmplot(x = 'Day', y='Score', data = df, fit_reg=False, hue = 'Quiz', x_jitter = True, scatter_kws={'s': df.Size})
plt.show()

在此处输入图片说明

Is there a way I can have hue while keeping the size of my points?

It doesn't work because when you are using hue , seaborn does two separate scatterplots and therefore the size argument you are passing using scatter_kws= no longer aligns with the content of the dataframe.

You can recreate the same effect by hand however:

x_col = 'Day'
y_col = 'Score'
hue_col = 'Quiz'
size_col = 'Size'
jitter=0.2

fig, ax = plt.subplots()
for q,temp in df.groupby(hue_col):
    n = len(temp[x_col])
    x = temp[x_col]+np.random.normal(scale=0.2, size=(n,))
    ax.scatter(x,temp[y_col],s=temp[size_col], label=q)
ax.set_xlabel(x_col)
ax.set_ylabel(y_col)
ax.legend(title=hue_col)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM