Make spirals more awesome

This commit is contained in:
Thiago Teixeira 2023-11-02 12:32:17 -07:00 committed by GitHub
parent af17bb9f5d
commit 8bd2197e4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,38 +1,40 @@
from collections import namedtuple
import altair as alt import altair as alt
import math import numpy as np
import pandas as pd import pandas as pd
import streamlit as st import streamlit as st
""" """
# Welcome to Streamlit! # Welcome to Streamlit!
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart: Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
forums](https://discuss.streamlit.io). forums](https://discuss.streamlit.io).
In the meantime, below is an example of what you can do with just a few lines of code: In the meantime, below is an example of what you can do with just a few lines of code:
""" """
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
with st.echo(code_location='below'): indices = np.linspace(0, 1, num_points)
total_points = st.slider("Number of points in spiral", 1, 5000, 2000) theta = 2 * np.pi * num_turns * indices
num_turns = st.slider("Number of turns in spiral", 1, 100, 9) radius = indices
Point = namedtuple('Point', 'x y') x = radius * np.cos(theta)
data = [] y = radius * np.sin(theta)
points_per_turn = total_points / num_turns df = pd.DataFrame({
"x": x,
"y": y,
"idx": indices,
"rand": np.random.randn(num_points),
})
for curr_point_num in range(total_points): st.altair_chart(alt.Chart(df, height=700, width=700)
curr_turn, i = divmod(curr_point_num, points_per_turn) .mark_point(filled=True)
angle = (curr_turn + 1) * 2 * math.pi * i / points_per_turn .encode(
radius = curr_point_num / total_points x=alt.X("x", axis=None),
x = radius * math.cos(angle) y=alt.Y("y", axis=None),
y = radius * math.sin(angle) color=alt.Color("idx", legend=None, scale=alt.Scale()),
data.append(Point(x, y)) size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
))
st.altair_chart(alt.Chart(pd.DataFrame(data), height=500, width=500)
.mark_circle(color='#0068c9', opacity=0.5)
.encode(x='x:Q', y='y:Q'))