python - 使用不同的色调分组覆盖对 seaborn 的 FacetGrid 的多个调用
问题描述
使用 seaborn 的 FacetGrid,我想在多个映射调用之间更新“色调”分组参数。具体来说,我最初有一个自定义绘图函数,它采用“色调”分组,在此之上我想显示各组的平均值(因此忽略色调并汇总所有数据)。
例如
g = sns.FacetGrid(tips, col="time", hue="smoker")
g = (g.map(plt.scatter, "total_bill", "tip", edgecolor="w").add_legend())
g = (g.map(sns.regplot, "total_bill", "tip").add_legend())
将为每个组创建一条彩色回归线。如何更新 FacetGrid 以忽略hue
第二次调用时的分组map
?在此示例中,我希望有两个彩色散点图,其中覆盖了一条(黑色)回归线。
解决方案
色调用于FacetGrid
对输入数据进行分组。它不能只对它进行部分分组。
matplotlib 解决方案可能看起来像
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips", cache=True)
n = len(tips["time"].unique())
usmoker = tips["smoker"].unique()
fig, axes = plt.subplots(ncols=n, sharex=True, sharey=True)
for ax, (time, grp1) in zip(axes.flat, tips.groupby("time")):
ax.set_title(time)
ax.set_prop_cycle(plt.rcParams["axes.prop_cycle"])
for smoker in usmoker:
grp2 = grp1[grp1["smoker"] == smoker]
sns.regplot("total_bill", "tip", data=grp2, label=str(smoker),
fit_reg=False, ax=ax)
ax.legend(title="Smoker")
for ax, (time, grp1) in zip(axes.flat, tips.groupby("time")):
sns.regplot("total_bill", "tip", data=grp1, ax=ax, scatter=False,
color="k", label="regression line")
plt.show()
推荐阅读
- go - 如何处理长时间运行的服务器响应和将其视为停止连接的负载均衡器
- python - 将 API unicode 响应保存到 zip 文件 python
- elixir - 如何确保 distillery 在版本中包含 Erlang inets?
- meteor - 2019年流星值得用吗?它还活着吗?
- spring - 如何调整 Spring Boot 安全配置?
- javascript - 在传递给反应路由器的理解路径中有问题
- tcl - python-2.7.10-1.ppc AIX 7.1 需要 libtcl8.4.so
- sentiment-analysis - 哪个 API 类似于 Twitter 流 API,我可以从中获取来自 Flume 的数据并对这些数据进行情绪分析?
- angular - Angular:动态加载谷歌分析?
- php - Vimeo Laravel API 问题 - 无效的存储桶错误