![](/img/trans.png)
[英]Assign/map colors to the points in Seaborn.regplot (Python 3)
[英]Different colors for points and line in Seaborn regplot
您是對的,因為color
參數會更改所有繪圖元素。 但是,如果您閱讀文檔中相關句子的最后一點:
顏色 : matplotlib 顏色
應用於所有繪圖元素的顏色; 將被
scatter_kws
或line_kws
傳遞的顏色取代。
因此,使用scatter_kws
或line_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_kws
和scatter_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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.