LoginSignup
0
0

pythonでFigの作成(seaborn)

Last updated at Posted at 2023-12-21

準備

日本語を含むFigを作成する場合は、

conda install japanize-matplotlib
pip install japanize-matplotlib
import japanize_matplotlib

ライブラリのインポート

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

ダミーデータの生成

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から)

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

seabornによるFig作成

全体の表示

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

指定したgroupのみ表示する

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

エラーバーを重ねる

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

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

棒グラフで表示

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0