MENU
  • ホーム
  • Contact Us
Hayashi's discovery-oriented learning | 地域研究と分析の林
地域研究と分析の林
  • ホーム
  • Contact Us
地域研究と分析の林
  • ホーム
  • Contact Us
Youtubeチャンネルはこちら(鉄道趣味メイン)
  1. ホーム
  2. データ前処理と即席可視化
  3. バイオリンプロット

バイオリンプロット

idvaluelabel
id001251
id002490
・・・・・・・・・
id024300
Python
plt.figure(figsize=(4,3))
sns.violinplot(x='label', y='value',data=df)
# データをラベルごとにグループ化
grouped_data = [group['value'].values for name, group in df.groupby('label')]

# バイオリンプロットの描画
fig, ax = plt.subplots(figsize=(4,3))
ax.violinplot(grouped_data, showmeans=True, showmedians=True)

# ラベルの設定
ax.set_xticks(range(1, len(df['label'].unique()) + 1))
ax.set_xticklabels(df['label'].unique())
ax.set_xlabel('Labels')
ax.set_ylabel('Values')
ax.set_title('Violin Plot from DataFrame')
from scipy.stats import gaussian_kde

def violin_plot(ax, data, positions, width=0.5):
    for i, pos in enumerate(positions):
        d = data[i]
        kde = gaussian_kde(d, bw_method=0.3)
        x = np.linspace(min(d), max(d), 100)
        v = kde.evaluate(x)
        v = v / v.max() * width
        ax.fill_betweenx(x, pos - v, pos + v, alpha=0.5)

fig, ax = plt.subplots(figsize=(4,3))

# データをラベルごとに分ける
data_0 = df[df['label'] == 0]['value']
data_1 = df[df['label'] == 1]['value']

# 描画
violin_plot(ax, [data_0, data_1], positions=[0, 1])

ax.set_xticks([0, 1])
ax.set_xticklabels(['0', '1'])
ax.set_xlabel('Label')
ax.set_ylabel('Value')
ax.set_title('Violin Plot of Value by Label')
R言語
# label列を因子型に変換
df$label <- as.factor(df$label)

# 描画
ggplot(df, aes(x = label, y = value)) +
  geom_violin(trim = FALSE) +
  labs(title = "Value by Label", x = "Label", y = "Value")
スポンサーリンク
新着記事
  • 鳳派出所の風景(後編)
  • 鳳派出所の風景(中編)
  • 鳳派出所の風景(前編)
おすすめ

Youtubeチャンネル
https://www.youtube.com/watch?v=w2q30VHcXj4
  • ホーム
  • Contact Us

© 地域研究と分析の林.