PongsakornSET's picture
Update app.py
f7443b0 verified
raw
history blame
2.05 kB
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
# Load data
data_y = pd.read_csv("merged_y.csv")
data_p = pd.read_csv("merged_p.csv")
# Sidebar - เลือกปีเริ่มต้นและสิ้นสุด
start_year = st.sidebar.selectbox("เลือกปีเริ่มต้น", options=list(range(2002, 2022)))
end_year= st.sidebar.selectbox("เลือกปีสิ้นสุด", options=list(range(start_year+1, 2024)))
data_y['date'] = pd.to_datetime(data_y['date'])
data_p['date'] = pd.to_datetime(data_p['date'])
# Filter data based on the year range
filtered_data = data_y[(data_y["date"].dt.year >= start_year) & (data_y["date"].dt.year <= end_year)]
filtered_data2 = data_p[(data_p["date"].dt.year >= start_year) & (data_p["date"].dt.year <= end_year)]
# Filter data based on selected years
# สร้างตัวเลือก Selectbox
selected_data = st.sidebar.selectbox("เลือกข้อมูลที่ต้องการโชว์",
options=["Electricity", "LPG", "Diesel"])
# Filter data based on selected data type
filtered_data = filtered_data[filtered_data["symbol"] == selected_data]
filtered_data2 = filtered_data2[filtered_data2["symbol"] == selected_data]
# Plot time series
st.write("### กราฟเส้น Time Series")
plt.figure(figsize=(10, 6))
for fuel_type, group_data in filtered_data.groupby("symbol"):
if fuel_type == "Electricity":
plt.plot(group_data["date"], group_data["Y"], label="Actual", color="yellow")
elif fuel_type == "LPG":
plt.plot(group_data["date"], group_data["Y"], label="Actual", color="green")
elif fuel_type == "Diesel":
plt.plot(group_data["date"], group_data["Y"], label="Actual", color="red")
for fuel_type,group_data in filtered_data2.groupby("symbol"):
plt.plot(group_data["date"], group_data["Y"], label="Predict",color="blue")
plt.xlabel("Date")
plt.ylabel("Value")
plt.title("Time Series")
plt.legend()
st.pyplot(plt)