hey assitent I have this wierd task at work where I need to plot a signal based on some parameter in python streamlit. First of all, I don't know how to use streamlit. Can you do something similar but for a sin wave?
Assistant
Certainly! Streamlit is a great tool for creating interactive web apps with Python, and plotting a sine wave is a good way to get started with it. Below is a step-by-step guide on how you can create a Streamlit app to plot a sine wave based on user input for parameters like frequency and amplitude.
User
can you derscripe your code?
Assistant
1. Install Streamlit
First, you need to install Streamlit. You can do this using pip:
bash
pip install streamlit
2. Create the Streamlit App
Create a new Python file (e.g., sine_wave_app.py) and use the following code:
python
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
# Streamlit App Title
st.title("Interactive Sine Wave Plot")
# Input Parameters
st.sidebar.header("Parameters")
frequency = st.sidebar.slider('Frequency', 0.1, 10.0, 1.0, step=0.1)
amplitude = st.sidebar.slider('Amplitude', 0.1, 10.0, 1.0, step=0.1)
phase_shift = st.sidebar.slider('Phase Shift', 0.0, 2 * np.pi, 0.0, step=0.1)
time_period = st.sidebar.slider('Time Period', 1.0, 10.0, 5.0, step=0.5)
# Create the sine wave
t = np.linspace(0, time_period, 1000)
y = amplitude * np.sin(2 * np.pi * frequency * t + phase_shift)
# Plotting
fig, ax = plt.subplots()
ax.plot(t, y, label=f"{amplitude} * sin(2π * {frequency} * t + {phase_shift:.2f})")
ax.set_xlabel("Time")
ax.set_ylabel("Amplitude")
ax.set_title("Sine Wave")
ax.legend()
# Display the plot
st.pyplot(fig)
3. Run the Streamlit App
Open a terminal, navigate to the directory where you saved your sine_wave_app.py file, and run the app:
bash
streamlit run sine_wave_app.py
This command will start a local server and open your default web browser, where you'll see the app running. You can use the sliders in the sidebar to adjust the frequency, amplitude, phase shift, and time period of the sine wave.
Key Features of the Code
Sidebar Widgets: Used Streamlit's sidebar widgets (st.sidebar.slider) to allow users to interactively adjust the parameters of the sine wave.
Matplotlib Integration: Used Matplotlib to plot the sine wave and st.pyplot() to render the plot in the Streamlit app.
Parameter Controls: The sliders control the frequency, amplitude, phase shift, and time period, providing real-time updates to the plot.
This should give you a good starting point to work with Streamlit for plotting signals. You can modify these parameters or add new ones based on the needs of your task at work!
Certainly! Let's break down the code and explain each part in detail:
1. Import Libraries
python
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
streamlit: This is the main library used to create the web app interface.
numpy: A library used for numerical operations, here specifically to generate the time data and compute the sine wave values.
matplotlib.pyplot: A popular plotting library in Python, used here to create the sine wave plot.
2. Streamlit App Title
python
st.title("Interactive Sine Wave Plot")
st.title: This function sets the title of the Streamlit app. It's typically used to provide a heading or the main purpose of the app.
st.sidebar.header: Adds a header in the sidebar. This helps organize input controls into sections.
st.sidebar.slider: Creates a slider widget in the sidebar. It allows users to interactively adjust its values:
Frequency: Affects how many cycles the sine wave completes in a given time period.
Amplitude: Adjusts the height of the wave peaks.
Phase Shift: Changes the starting point of the wave, effectively shifting it left or right.
Time Period: Controls the total time duration for which the signal is plotted.
4. Create the Sine Wave
python
t = np.linspace(0, time_period, 1000)
y = amplitude * np.sin(2 * np.pi * frequency * t + phase_shift)
np.linspace: Generates 1000 evenly spaced values between 0 and the time_period. This array represents points in time.
sin wave formula: Computes the sine wave values y using the formula ( y = A \sin(2\pi f t + \text{phase_shift}) ), where ( A ) is amplitude, ( f ) is frequency, ( t ) is time, and phase_shift is the phase shift.
5. Plotting
python
fig, ax = plt.subplots()
ax.plot(t, y, label=f"{amplitude} * sin(2π * {frequency} * t + {phase_shift:.2f})")
ax.set_xlabel("Time")
ax.set_ylabel("Amplitude")
ax.set_title("Sine Wave")
ax.legend()
plt.subplots: Creates a figure and an axes. This is where the sine wave will be plotted.
ax.plot: Adds the sine wave to the plot.
ax.set_xlabel / ax.set_ylabel: Labels for the X and Y axes.
ax.set_title: Sets the title of the plot.
ax.legend: Displays a legend on the plot, showing the mathematical representation of the sine wave.
6. Display the Plot
python
st.pyplot(fig)
st.pyplot: Renders the Matplotlib figure in the Streamlit app. This function takes the figure we created and displays it on the web page.
Summary
This Streamlit app allows users to manipulate sliders to adjust the parameters of a sine wave and instantly see the results plotted. It's a simple example that demonstrates how Streamlit can be used to create interactive data visualizations quickly. Feel free to expand on this example by adding more features or parameters according to your task at work!