Created
April 1, 2024 02:59
-
-
Save partrita/cb663b3807235ce39102586146960766 to your computer and use it in GitHub Desktop.
lineplot with errorbar and T-test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import warnings | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import seaborn as sns | |
from scipy import stats | |
df = pd.read_csv("../input/tumor_growth.csv") | |
df.head() | |
# Tumor size Week Treatment | |
# 0 150.1 1 Control | |
# 1 149.3 1 Control | |
# 2 162.6 1 Control | |
# 3 231.4 2 Control | |
# 4 300.8 2 Control | |
x_data = "Week" | |
condition = ["Drug", "Control"] | |
y_data = "Tumor size" | |
color = "Treatment" | |
plt.figure(figsize=(4, 4)) # plot의 크기 | |
# 오차 막대선에 대한 설정 | |
err_kws = {"capsize": 3, "capthick": 1, "elinewidth": 1} | |
# 선 그래프 생성 | |
ax = sns.lineplot( | |
data=df, | |
x=x_data, | |
y=y_data, | |
hue=color, | |
lw=1.5, | |
style=color, | |
markers=["o", "^"], | |
dashes=False, | |
markersize=8, | |
err_style="bars", | |
err_kws=err_kws, | |
# palette=["gray", "firebrick"], | |
) | |
# 축 스타일 지정 | |
for axis in ["bottom", "left"]: | |
ax.spines[axis].set_linewidth(1.5) # 축의 선 두께 | |
ax.spines[axis].set_color("0.2") | |
ax.spines["top"].set_visible(False) | |
ax.spines["right"].set_visible(False) | |
# 축 눈금 스타일 지정 | |
ax.tick_params(width=1.5, color="0.2") | |
plt.xticks(size=14, weight="regular", color="0.2") | |
plt.yticks(size=14, weight="regular", color="0.2") | |
# 축 라벨 스타일 지정 | |
ax.set_xlabel(ax.get_xlabel(), fontsize=14, weight="regular", color="0.2") | |
ax.set_ylabel(ax.get_ylabel(), fontsize=14, weight="regular", color="0.2") | |
# 범례 스타일 지정 | |
plt.legend(frameon=False, prop={"weight": "regular", "size": 10}, labelcolor="0.2") | |
# T-검정 결과를 텍스트로 표시 | |
for week in range(1, 8): | |
z = df[(df.Week == week) & (df.Treatment == condition[0])][y_data].values | |
c = df[(df.Week == week) & (df.Treatment == condition[1])][y_data].values | |
p = stats.ttest_ind(z, c).pvalue | |
max_v = df[df.Week == week][y_data].max() | |
# 유의미한 차이가 있는 경우 * 표시 | |
if p < 0.05: | |
plt.text( | |
x=week - 0.05, | |
y=max_v - 0.5, | |
s="*", | |
fontsize=12, | |
ha="center", | |
va="bottom", | |
color="0.2", | |
weight="bold", | |
) | |
# 유의미한 차이가 없는 경우 ns 표시 | |
else: | |
plt.text( | |
x=week, | |
y=max_v, | |
s="ns", | |
fontsize=10, | |
ha="center", | |
va="bottom", | |
color="0.2", | |
) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment