Spaces:
Runtime error
Runtime error
streamlit_app.py
#1
by KhoaHo305 - opened
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import streamlit as stimport numpy as npimport matplotlib.pyplot as plt
st.title("Đồ thị dao động điều hòa")
# Nhập dữ liệuA = st.number_input("Biên độ A (cm)", min_value=0.0, value=10.0, step=0.1)step_A = st.number_input("Bước của trục tung (step A)", min_value=0.01, value=5.0, step=0.01)T = st.number_input("Chu kỳ T (s)", min_value=0.01, value=10.0, step=0.01)step_t = st.number_input("Bước của trục thời gian (s)", min_value=0.01, value=1.0, step=0.01)duration = st.number_input("Khoảng thời gian vẽ (s)", min_value=0.1, value=30.0, step=0.1)phi_input = st.text_input("Pha ban đầu (rad, có thể nhập pi/3, pi/6, 0...)","-pi/2")
# Chuyển pha nhập vào thành sốtry: phi = float(eval(phi_input, {"pi": np.pi, "np": np, "__builtins__": {}}))except: phi = -np.pi/2 st.warning("Pha ban đầu không hợp lệ, đặt mặc định -π/2")
# Tạo dữ liệut = np.linspace(0, duration, int(duration*100))w = 2 * np.pi / Tx = A * np.cos(w * t + phi)
# Vẽ đồ thịfig, ax = plt.subplots(figsize=(7, 4))
ax.plot(t, x, color='black', linewidth=1.8)
# Setup trụcmargin_ratio = 0.2ylim_min = -A*(1+margin_ratio)ylim_max = A*(1+margin_ratio)ax.set_ylim(ylim_min, ylim_max)ax.set_xlim(0, duration*(1+margin_ratio))
# Ticks trục tungyticks = np.arange(-A, A+step_A, step_A)ax.set_yticks(yticks)ax.set_yticklabels([f"{int(y)}" if y!=0 else "0" for y in yticks])
# Ticks trục hoànhxticks = np.arange(0, duration+step_t, step_t)ax.set_xticks(xticks)ax.set_xticklabels([f"{x:.2f}" if x !=0 else "" for x in xticks])
# Trục và mũi tênax.spines['top'].set_visible(False)ax.spines['right'].set_visible(False)ax.spines['left'].set_position('zero')ax.spines['bottom'].set_position('zero')
ax.annotate('', xy=(duration*(1+margin_ratio), 0), xytext=(0, 0), arrowprops=dict(arrowstyle='->', linewidth=1.5))ax.annotate('', xy=(0, ylim_max*1.1), xytext=(0, ylim_min*1.1), arrowprops=dict(arrowstyle='->', linewidth=1.5))
ax.text(duration*(1+margin_ratio)*1.02, 0, 't (s)', va='center', fontsize=12)ax.text(0, ylim_max*1.05, 'x (cm)', ha='left', fontsize=12)
ax.grid(True, linestyle='-', alpha=0.7, linewidth=1)
st.pyplot(fig)
HMAdded+64