如何在Seaborn Catplot中根据x值制作不同形状的标记?

问题描述 投票:0回答:1

我有一个 catplot,沿 x 轴有 4 个分类组,右侧每个分组在 catplot 上都是不同的颜色,但它们都是圆形标记(出于可访问性原因,我希望每个分组都是不同形状的标记,例如色觉缺陷)。我该怎么做?

dataframe = pd.read_csv(r"C:\Users\zoesh\Downloads\CatPlotCompare.csv")

print(dataframe.head())

print(dataframe.isnull().values.any())



isolation = dataframe["isolation"]

mainhouse = dataframe['mainhouse']

classroom = dataframe['classroom']

community = dataframe['community']



g = sns.catplot(data=dataframe,jitter=0, marker="o")

plt.ylabel("Airborne Concentration of SARS-CoV-2 (RNA copies/m³)")

#make the y axis log scale

plt.yscale('log')

#plt.xlabel ("xlabel", fontsize=8)

#g.fig.get_axes()[0].set_yscale('log')

plt.savefig("catplot4", dpi=2000)

#fig.get_axes()[0].set_yscale('log')

我尝试更改此行中的标记:g = sns.catplot(data=dataframe,jitter=0, marker="o"),但这更改了所有数据点的标记,而不是不同类别的标记。我尝试更改为每个不同的数据帧创建这样的多行,g = sns.catplot(data=isolation,jitter=0, marker="x") 但是创建了 4 个不同的图(每个图都有不同的标记类型),但我希望他们都在一个地块上。

python seaborn accessibility google-maps-markers catplot
1个回答
0
投票

要更改 catplot 中不同类别的标记,您可以使用

hue
中的
sns.catplot
参数将分类变量映射到标记样式。这是一个例子:

g = sns.catplot(data=dataframe, x="category", y="value", hue="category", jitter=0, marker="o", style="category")

在此示例中,

x
y
分别指定 DataFrame 中用于绘图的 x 轴和 y 轴的列。
hue
设置为与
x
相同的列,以将每个类别映射到不同的标记样式。
style
参数也设置为与
hue
相同的列,以设置图例标记以匹配图中标记的样式。您可以根据需要将“类别”和“值”替换为 DataFrame 中列的名称。

您还可以使用字典自定义标记样式,其中键是映射到

hue
的分类变量中的唯一值,值是要使用的标记样式。这是一个例子:

marker_dict = {"A": "o", "B": "s", "C": "^", "D": "d"}
g = sns.catplot(data=dataframe, x="category", y="value", hue="category", jitter=0, style="category", markers=marker_dict)

在这个例子中,

markers
被设置为
marker_dict
字典来为每个类别指定不同的标记样式。您可以将
marker_dict
中的类别值和标记样式视情况替换为您自己的值。

© www.soinside.com 2019 - 2024. All rights reserved.