python - Seaborn,每列一个数据的 fiddle 图

标签 python seaborn

我想结合这个 fiddle 情节http://seaborn.pydata.org/generated/seaborn.violinplot.html (split=True 的第四个示例)与这个 http://seaborn.pydata.org/examples/elaborate_violinplot.html .

实际上,我有一个 dataFrame,其中有一列 Success(是或否)和几个数据列。例如:

df = pd.DataFrame(
    {"Success": 50 * ["Yes"] + 50 * ["No"], 
     "A": np.random.randint(1, 7, 100), 
     "B": np.random.randint(1, 7, 100)}
)

    A  B Success
0   6  4     Yes
1   6  2     Yes
2   1  1     Yes
3   1  2     Yes
.. .. ..     ...
95  4  4      No
96  2  1      No
97  2  6      No
98  2  3      No
99  2  1      No

我想为每个数据列绘制一个 fiddle 图。它适用于:

import seaborn as sns
sns.violinplot(data=df[["A", "B"]], inner="quartile", bw=.15)

但是现在,我想根据 Success 列拆分 fiddle 。但是,在使用 hue="Success" 时,我遇到了一个错误:Cannot use 'hue' without 'x' or 'y'。因此,我该如何通过根据“成功”列拆分来绘制 fiddle 图?

最佳答案

如果正确理解您的问题,您需要 reshape 数据框以使其具有长格式:

df = pd.melt(df, value_vars=['A', 'B'], id_vars='Success')
sns.violinplot(x='variable', y='value', hue='Success', data=df)
plt.show()

enter image description here

关于python - Seaborn,每列一个数据的 fiddle 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41573283/

相关文章:

python - 我如何等待 ThreadPoolExecutor.map 完成

python - 从 url 不变的网站中抓取响应表

python - 如何在matplotlib中制作具有不同y轴的堆叠折线图?

python - 如何使用条形图绘制最小/最大条形图

python - seaborn 热图中的离散图例

python - 使用 kdeplot 与 searborn 进行绘图时,将图例移动到框外时出现错误 : No handles with labels found to put in legend.

python - 通过管道将 Haskell 程序连接到 Python 程序(在 Python 中)

python - IPython 笔记本 - ShimWarning : The `IPython.kernel` package has been deprecated

python - 如何在每次数据变化时刷新kivy RecycleView?

python - seaborn 热图标签无法清晰显示