使用 Python matplotlib 的混沌游戏密度图?

Density plot of chaos game using Python matplotlib?

基本上我要做的就是通过 IFS 生成一组点,并使用彩色图显示每个点的多样性。换句话说,如果我们假设一个颜色图,其中高值更黄,低值更红,那么 IFS 重复生成的值将更黄。

我正在努力为此获得正确的结果。我尝试过的每一件事都产生了一个看起来很有趣的图像,但显然是不正确的,因为它与你在没有颜色映射的情况下简单地绘制点的结果大不相同。

下面是我熟悉的基本代码,没有失败的颜色映射尝试。我该怎么做才能获得正确的颜色图?

我认为,基本策略是制作一个矩阵 'mat' 来保存点的多重性,然后做类似 plt.imshow(xs, ys, c=mat.cmap="... ”)。我尝试了不同的方法来解决这个问题,但总是得出不正确的结果。

import numpy as np
import matplotlib.pyplot as plt
import random

def f(x, y, n):
    N = np.array([[x, y]])

    M = np.array([[1, 0], [0, 1]])

    b = np.array([[.5], [0]])
    b2 = np.array([[0], [.5]])

    if n == 0:
        return np.dot(M, N.T)

    elif n == 1:
       return np.dot(M, N.T) + b

    elif n == 2:
        return np.dot(M, N.T) + b2

    elif n == 3:
        return np.dot(M, N.T) - b

    elif n == 4:
        return np.dot(M, N.T) - b2


xs = [] # x coordinates
ys = [] # y coordinates
D = {}  # point multiplicities 

random.seed()

x = 1
y = 1

for i in range(0, 100000):
    n = random.randint(1, 4)

    V = f(x, y, n)

    x = V.item(0)
    y = V.item(1)

    xs.append(x)
    ys.append(y)

    xi = round(x, 3)
    yi = round(y, 3)

    if (xi, yi) in D:
        D[(xi, yi)] += 1
    else:   
        D[(xi, yi)] = 1

plt.xlabel('x')
plt.ylabel('y')

plt.scatter(xs,ys, s=.05)
plt.autoscale(True, True, True)
plt.show()

如果我理解你的问题,听起来你想使用 2D 直方图来获取点的密度,

H, x, y = np.histogram2d(xs,ys,bins=100)
X, Y = np.meshgrid(x[:-1],y[:-1],indexing='ij')
plt.pcolormesh(X,Y,H,alpha=0.8, cmap = plt.cm.YlOrRd_r)
plt.colorbar()

这给出了,

这是绘制在散点图上的透明颜色网格。 您还可以根据点的值为散点图着色,

pc = some_fn_to_get_color_at_points(X, Y, H, xs, yx)
plt.scatter(xs,ys, s=.05, c=pc)