![](/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.