简体   繁体   English

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

[英]Density plot of chaos game using Python matplotlib?

Essentially all I'm trying to do is produce as set of points via an IFS and use a color map to show the multiplicity of each point.基本上我想要做的就是通过 IFS 生成一组点,并使用颜色图来显示每个点的多重性。 In other words, if we assume a color map where high values are more yellow and lower ones are more red, then values repeatedly produced by the IFS will be more yellow.换句话说,如果我们假设一个颜色图,其中高值更黄,低值更红,那么 IFS 重复生成的值将更黄。

I'm struggling to get correct results for this.我正在努力为此获得正确的结果。 Each thing I've tried has resulted in an image that looks interesting, but is clearly incorrect as it differs wildly from what you get from simply plotting the points without color mapping.我尝试过的每件事都产生了一个看起来很有趣的图像,但显然是不正确的,因为它与您通过简单地绘制没有颜色映射的点所得到的结果大不相同。

Below is the base code that I'm comfortable with, without the failed attempts at color mapping.下面是我熟悉的基本代码,没有失败的颜色映射尝试。 What can I do to get a proper color map?我该怎么做才能获得正确的颜色图?

The basic strategy, I think, is to make a matrix 'mat' holding the point multiplicities and do something like plt.imshow(xs, ys, c=mat. cmap="...").我认为,基本策略是制作一个包含点多重性的矩阵“mat”并执行类似 plt.imshow(xs, ys, c=mat.cmap="...") 的操作。 I've tried different approaches to this but keep coming up with incorrect results.我尝试了不同的方法来解决这个问题,但不断提出不正确的结果。

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()

If I understand your problem, it sounds like you want to use a 2D histogram to get the density of points,如果我理解您的问题,听起来您想使用 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()

Which gives,这使,

在此处输入图片说明

This is a transparent colormesh plotted over the scatter plot.这是绘制在散点图上的透明彩色网格。 You could also colour your scatter plot by the value at point,您还可以通过点处的值为散点图着色,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM