[英]change the factorplot of seaborn to include dots
I have a pandas dataframe that looks like this:我有一个 pandas dataframe 看起来像这样:
feat roi sbj alpha test_type acc
0 cnn2 LOC Subject1 normal_space imagery 0.260961
1 cnn2 LOC Subject1 0.4 imagery 0.755594
2 cnn4 LOC Subject1 normal_space imagery 0.282238
3 cnn4 LOC Subject1 0.4 imagery 0.726485
4 cnn6 LOC Subject1 normal_space imagery 0.087359
5 cnn6 LOC Subject1 0.4 imagery 0.701167
6 cnn8 LOC Subject1 normal_space imagery 0.209444
7 cnn8 LOC Subject1 0.4 imagery 0.612597
8 glove LOC Subject1 normal_space imagery 0.263176
9 glove LOC Subject1 0.4 imagery 0.659182
10 cnn2 FFA Subject1 normal_space imagery 0.276830
11 cnn2 FFA Subject1 0.4 imagery 0.761014
12 cnn4 FFA Subject1 normal_space imagery 0.288127
13 cnn4 FFA Subject1 0.4 imagery 0.727325
14 cnn6 FFA Subject1 normal_space imagery 0.113507
15 cnn6 FFA Subject1 0.4 imagery 0.732963
16 cnn8 FFA Subject1 normal_space imagery 0.264455
17 cnn8 FFA Subject1 0.4 imagery 0.615467
18 glove FFA Subject1 normal_space imagery 0.245950
19 glove FFA Subject1 0.4 imagery 0.640502
20 cnn2 PPA Subject1 normal_space imagery 0.344078
...
For plotting it, I wrote:为了绘制它,我写道:
ax = sns.factorplot(x="feat", y="acc", col="roi", hue="alpha", alpha = 0.9, data=df_s_pt, kind="bar").set(title = "perception, scene wise correlation")
The result look like this:结果如下所示:
I want to upgrade it so it can look like the one in this answer (so it has the dots of each subject (ie, Subject1, Subject2, ...))我想升级它,让它看起来像这个答案中的那个(所以它有每个主题的点(即 Subject1,Subject2,...))
Also, I want to control the color.另外,我想控制颜色。
I could'nt use the code in that answer .我无法使用该答案中的代码。 How should I apply having dots/color change in factorplot?我应该如何在 factorplot 中应用点/颜色变化?
Thanks in advance提前致谢
Some remarks:一些评论:
sns.factorplot
is a very old function. In the newer seaborn versions it has been replaced by sns.catplot
. sns.factorplot
是一个非常古老的 function。在较新的 seaborn 版本中,它已被sns.catplot
取代。 To take advantage of the hard work in correcting, improving and extending the library, it is highly recommended to upgrade to the latest version (0.12.2)为了利用在纠正、改进和扩展库方面的辛勤工作,强烈建议升级到最新版本 (0.12.2)ax
, but a grid of subplots (a FacetGrid
).在一个 go 中创建多个子图的函数不返回ax
,而是返回子图网格(一个FacetGrid
)。 It is extremely confusing storing the result of such a function in ax
, as matplotlib's axes functions won't work on them.将这样一个 function 的结果存储在ax
中是非常令人困惑的,因为 matplotlib 的轴函数对它们不起作用。set(title=...)
on the FacetGrid
changes the titles of the individual subplots.在FacetGrid
上调用set(title=...)
会更改各个子图的标题。 It therefore removes the title given by seaborn to indicate the feature used for each subplot ( 'roi'
in the current example).因此,它删除了 seaborn 给出的标题,以指示用于每个子图的特征(当前示例中的'roi'
)。g.fig.suptitle(...)
can be used.要更改整体标题,可以使用g.fig.suptitle(...)
。 Some extra space needs to be provided, as that doesn't happen automatically.需要提供一些额外的空间,因为这不会自动发生。g.map_dataframe
to apply a function to each subset used corresponding to its subplot.最新的 seaborn 版本有一个 function g.map_dataframe
将 function 应用于与其子图对应的每个子集。palette=
parameter. Colors 可以通过palette=
参数控制。 Either individual colors, or a colormap can be chosen.可以选择单独的 colors 或颜色图。pd.Categorical
.为了确保顺序在任何地方都相同,通常有助于制作类型为pd.Categorical
的 dataframe 列。sns.catplot(..., errorbar=None)
您可能希望使用sns.catplot(..., errorbar=None)
来抑制错误栏Here is an example starting from dummy test data.这是一个从虚拟测试数据开始的示例。
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'feat': np.random.choice(['cnn2', 'cnn4', 'cnn6', 'cnn8', 'glove'], 100),
'roi': np.random.choice(['LOC', 'FFA'], 100),
'alpha': np.random.choice(['normal_space', 0.4], 100),
'acc': 1 - np.random.rand(100) ** 2})
df['feat'] = pd.Categorical(df['feat'])
df['roi'] = pd.Categorical(df['roi'])
df['alpha'] = pd.Categorical(df['alpha'])
g = sns.catplot(x="feat", y="acc", col="roi", hue="alpha", palette=['crimson', 'limegreen'],
alpha=0.9, data=df, kind="bar")
g.map_dataframe(sns.stripplot, x="feat", y="acc", hue="alpha", palette=['cornflowerblue', 'yellow'],
edgecolor="black", linewidth=.75, dodge=True)
g.set(xlabel='') # remove the xlabels if they are already clear from the xticks
g.fig.subplots_adjust(top=0.9) # need extra space for the overall title
g.fig.suptitle("perception, scene wise correlation")
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.