# グラフの作成 (主効果あり交互作用なし)import seaborn as snsimport matplotlib.pyplot as plt
# グラフエリアを作成fig = plt.figure(figsize=(90/25.4, 90/25.4))ax = fig.add_subplot(111)ax.spines['top'].set_linewidth(0.5)ax.spines['right'].set_linewidth(0.5)ax.spines['bottom'].set_linewidth(0.5)ax.spines['left'].set_linewidth(0.5)ax.grid(color=(0.8, 0.8, 0.8), linewidth=0.2)
# プロット作成sns.pointplot( data=data, x=data.columns[3], y=data.columns[5], hue=data.columns[4], dodge=0.1, palette="dark:gray", markers="o", markersize=4, linestyle="-", linewidth=0.3, markeredgecolor='black', markeredgewidth=0.2, errorbar="se", capsize=0.05, ax=ax )
# 凡例の設定legend = ax.legend(loc='lower right', bbox_to_anchor=(1, 0), fontsize=6, ncol=2)legend.get_frame().set_linewidth(0)ax.tick_params(axis='both', which='major', labelsize=7)plt.xlabel(data.columns[4], fontsize=7)plt.ylabel(data.columns[5], fontsize=7)
# 科学研究のグラフとして必要な注釈の追加plt.text(1, 1.01, "Error bars denote Mean ± SE. * indicates p < 0.05 significance.", ha='right', fontsize=6, transform=plt.gca().transAxes)
# 統計的注釈の追加# 位置調整用の数値の設定dodge = 0.07y_max = ax.get_ylim()[1]y_len = (data[data.columns[5]].max() - data[data.columns[5]].min()) / 120
# plt.ylim(800, 1150)
# 統計的注釈の形状を定義def add_stat_annotation(x1, x2, y, dg): plt.plot([x1+dg, x1+dg, x2+dg, x2+dg], [y, y + y_len/2, y + y_len/2, y], lw=0.2, c='k') plt.text((x1 + x2)/2+dg, y, "*", ha='center', va='bottom', fontsize=6)
# 水準間の有意差(交互作用なし)def group_stat_annotation(x1, x2, y, dg): plt.plot([x1+dg, x1+dg, x2+dg, x2+dg], [y, y + y_len/2, y + y_len/2, y], lw=0.2, c='k') plt.plot([(x1+x2)/2, (x1+x2)/2], [y+ y_len/2, y + y_len ], lw=0.2, c='k')
levels = data['Condition_C'].unique()level_pairs_g = [(0, 0), (1, 1), (2, 2), (3, 3)]for x1, x2 in level_pairs_g: group_stat_annotation(x1-0.1, x2+0.1, y_max, 0)
y_max += y_len
significant_pairs =[ (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3) ] #有意差が出た水準間のみ残す
for x1, x2 in significant_pairs: add_stat_annotation(x1, x2, y_max, 0) y_max += y_len # 次の注釈のためにy_maxを更新