帮我找绘制三分康托集的python代码
以下是绘制三分康托集的Python代码:
import matplotlib.pyplot as plt
import numpy as np
def ternary_cantor_set(n):
if n == 0:
return np.array([[0, 1]])
else:
previous_set = ternary_cantor_set(n-1)
left_third = previous_set / 3
right_third = (previous_set + 2) / 3
middle_third = np.concatenate([left_third[:, 1:], right_third[:, :-1]], axis=1)
return np.concatenate([left_third, middle_third, right_third], axis=1)
cantor_set = ternary_cantor_set(10)
plt.figure(figsize=(10, 5))
plt.plot(cantor_set[1], np.zeros_like(cantor_set[1]), 'o', markersize=1)
plt.title('Ternary Cantor Set')
plt.show()
这段代码将绘制一个三分康托集的图形,其中 n 变量控制迭代次数,值越大,生成的康托集就越详细
原文地址: http://www.cveoy.top/t/topic/hoze 著作权归作者所有。请勿转载和采集!