簡體   English   中英

為什么在seaborn plot中設置色調會改變一個點的大小?

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

我試圖制作的情節需要實現三件事。

  1. 如果在同一天以相同的分數進行測驗,則該分數需要更大。
  2. 如果兩個測驗分數重疊,則需要有一些抖動,以便我們可以看到所有分數。
  3. 每個測驗都需要有自己的顏色

這就是我要怎么做。

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

在此處輸入圖片說明

設置色調,幾乎可以滿足我的所有需求,結果就是這樣。

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

在此處輸入圖片說明

有沒有辦法在保持點的大小的同時獲得色調?

因為當你使用它不工作hue ,seaborn做兩個獨立的散點圖,因此尺寸參數使用的是經過scatter_kws=與數據框的內容不再一致。

但是,您可以手動重新創建相同的效果:

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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM