簡體   English   中英

Seaborn regplot中點和線的不同顏色

[英]Different colors for points and line in Seaborn regplot

Seaborn 的regplot文檔中列出的所有示例都顯示相同顏色的點和回歸線。 更改color參數會同時更改兩者。 如何為點設置不同的顏色作為線?

您是對的,因為color參數會更改所有繪圖元素。 但是,如果您閱讀文檔中相關句子的最后一點:

顏色 : matplotlib 顏色

應用於所有繪圖元素的顏色; 將被scatter_kwsline_kws傳遞的顏色取代。

因此,使用scatter_kwsline_kws我們可以單獨更改它們的顏色。 以文檔中給出的第一個例子為例:

import seaborn as sns

tips = sns.load_dataset("tips")
ax = sns.regplot(x="total_bill", y="tip", data=tips,
                 scatter_kws={"color": "black"}, line_kws={"color": "red"})

plt.show()

給出:

在此處輸入圖片說明

你已經有了很好的答案。 DavidG建議使用line_kwsscatter_kws有副作用,即回歸線和置信區間顏色相同(盡管 ci 是 alpha-ed)。 這是一種具有不同顏色的方法。 如果有更好的方法,我想知道!

創建一個 seaborn FacetGrid ,然后使用map()函數添加圖層:

import pandas 
x = [5, 3, 6, 3, 4, 4, 6, 8]
y = [13, 15, 7, 12, 13, 11, 9, 5]
d = pandas.DataFrame({'x':x, 'y': y})
import seaborn
import matplotlib.pyplot as plt 
seaborn.set(style = 'whitegrid')
p = seaborn.FacetGrid(d, size = 4, aspect = 1.5) 
p.map(plt.scatter, 'x', 'y', color = 'red')
p.map(seaborn.regplot, 'x', 'y', scatter = False, ci = 95, 
    fit_reg = True, color = 'blue') 
p.map(seaborn.regplot, 'x', 'y', scatter = False, ci = 0, 
    fit_reg = True, color = 'darkgreen')
p.set(xlim = (2, 9)) 
p.set(ylim = (2, 17)) 
p.savefig('xy-regression-ci.pdf', bbox_inches='tight')

我被這個問題啟發了

在此處輸入圖片說明

順便說一句(題外話):盡早設置圖形的大小,因為通常的方法在這里似乎並不適用。

# set figure size here by combining size and aspect:
seaborn.FacetGrid(d, size=4, aspect=1.5) 

# usual tricks below do not work with FacetGrid?
p.set_size_inches(8,4)
seaborn.set(rc={'figure.figsize':(8,4)})
rcParams['figure.figsize'] = 8,4

我能夠使用 PatrickT 的答案獲得不同的顏色,而無需 FacetGrid。 我想我會提到它。

import pandas as pd
x = [5, 3, 6, 3, 4, 4, 6, 8]
y = [13, 15, 7, 12, 13, 11, 9, 5]
d = pd.DataFrame({'x':x, 'y':y})
import seaborn as sns
import matplotlib.pyplot as plt 
sns.set(style = 'whitegrid')
plt.scatter(x, y, color = 'red')
sns.regplot(data=d, x='x', y='y', scatter = False, ci = 95, 
    fit_reg = True, color = 'blue') 
sns.regplot(data=d, x='x', y='y', scatter = False, ci = 0, 
    fit_reg = True, color = 'darkgreen')

seaborn 多彩 regplot

暫無
暫無

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

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