サムネがコーヒーの記事は書きかけです。

【研究用スクリプト】PythonでJitter Plotを描画する方法

基本スクリプト

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

plt.style.use('default')
sns.set()
sns.set_style('whitegrid')
sns.set_palette('Set3')


df = pd.DataFrame({
    'day1': np.random.normal(10, 2, 20),
    'day2': np.random.normal(15, 3, 20),
    'day3': np.random.normal(5, 1, 20)
})

df_melt = pd.melt(df)
print(df_melt.head())

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
sns.stripplot(x='variable', y='value', data=df_melt, jitter=True, color='black', ax=ax)
ax.set_xlabel('day')
ax.set_ylabel('Relative fluo. intensity(-)')
ax.set_ylim(0, 20)

fig.savefig("jitter_plot.png",dpi = 500)

実行結果

dayあたりに2項目同時にプロットする場合

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

plt.style.use('default')
sns.set()
sns.set_style('whitegrid')
sns.set_palette('Set3')



# Control
dfCtrl = pd.DataFrame({
    'day1': np.random.normal(9, 2, 30),
    'day2': np.random.normal(9, 2, 30),
    'day3': np.random.normal(9, 2, 30)
})

# Gentamicin
dfGen = pd.DataFrame({
    'day1': np.random.normal(10, 2, 30),
    'day2': np.random.normal(11, 3, 30),
    'day3': np.random.normal(16, 2, 30)
})


dfCtrl_melt = pd.melt(dfCtrl)
dfCtrl_melt['species'] = 'Control'
dfGen_melt = pd.melt(dfGen)
dfGen_melt['species'] = 'Gentamicin'

df = pd.concat([dfCtrl_melt, dfGen_melt], axis=0)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

sns.stripplot(x='variable', y='value', data=df, hue='species', dodge=True,
              jitter=True, color='black', palette='Dark2', ax=ax)

ax.set_xlabel('day')
ax.set_ylabel('Relative fluo. intensity(-)')
ax.set_ylim(0, 20)
fig.savefig("jitter2.png",dpi = 500)

実行結果

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です