huhan3000/visualization/kym_chain_visualizer.py

270 lines
10 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
K/Y/M三链理论可视化工具
基于权力符号考古学理论,可视化展示三链功能关系和文明发展轨迹
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle, Wedge, Polygon
from matplotlib.collections import PatchCollection
import matplotlib.patches as mpatches
class KYMChainVisualizer:
"""K/Y/M三链理论可视化类"""
def __init__(self):
self.fig = None
self.ax = None
# 三链颜色定义
self.chain_colors = {
'K': '#1f77b4', # 蓝色 - 秩序
'Y': '#2ca02c', # 绿色 - 道德
'M': '#d62728' # 红色 - 信仰
}
# 三链功能描述
self.chain_descriptions = {
'K': {'name': 'K链秩序', 'function': '宇宙的"主程序",负责空间秩序'},
'Y': {'name': 'Y链道德', 'function': '"执行者",负责道德和物理操作'},
'M': {'name': 'M链信仰', 'function': '"信仰",负责时间预测和救赎'}
}
def create_functional_triangle(self, k_weight, y_weight, m_weight, title="三链功能关系图"):
"""创建三链功能三角形图"""
self.fig, self.ax = plt.subplots(1, 1, figsize=(10, 8))
# 创建等边三角形
triangle_vertices = np.array([
[0, 0], # K顶点左下
[1, 0], # Y顶点右下
[0.5, np.sqrt(3)/2] # M顶点顶部
])
# 绘制三角形边框
triangle = Polygon(triangle_vertices, closed=True,
fill=False, edgecolor='black', linewidth=2)
self.ax.add_patch(triangle)
# 计算重心坐标(功能平衡点)
total_weight = k_weight + y_weight + m_weight
k_norm = k_weight / total_weight
y_norm = y_weight / total_weight
m_norm = m_weight / total_weight
# 计算功能点在三角形内的坐标
x = k_norm * triangle_vertices[0][0] + y_norm * triangle_vertices[1][0] + m_norm * triangle_vertices[2][0]
y = k_norm * triangle_vertices[0][1] + y_norm * triangle_vertices[1][1] + m_norm * triangle_vertices[2][1]
# 绘制功能点
self.ax.scatter(x, y, s=200, c='gold', edgecolors='black', linewidth=2, zorder=5)
# 绘制到各顶点的连线
for i, (vertex, color, weight) in enumerate(zip(triangle_vertices,
[self.chain_colors['K'], self.chain_colors['Y'], self.chain_colors['M']],
[k_weight, y_weight, m_weight])):
self.ax.plot([x, vertex[0]], [y, vertex[1]],
color=color, linewidth=weight*2, alpha=0.7)
# 添加顶点标签
self.ax.text(triangle_vertices[0][0]-0.1, triangle_vertices[0][1]-0.1,
'K链\n秩序', ha='right', va='top',
bbox=dict(boxstyle="round,pad=0.3", facecolor=self.chain_colors['K'], alpha=0.8))
self.ax.text(triangle_vertices[1][0]+0.1, triangle_vertices[1][1]-0.1,
'Y链\n道德', ha='left', va='top',
bbox=dict(boxstyle="round,pad=0.3", facecolor=self.chain_colors['Y'], alpha=0.8))
self.ax.text(triangle_vertices[2][0], triangle_vertices[2][1]+0.1,
'M链\n信仰', ha='center', va='bottom',
bbox=dict(boxstyle="round,pad=0.3", facecolor=self.chain_colors['M'], alpha=0.8))
# 添加功能点标签
self.ax.text(x, y-0.05, f'功能平衡点\nK:{k_weight} Y:{y_weight} M:{m_weight}',
ha='center', va='top', fontsize=10,
bbox=dict(boxstyle="round,pad=0.3", facecolor='lightgray'))
# 设置图形属性
self.ax.set_xlim(-0.2, 1.2)
self.ax.set_ylim(-0.2, 1.2)
self.ax.set_aspect('equal')
self.ax.axis('off')
self.ax.set_title(title, fontsize=16, fontweight='bold', pad=20)
return self.fig, self.ax
def create_civilization_timeline(self, civilizations_data):
"""创建文明发展时间线图"""
fig, ax = plt.subplots(1, 1, figsize=(15, 8))
# 设置时间轴
time_range = (-3000, 2000) # 从公元前3000年到公元2000年
ax.set_xlim(time_range)
# 创建文明轨迹
y_positions = {}
current_y = 0
for civ in civilizations_data:
# 为每个文明分配y轴位置
if civ['name'] not in y_positions:
y_positions[civ['name']] = current_y
current_y += 1
y_pos = y_positions[civ['name']]
# 绘制文明时间段
start_time = civ['start']
end_time = civ['end']
# 计算三链权重
k_weight = civ.get('k_weight', 0.33)
y_weight = civ.get('y_weight', 0.33)
m_weight = civ.get('m_weight', 0.34)
# 创建颜色渐变(基于三链权重)
color = np.array([
self.chain_colors['K'][0] * k_weight,
self.chain_colors['Y'][1] * y_weight,
self.chain_colors['M'][2] * m_weight
])
# 绘制时间段
ax.plot([start_time, end_time], [y_pos, y_pos],
color=color, linewidth=8, alpha=0.8)
# 添加文明标签
ax.text(start_time, y_pos + 0.1, civ['name'],
fontsize=10, ha='left', va='bottom')
# 添加三链权重标签
ax.text((start_time + end_time)/2, y_pos - 0.1,
f'K:{k_weight:.2f} Y:{y_weight:.2f} M:{m_weight:.2f}',
fontsize=8, ha='center', va='top')
# 设置图形属性
ax.set_xlabel('时间(公元前/公元)', fontsize=12)
ax.set_ylabel('文明', fontsize=12)
ax.set_title('文明发展时间线与三链权重变化', fontsize=16, fontweight='bold')
# 添加图例
legend_elements = [
mpatches.Patch(color=self.chain_colors['K'], label='K链秩序'),
mpatches.Patch(color=self.chain_colors['Y'], label='Y链道德'),
mpatches.Patch(color=self.chain_colors['M'], label='M链信仰')
]
ax.legend(handles=legend_elements, loc='upper right')
# 添加时间网格
ax.grid(True, alpha=0.3)
return fig, ax
def create_chain_network(self, nodes, edges):
"""创建三链关联网络图"""
fig, ax = plt.subplots(1, 1, figsize=(12, 10))
# 设置节点位置(圆形布局)
angles = np.linspace(0, 2*np.pi, len(nodes), endpoint=False)
radius = 5
node_positions = {}
for i, node in enumerate(nodes):
x = radius * np.cos(angles[i])
y = radius * np.sin(angles[i])
node_positions[node] = (x, y)
# 绘制边
for edge in edges:
source, target, weight, chain_type = edge
x1, y1 = node_positions[source]
x2, y2 = node_positions[target]
# 根据链类型设置颜色
color = self.chain_colors.get(chain_type, 'gray')
# 绘制边
ax.plot([x1, x2], [y1, y2], color=color,
linewidth=weight*3, alpha=0.7)
# 绘制节点
for node, (x, y) in node_positions.items():
# 根据节点类型设置颜色
if 'K' in node:
color = self.chain_colors['K']
elif 'Y' in node:
color = self.chain_colors['Y']
elif 'M' in node:
color = self.chain_colors['M']
else:
color = 'lightgray'
# 绘制节点
ax.scatter(x, y, s=300, c=color, edgecolors='black',
linewidth=2, alpha=0.8)
# 添加节点标签
ax.text(x, y, node, ha='center', va='center',
fontsize=9, fontweight='bold')
# 设置图形属性
ax.set_xlim(-radius-1, radius+1)
ax.set_ylim(-radius-1, radius+1)
ax.set_aspect('equal')
ax.axis('off')
ax.set_title('三链符号关联网络', fontsize=16, fontweight='bold')
return fig, ax
def demo_visualizations():
"""演示可视化功能"""
visualizer = KYMChainVisualizer()
# 演示1三链功能关系图
print("=== 演示1三链功能关系图 ===")
fig1, ax1 = visualizer.create_functional_triangle(
k_weight=0.4, y_weight=0.3, m_weight=0.3,
title="商朝晚期三链功能关系K链过度"
)
plt.show()
# 演示2文明发展时间线
print("=== 演示2文明发展时间线 ===")
civilizations = [
{'name': '商朝', 'start': -1600, 'end': -1046, 'k_weight': 0.6, 'y_weight': 0.2, 'm_weight': 0.2},
{'name': '周朝', 'start': -1046, 'end': -256, 'k_weight': 0.3, 'y_weight': 0.5, 'm_weight': 0.2},
{'name': '罗马帝国', 'start': -27, 'end': 476, 'k_weight': 0.5, 'y_weight': 0.3, 'm_weight': 0.2},
{'name': '玛雅文明', 'start': 200, 'end': 900, 'k_weight': 0.2, 'y_weight': 0.3, 'm_weight': 0.5},
{'name': '印加帝国', 'start': 1200, 'end': 1533, 'k_weight': 0.3, 'y_weight': 0.5, 'm_weight': 0.2}
]
fig2, ax2 = visualizer.create_civilization_timeline(civilizations)
plt.show()
# 演示3三链关联网络
print("=== 演示3三链关联网络 ===")
nodes = ['昆仑(K)', 'Kósmos(K)', '大禹(Y)', '印加(Y)', '摩西(M)', '玛雅(M)', '秩序', '道德', '信仰']
edges = [
('昆仑(K)', '秩序', 0.8, 'K'),
('Kósmos(K)', '秩序', 0.7, 'K'),
('大禹(Y)', '道德', 0.9, 'Y'),
('印加(Y)', '道德', 0.8, 'Y'),
('摩西(M)', '信仰', 0.9, 'M'),
('玛雅(M)', '信仰', 0.8, 'M'),
('秩序', '道德', 0.6, 'K'),
('道德', '信仰', 0.5, 'Y'),
('信仰', '秩序', 0.4, 'M')
]
fig3, ax3 = visualizer.create_chain_network(nodes, edges)
plt.show()
if __name__ == "__main__":
demo_visualizations()