我试图让我的swarmplot在黑白和色盲的人中更容易阅读,通过让色调影响颜色,同时也影响标记的另一个几何方面。在
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
fig, ax = plt.subplots(1,1)
ax = sns.swarmplot(x="day", y="total_bill", hue="sex",data=tips,size=8,ax=ax)
plt.show()
下面将提供一种方法,它可以轻松地为swarmplot获得所需的不同标记(或者更普遍地说是任何分类散点图)。它可以按原样使用,只需将其复制到现有的绘图脚本上即可。在
这个想法是把一个散点的颜色和一个标记联系起来。E、 任何散点都会自动从指定列表中获取标记。因此,这只适用于不同颜色的地块。在
其实我之前也想过同样的问题。我没有想出最好的解决方案,但我有一个黑客,工作正常。不幸的是,如果使用
dodge=True
,则实现起来要容易得多。在其思想是收集由} 以了解如何创建路径。在
swarmplot
创建的PathCollections
对象。如果dodge=True
,那么您将得到N_cat*N_hues+N_hues
个集合(N_hues extra用于创建图例)。您可以简单地遍历该列表。因为我们希望所有的色调都是相同的,所以我们使用N_hues步幅来获得与每个色调对应的所有集合。之后,您可以将该集合的paths
更新为您选择的任何Path
对象。请参阅the documentation for ^{为了简化操作,我在手之前创建了一些虚拟的散点图,以获得一些可以使用的预制
Paths
。当然,任何Path
都应该能够工作。在更新与
dodge=False
“合作”的解决方案。在如果使用
dodge=False
,那么将得到N+2个集合,每个类别一个,图例+2个。问题是所有不同的标记颜色在这些集合中都是混乱的。在一个可能的,但很难看的解决方案是循环访问集合的每个元素,并基于每个元素的颜色创建一个
^{pr2}$Path
对象的数组。在感谢@ImportanceOfBeingErnest提供解决方案。我试图编辑他/她的解决方案来解决一些小问题,但最终他/她建议我发布自己的答案。在
这个解决方案和他/她的一样,但是当没有指定标记阵列时,它不会改变法向散射的行为。它的应用也更简单,它修复了图例失去标题的错误。在
下图由以下代码生成:
相关问题 更多 >
编程相关推荐