海生传奇不显风采,只显色调

2024-05-15 22:03:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的点中同时使用stylehue来使用seaborn可视化我的数据。请参见下面的示例

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset('tips')
grid = sns.FacetGrid(data=df, row='smoker', col='day', hue='time', sharex=False, sharey=False)
grid.map_dataframe(sns.scatterplot, x='total_bill', y='tip', style='sex')
grid.add_legend()

输出如下。我还想看到传说中的性别差异。我怎样才能做到这一点?例如,蓝色x-男性午餐、蓝色*女性午餐、橙色x-男性晚餐、橙色*女性晚餐。如果我甚至可以使用sns.catplot或任何其他方法来做同样的事情,那也很好

Output


Tags: importfalsedfstyleasseabornhuegrid
1条回答
网友
1楼 · 发布于 2024-05-15 22:03:58

您必须将hue=style=移动到对scatterplot()的调用

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset('tips')
grid = sns.FacetGrid(data=df, row='smoker', col='day', sharex=False, sharey=False)
grid.map_dataframe(sns.scatterplot, x='total_bill', y='tip', hue='time', style='sex')
grid.add_legend()

但是,您需要注意FacetGrid文档中的警告:

Warning

When using seaborn functions that infer semantic mappings from a dataset, care must be >taken to synchronize those mappings across facets (e.g., by defing the hue mapping with >a palette dict or setting the data type of the variables to category). In most cases, it >will be better to use a figure-level function (e.g. relplot() or catplot()) than to use >FacetGrid directly.

如果默认值适合您,您最好使用sns.relplot()

sns.relplot(data=df, row='smoker', col='day', x='total_bill', y='tip', hue='time', style='sex', facet_kws=dict(sharex=False, sharey=False))

相关问题 更多 >