summaryrefslogtreecommitdiff
path: root/hikakucsv.py
blob: 5bfdb05ce2bb0934ed3f5b93a257bd0eb694d152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python

import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# 日本語フォントの設定
plt.rcParams['font.family'] = 'Noto Sans CJK JP'

# Streamlitアプリケーションのタイトル設定
st.title('CSVファイル比較チャート')

# アップロードボックスの作成
uploaded_files = st.file_uploader("ここにCSVファイルをドラッグ&ドロップ", type=['csv'], accept_multiple_files=True)

if uploaded_files:
    # ファイルの最大数を10に制限
    if len(uploaded_files) > 10:
        st.error("最大10個のファイルをアップロードしてください。")
        st.stop()

    # アップロードされたファイルをリストとして処理
    file_list = []
    for file in uploaded_files:
        df = pd.read_csv(file)
        file_list.append(df)
        st.write(f"アップロードされたファイルのカラム名: {df.columns.tolist()}")

    date_column = 'DATE'  # 日付カラムのデフォルト値

    # 各DataFrameに対して日付カラムの存在を確認
    for df in file_list:
        if date_column not in df.columns:  # 日付カラムが存在しない場合、エラーメッセージを表示
            st.error(f"各CSVファイルには'{date_column}'という名前の日付カラムが必要です。カラム名: {df.columns.tolist()}")
            st.stop()
        df[date_column] = pd.to_datetime(df[date_column])

    # プロットの作成
    fig, ax1 = plt.subplots(figsize=(12, 6))
    ax2 = ax1.twinx()

    for i, df in enumerate(file_list):
        # ファイルごとに縦軸の位置を選択するためのセレクトボックスを作成
        axis_position = st.selectbox(f'ファイル{i+1}の縦軸の位置を選択してください', ["左", "右"], index=0, key=f'axis_position_{i}')

        # ファイルごとにプロットしたいカラムを選択
        col = st.selectbox(f'ファイル{i+1}のカラムを選択してください', [col for col in df.columns if col!= date_column], key=f'col_select_{i}')

        # 凕例名を入力するためのテキストボックスを作成
        legend_name = st.text_input(f'ファイル{i+1}の凡例名を入力してください(デフォルトはカラム名: {col})', col, key=f'legend_input_{i}')

        # データのスケーリング(必要に応じて)
        try:
            df[col] = pd.to_numeric(df[col], errors='coerce')  # 文字列を数値に変換
            min_val = df[col].min()
            max_val = df[col].max()
            range_val = max_val - min_val
            if range_val < 0:
                df[col] -= abs(min_val)  # 中心線を0にするためにデータをスケール
        except ValueError:
            st.error(f"{col}カラムの値が数値に変換できません。")

        # 縦軸の位置に応じてデータフレームをプロット
        if axis_position == "左":
            ax1.plot(df[date_column], df[col], label=legend_name, color=f'C{i}')
        else:
            ax2.plot(df[date_column], df[col], label=legend_name, color=f'C{i}', linestyle='--')

    ax1.set_xlabel(date_column)
    ax1.set_ylabel('')
    ax2.set_ylabel('')

    # 凡例の設定(左軸の凡例を右側に、右軸の凡例を左側に)
    handles1, labels1 = ax1.get_legend_handles_labels()
    handles2, labels2 = ax2.get_legend_handles_labels()
    ax1.legend(handles2, labels2, loc='upper left')
    ax2.legend(handles1, labels1, loc='upper right')

    plt.title('CSV to plot')
    st.pyplot(fig)
else:
    st.write("CSVファイルをアップロードしてください。")