Python 代码绘制三分康托集
以下是绘制三分康托集的 Python 代码示例:
import matplotlib.pyplot as plt
def ternary_cantor_set(depth):
if depth == 0:
return [0, 1]
else:
last_set = ternary_cantor_set(depth-1)
new_set = []
for i in range(len(last_set)-1):
new_set.append(last_set[i])
new_set.append((last_set[i]+last_set[i+1])/3)
new_set.append((last_set[i]+2*last_set[i+1])/3)
new_set.append(last_set[-1])
return new_set
depth = 7
cantor_set = ternary_cantor_set(depth)
fig, ax = plt.subplots(figsize=(8, 4))
ax.scatter(x=cantor_set, y=[0]*len(cantor_set), s=1)
ax.set_xlim(0, 1)
ax.set_ylim(-0.1, 0.1)
ax.set_axis_off()
plt.show()
在此示例中,我们使用递归函数 ternary_cantor_set() 来生成三分康托集,并将其在 matplotlib 中显示出来。您可以根据需要调整深度和图形参数。
原文地址: https://www.cveoy.top/t/topic/oRoY 著作权归作者所有。请勿转载和采集!