簡體   English   中英

Seaborn 列式小提琴圖

[英]Seaborn columnwise violinplot

我有一個 dict d列出數字的出現:

{'item1': [42, 1, 2, 3, 42, 2, 1, 1, 1, 1, 1],
 'item2': [2, 5],
 'item3': [5, 1, 7, 2, 7, 1, 42, 2, 9]}

然后我將其轉換為 DataFrame 計算這些事件:

df = pd.DataFrame.from_dict({k: dict(Counter(v)) for k, v in d.items()})

    item1  item2  item3
42    2.0    NaN    1.0
1     6.0    NaN    2.0
2     2.0    1.0    2.0
3     1.0    NaN    NaN
5     NaN    1.0    1.0
7     NaN    NaN    2.0
9     NaN    NaN    1.0

How can I plot this or some other DataFrame that was derived from d using seaborn.violinplot , so that each column in the dataframe represents a violin in the plot based on the data provided by each columns values and their respective indices?

我嘗試了多種組合,我相信這在直覺上是最接近的,但不幸的是仍然失敗:

sns.violinplot(x=df.keys(), y=df.index, data=df)

DataFrame傳遞給seaborn.violinplot ,因此每個系列(列)分別繪制:

sns.violinplot(data=df)

圖片

簡單的就夠了

from collections import Counter
d = {'item1': [42, 1, 2, 3, 42, 2, 1, 1, 1, 1, 1],
     'item2': [2, 5],
     'item3': [5, 1, 7, 2, 7, 1, 42, 2, 9]}
df = pd.DataFrame.from_dict({k: dict(Counter(v)) for k, v in d.items()})

sns.violinplot(data=df)

在此處輸入圖像描述

下面的代碼創建 plot:

# Import libraries
import pandas as pd
import collections
import seaborn as sns

# Create dictionary
d = {'item1': [42, 1, 2, 3, 42, 2, 1, 1, 1, 1, 1],
 'item2': [2, 5],
 'item3': [5, 1, 7, 2, 7, 1, 42, 2, 9]}

# Create DataFrame and melt
df = pd.DataFrame.from_dict({k: dict(collections.Counter(v)) for k, v in d.items()})
df = df.melt()

# Plot
sns.violinplot(x="variable", y="value", data=df)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM