Skip to content

Commit 7df27bc

Browse files
authored
Add files via upload
1 parent 83c3862 commit 7df27bc

File tree

8 files changed

+218
-0
lines changed

8 files changed

+218
-0
lines changed
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
# =============================================================================
3+
# 快捷键 ctrl + 4 产生这部分注释模板
4+
# =============================================================================
5+
6+
7+
#%% 导入库
8+
# =============================================================================
9+
# 导入库
10+
# =============================================================================
11+
12+
import numpy as np
13+
import matplotlib.pyplot as plt
14+
import seaborn as sns
15+
#%% 等差数列
16+
# =============================================================================
17+
# 等差数列
18+
# =============================================================================
19+
20+
a0 = 1 # 首项
21+
n = 10 # 项数
22+
d = 2 # 公差
23+
24+
a_array = np.arange(a0, a0 + n*d, d)
25+
print('打印等差数列'); print(a_array)
26+
27+
fig = plt.figure(figsize = (8,8))
28+
plt.scatter(np.arange(n), a_array)
29+
plt.title('Arithmetic Progression')
30+
plt.xlabel('Index, $n$')
31+
plt.ylabel('Value, $a_n$')
32+
plt.show()
33+
#%% 二元函数
34+
# =============================================================================
35+
# 二元函数
36+
# =============================================================================
37+
38+
x1_array = np.linspace(-3, 3, 301)
39+
x2_array = np.linspace(-3, 3, 301)
40+
xx1, xx2 = np.meshgrid(x1_array, x2_array)
41+
# np.meshgrid() 函数将 x1_array 和 x2_array 两个一维数组转化为一个二维的网格点矩阵
42+
43+
ff = xx1 * np.exp(-xx1**2 - xx2**2)
44+
# 二元函数的曲面数据
45+
46+
# 可视化
47+
fig = plt.figure(figsize = (8,8))
48+
ax = fig.add_subplot(projection='3d')
49+
# 绘制二元函数网格曲面
50+
ax.plot_wireframe(xx1, xx2, ff, rstride=10, cstride=10)
51+
plt.show()
52+
#%%
53+
# =============================================================================
54+
# 鸢尾花数据
55+
# =============================================================================
56+
57+
# 加载鸢尾花数据集
58+
iris_df = sns.load_dataset('iris')
59+
# 显示数据集前5行
60+
print('打印鸢尾花数据前5行'); print(iris_df.head())
61+
62+
#%%%
63+
fig, ax = plt.subplots(figsize = (8,8))
64+
ax = sns.scatterplot(data=iris_df, x="sepal_length",
65+
y="sepal_width", hue = "species")
66+
# hue 用不同色调表达鸢尾花的类别
67+
68+
ax.set_xlabel('Sepal length (cm)')
69+
ax.set_ylabel('Sepal width (cm)')
70+
ax.set_xticks(np.arange(4, 8 + 1, step=1))
71+
ax.set_yticks(np.arange(1, 5 + 1, step=1))
72+
ax.axis('scaled')
73+
ax.grid(linestyle='--', linewidth=0.25, color=[0.7,0.7,0.7])
74+
ax.set_xbound(lower = 4, upper = 8)
75+
ax.set_ybound(lower = 1, upper = 5)
76+
77+
#%%
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import streamlit as st
2+
import seaborn as sns
3+
import plotly.express as px
4+
5+
# 显示标题
6+
st.title('Welcome to the world of :red[Streamlit]')
7+
# 显示章节标题
8+
st.header('Pandas DataFrame')
9+
# 显示 markdown 文本
10+
st.markdown("Load :blue[Iris Data Set]")
11+
# 从Seaborn导入鸢尾花数据帧
12+
df = sns.load_dataset('iris')
13+
# 显示数据帧
14+
st.write(df)
15+
# 显示章节标题
16+
st.header('Visualize Using Heatmap')
17+
fig = px.imshow(df.iloc[:,:-1])
18+
# 显示热图
19+
st.write(fig)
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import plotly.graph_objects as go
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import streamlit as st
5+
6+
# 产生数据
7+
x1_array = np.linspace(-3, 3, 301)
8+
x2_array = np.linspace(-3, 3, 301)
9+
xx1, xx2 = np.meshgrid(x1_array, x2_array)
10+
11+
# 二元函数的曲面数据
12+
ff = xx1 * np.exp(-xx1**2 - xx2**2)
13+
14+
# Matplotly图像
15+
fig = plt.figure(figsize = (8,8))
16+
ax = fig.add_subplot(projection='3d')
17+
ax.plot_wireframe(xx1, xx2, ff, rstride=10, cstride=10)
18+
st.pyplot(fig)
19+
20+
# Plotly图像
21+
fig = go.Figure(data=[go.Surface(z=ff, x=xx1, y=xx2,
22+
colorscale = 'RdYlBu_r')])
23+
st.plotly_chart(fig)
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import streamlit as st
2+
button_return = st.button("Click me")
3+
st.write(button_return)
4+
st.checkbox("Check me")
5+
st.radio("Choose one:",
6+
["A", "B", "C"])
7+
st.selectbox("Choose one:",
8+
["A", "B", "C"])
9+
st.multiselect("Choose many:",
10+
["A", "B", "C", "D"])
11+
st.slider("Select a value:",
12+
0.0, 10.0, 5.0)
13+
st.select_slider("Select a value:",
14+
options=[1, 2, 3, 4, 5])
15+
st.text_input("Enter your name")
16+
st.number_input("Enter a number")
17+
st.text_area("Enter your message")
18+
st.date_input("Select a date")
19+
st.time_input("Select a time")
20+
st.file_uploader("Upload a file")
21+
st.color_picker("Pick a color")
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import streamlit as st
2+
import numpy as np
3+
from sympy import symbols,lambdify
4+
import matplotlib.pyplot as plt
5+
6+
# 侧边框
7+
with st.sidebar:
8+
st.header('Choose coefficients')
9+
st.latex(r'f(x) = ax^2 + bx + c')
10+
a = st.slider("a",min_value = -5.0,
11+
max_value = 5.0,
12+
step = 0.01, value = 1.0)
13+
b = st.slider("b",min_value = -5.0,
14+
max_value = 5.0,
15+
step = 0.01, value = -2.0)
16+
c = st.slider("c",min_value = -5.0,
17+
max_value = 5.0,
18+
step = 0.01, value = -3.0)
19+
# 抛物线
20+
x = symbols('x')
21+
f_x = a*x**2 + b*x + c
22+
x_array = np.linspace(-5,5,101)
23+
f_x_fcn = lambdify(x, f_x)
24+
y_array = f_x_fcn(x_array)
25+
26+
# 主页面
27+
st.title('Qudratic function')
28+
st.latex(r'f(x) = ')
29+
st.latex(f_x)
30+
31+
# 可视化
32+
fig = plt.figure()
33+
ax = fig.add_subplot(111)
34+
ax.plot(x_array, y_array)
35+
36+
ax.set_xlim([-5, 5])
37+
ax.set_ylim([-5, 5])
38+
ax.set_aspect('equal', adjustable='box')
39+
ax.set_xlabel('x')
40+
ax.set_ylabel('f(x)')
41+
st.write(fig)
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import streamlit as st
2+
3+
# 在两列中显示不同的内容
4+
col1, col2 = st.columns(2)
5+
col1.write("This is column 1")
6+
col1.latex(r'f(x) = ax^2 + bx + c')
7+
8+
col2.write("This is column 2")
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import streamlit as st
2+
# 创建两个选项卡,每个选项卡显示不同的内容
3+
tab_A, tab_B = st.tabs(["Tab A", "Tab B"])
4+
5+
with tab_A:
6+
st.header("Tab A Title")
7+
st.write('This is Tab A.')
8+
9+
with tab_B:
10+
st.header("Tab B Title")
11+
st.write('This is Tab B.')
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import streamlit as st
2+
import seaborn as sns
3+
import plotly.express as px
4+
5+
# 显示标题
6+
st.title('Iris Dataset')
7+
8+
# 从Seaborn导入鸢尾花数据帧
9+
df = sns.load_dataset('iris')
10+
# 第一个可展开区域
11+
with st.expander("Open and view DataFrame"):
12+
# 显示数据帧
13+
st.write(df)
14+
# 第二个可展开区域
15+
with st.expander("Open and view Heatmap"):
16+
fig = px.imshow(df.iloc[:,:-1])
17+
# 显示热图
18+
st.write(fig)

0 commit comments

Comments
 (0)