forked from Visualize-ML/Book2_Beauty-of-Data-Visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamlit_Dirichlet_distribution.py
56 lines (42 loc) · 1.85 KB
/
Streamlit_Dirichlet_distribution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import streamlit as st
import plotly.figure_factory as ff
from scipy.stats import dirichlet
import numpy as np
import matplotlib.tri as tri
with st.sidebar:
st.title('Dirichlet distribution')
alpha_1 = st.slider('alpha_1', 1.0, 5.0, step = 0.1, value = 2.0)
alpha_2 = st.slider('alpha_2', 1.0, 5.0, step = 0.1, value = 2.0)
alpha_3 = st.slider('alpha_3', 1.0, 5.0, step = 0.1, value = 2.0)
alpha_array = [alpha_1,alpha_2,alpha_3]
# 定义等边三角形
corners = np.array([[0, 0], [1, 0], [0.5,0.75**0.5]]).T
triangle = tri.Triangulation(corners[0,:], corners[1,:])
refiner = tri.UniformTriRefiner(triangle)
trimesh_5 = refiner.refine_triangulation(subdiv=5)
# 自定义函数
def xy2bc(trimesh_8):
# 每个列向量代表一个三角网格坐标点
r_array = np.row_stack((trimesh_5.x,trimesh_5.y))
r1 = corners[:,[0]]
r2 = corners[:,[1]]
r3 = corners[:,[2]]
T = np.column_stack((r1 - r3,r2 - r3))
theta_1_2 = np.linalg.inv(T) @ (r_array - r3)
theta_3 = 1 - theta_1_2[0,:] - theta_1_2[1,:]
theta_1_2_3 = np.row_stack((theta_1_2,theta_3))
theta_1_2_3 = np.clip(theta_1_2_3, 1e-6, 1.0 - 1e-6)
theta_1_2_3 = theta_1_2_3/theta_1_2_3.sum(axis = 0)
# 归一化
return theta_1_2_3
tri_coordinates = xy2bc(trimesh_5)
PDF = dirichlet.pdf(tri_coordinates, alpha_array)
fig = ff.create_ternary_contour(tri_coordinates, PDF,
pole_labels=['theta_3',
'theta_1',
'theta_2'],
interp_mode='cartesian',
ncontours=12,
colorscale='Viridis',
showscale=True,)
st.plotly_chart(fig, use_container_width=True)