pythonでFigの作成（seaborn）

# 準備
日本語を含むFigを作成する場合は、
```terminal
conda install japanize-matplotlib
pip install japanize-matplotlib
```
```python
import japanize_matplotlib
```

### ライブラリのインポート
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
```
### ダミーデータの生成
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# ダミーデータの生成パラメータ
num_individuals = 40  # 個体数
num_day_points = 10   # 経過時間の数
mean_value = [[300, 350, 400, 600, 660, 700, 780, 820, 600, 500],       # 値の平均1
              [400, 650, 800, 860, 900, 1000, 1180, 1420, 700, 600],   # 値の平均2
              [300, 350, 360, 370, 400, 390, 400, 410, 400, 420]]       # 値の平均3


# 各経過日数におけるダミーデータ生成
data = []
rng = np.random.default_rng()

for i in range(3):
  for day_point in range(num_day_points):
    individual_values = rng.normal(loc=mean_value[i][day_point], scale=100, size=num_individuals)
    individual_numbers = np.arange(1, num_individuals + 1)
    day_column = np.full_like(individual_numbers, fill_value=day_point)

    # 個体番号、経過日数、値を含むデータ
    day_data = np.column_stack((individual_numbers, day_column, individual_values, np.full(num_individuals, i)))
    data.append(day_data)

# 全体のデータを結合
data = np.vstack(data)
```




### データフレームへの変換（ndarrayから）

```python
df = pd.DataFrame(data, columns=['individual', 'day', 'value', 'group'])
group_labels = np .array((df.loc[:,'group']), dtype='int64')
df.loc[:,'group'] = np.array(['A', 'B', 'C'])[group_labels]

df
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3655949/518495f3-beb1-dd40-05e8-0abcd154aa2d.png)



# seabornによるFig作成
### 全体の表示
```python
fig = plt.figure(figsize=(9,6)) 
ax = fig.add_subplot(1,1,1)

sns.stripplot(data=df,
              x='day',
              y='value',
              hue='group',
              dodge=True,
              palette='muted',
              alpha=0.7,
              ax=ax
              )
plt.show()
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3655949/908cbcc8-a63b-1d73-ce7f-891073e3ac67.png)

### 指定したgroupのみ表示する

```python
fig = plt.figure(figsize=(6,4)) 
ax = fig.add_subplot(1,1,1)

sns.stripplot(data=df[df.loc[:,'group']=='A'],
              x='day',
              y='value',
              alpha=0.7,
              ax=ax,
              )

plt.show()
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3655949/04ee06e5-11aa-9b4c-a870-1e4f761b8515.png)

### エラーバーを重ねる
```python
fig = plt.figure(figsize=(9,6)) 
ax1 = fig.add_subplot(1,1,1)

sns.stripplot(data=df,
              x='day',
              y='value',
              hue='group',
              dodge=0.5,
              palette='muted',
              alpha=0.7,
              ax=ax1
              )

ax2 = fig.add_subplot(111, frameon=False, sharex=ax1, sharey=ax1)

sns.pointplot(data=df,
              x='day',
              y='value',
              hue='group',
              dodge=0.5,
              palette=['black'],
              ax=ax2,
              markers=['_', '_', '_'],
              linestyles='none',
              errwidth=1,
              capsize=0.1,

              )
ax2.get_legend().remove()
plt.show()
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3655949/e9d9fd9e-c5c6-27cb-220a-fe8e337159f5.png)


```python
fig = plt.figure(figsize=(6,4)) 
ax = fig.add_subplot(1,1,1)

sns.stripplot(data=df[df.loc[:,'group']=='A'],
              x='day',
              y='value',
              color='black',
              alpha=1,
              ax=ax,
              )

### 棒グラフを重ねる
sns.barplot(data=df[df.loc[:,'group']=='A'],
              x='day',
              y='value',
              errorbar=("sd"), 
              errcolor='black',
              capsize=.4,
              color='grey',
              edgecolor="black",
              linewidth=1,
              ax=ax,
              )

ax.set_ylim(0, 1400)

plt.show()
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3655949/79e91564-1f9c-5ab1-cd4b-8ebfdc15836d.png)


### 棒グラフで表示

```python
fig = plt.figure(figsize=(6,4)) 
ax = fig.add_subplot(1,1,1)

sns.lineplot(data=df,
              x='day',
              y='value',
              hue='group',
              style='group',
              palette='muted',
              markers=['o'], 
              dashes=False, 
              errorbar=('ci', False),
              alpha=1.0,
              markersize=6,
              ax=ax
              )

plt.show()
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3655949/e466c1f5-cee1-12db-7f88-a5c54a992159.png)