我正在尝试使用seaborn lmplot 创建一个图形。我使用
hue
为一个类别的不同级别着色。但我希望每个点的标记形状对应于与 hue
不同的类别。但是 sns.lmplot 只接受色调级别的标记。
有没有办法可以使用
scatter_kws
将标签传递给点,然后以某种方式访问seaborn.axisgrid.FacetGrid中AxesSubplots类中的点?
简单的例子:
import seaborn as sns
import pandas as pd
snsdf = pd.DataFrame({'x' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'y' : [9, 9, 8, 6, 6, 4, 4, 3, 1, 1],
'hue' : ['tree', 'animal', 'animal', 'tree', 'animal'] * 2,
'marker' : ['o', 'o', '<', '<', 'o', '<', '<', '<', 'o', 'o']
})
# plot it normally
g = sns.lmplot(data=snsdf,
x='x',
y='y',
hue='hue',
scatter_kws{'label' : snsdf.marker})
# iterate objects in `g` to find points and change marker
# (i'm not sure about this part, but maybe accessing the label can tell me which marker the point should be? but I'm not sure where the actual points are where I could access the label)
谢谢!
您可以尝试使用
lmplot
和 scatterplot
的某种组合,其中有一个 style
关键字用于使用不同的标记(如这个答案)。
import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt
snsdf = pd.DataFrame({'x' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'y' : [9, 9, 8, 6, 6, 4, 4, 3, 1, 1],
'hue' : ['tree', 'animal', 'animal', 'tree', 'animal'] * 2,
'marker' : ['o', 'o', '<', '<', 'o', '<', '<', '<', 'o', 'o']
})
# plot the lmplot, but turn off the scatter
g = sns.lmplot(data=snsdf,
x='x',
y='y',
hue='hue',
scatter=False)
ax = plt.gca() # get the axes
# do scatter plot on the same axes
g = sns.scatterplot(
data=snsdf,
x="x",
y="y",
hue="hue",
style="marker", # set style to marker data (it won't actually use those markers though!
ax=ax,
legend=False
)
注意:这看起来不太漂亮,所以我希望你需要尝试一下。