import os import tempfile import shutil import gradio as gr import matplotlib.cm as cm import pandas as pd import japanize_matplotlib import numpy as np import matplotlib.pyplot as plt import seaborn as sns def all_likelihood_plot(csv_file_name, tmpdir): df = pd.read_csv(csv_file_name, header=[1, 2]) df = df.drop(df.columns[[0]], axis=1) columns = df.columns.droplevel(1) # 重複を削除 columns = columns.drop_duplicates() likelihood = [df[x]["likelihood"] for x in columns] a = pd.DataFrame(likelihood, index=columns).T #平均値を求める point_average = a.mean() parts = ["指節1", "指節2", "指節3", "指節4", "指節5", "指節6", "指節7", "指節8", "指節9", "指節10", "指節11", "指節12", "指節13", "指節14", "触角(左)", "触角(右)", "頭部", "腹尾節"] # カラーマップの設定 a.columns = parts cmap = plt.get_cmap('rainbow') # バイオリン図のプロット sns.set(style="whitegrid",font="IPAexGothic") fig, ax = plt.subplots() # データをバイオリンプロットで描画 sns.violinplot(data=a, palette=[cmap(i) for i in np.linspace(0, 1, len(columns))], ax=ax,inner=None) # 横軸のラベルを重ならないように plt.xticks(rotation=65) ax.set_title('付属肢別の尤度') ax.set_xlabel('付属肢') ax.set_ylabel('尤度') #それぞれの要素の平均値をプロット plt.scatter(x=parts, y=point_average, color='black', marker='x') # 最大値を1に plt.ylim(0, 1) # fig.set_figwidth(10) #ラベルがはみ出ないように plt.tight_layout() # グラフを表示 plt.savefig(f"likelihood.png", dpi=300) def main(csv_file): with tempfile.TemporaryDirectory(dir=".") as tmpdir: all_likelihood_plot(csv_file, tmpdir) return f"likelihood.png" iface = gr.Interface(fn=main, inputs="file", outputs="image", title="尤度のグラフを作成します。") iface.launch()