如何改善使用matplotlib制作的Julia集的不良视觉效果? - python

如果运行下面包含的代码(python 2.7),您会发现生成的图像暗淡且模糊,几乎似乎有线条在上面穿过。我意识到使用散点图功能绘制此类内容可能是对功能的滥用。

我仔细阅读了文档,却迷失了,只是希望有人能告诉我如何使我的Julia套看起来像在书本和在线上看到的漂亮的彩色板一样漂亮。

import numpy as np
import matplotlib.pyplot as plt

# Plot ranges
r_min, r_max = -2.0, 2.0
c_min, c_max = -2.0, 2.0

# Even intervals for points to compute orbits of
r_range = np.arange(r_min, r_max, (r_max - r_min) / 200.0)
c_range = np.arange(c_min, c_max, (c_max - c_min) / 200.0)

c = complex(-0.624, 0.435)
xs = []
ys = []
colors = []

for comp in c_range:
    for real in r_range:
        z = complex(real, comp)

        escaped = False
        for i in range(0, 50):
            z = z*z + c

            if abs(z) > max(abs(c), 2):
                escaped = True

                # Colors correspond to escape speed
                if i < 7:
                    colors.append((1.0 - .055* i, 0.0, 0.0))

                if i >= 7 and i < 14:
                    colors.append((1.0 - .025*i, .6 - .025*i, 0))

                if i >= 14 and i < 21:
                    colors.append((1.0 - .0035*i, 1.0 - .0045*i, 0.0))

                if i >= 21 and i < 28:
                    colors.append((0.0, 1.0 - .0045*i, 0.0))

                if i >= 28 and i < 35:
                    colors.append((0.0, 0.0, 1.0 - .0055*i))

                if i >= 35 and i < 42:
                    colors.append((.435 - .0055*i, 0.0, 1.0 - .0055*i))    

                if i >= 42:
                    colors.append((0.62 - .005*i, 0, 1.0 - .005*i))
                break


        xs.append(real)
        ys.append(comp)

        # Points that don't escape are black
        if escaped == False:
            colors.append((0.0, 0.0, 0.0))

plt.axis([-2, 2, -2, 2])
plt.xlabel('x0')
plt.ylabel('c')
plt.scatter(xs, ys, c = colors, alpha = .2)
plt.show()

编辑:这是上面的结果-https://imgur.com/bdtZGVh

python大神给出的解决方案

有三种主要方法可以改善此情节:

1-代替散布图,创建一个N x N矩阵,其中每个点的值确定该点的颜色。然后使用plt.imshow(...)

2-试用不同的颜色图(plt.imshow(...cmap="RdGy"))

3-增加点数以提高清晰度。也就是说,在定义c_ranger_range的语句中增加分母

我已经编辑了您的代码以实现这些更改。查找# CHANGED注释。新的数字看起来好多了。

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# Plot ranges
r_min, r_max = -2.0, 2.0
c_min, c_max = -2.0, 2.0

# Even intervals for points to compute orbits of
# CHANGED
r_range = np.arange(r_min, r_max, (r_max - r_min) / 500.0)
c_range = np.arange(c_min, c_max, (c_max - c_min) / 500.0)

c = complex(-0.624, 0.435)
xs = []
ys = []
# CHANGED
mat = np.zeros((len(c_range),len(r_range)))
colors = []

# CHANGED
matComp = 0 # Index of the new mat values
matReal = 0
for comp in c_range:
    for real in r_range:
        z = complex(real, comp)

        escaped = False
        for i in range(0, 50):
            z = z*z + c

            if abs(z) > max(abs(c), 2):
                escaped = True
                # CHANGED
                mat[matComp, matReal]=i

                # Colors correspond to escape speed
                if i < 7:
                    colors.append((1.0 - .055* i, 0.0, 0.0))

                if i >= 7 and i < 14:
                    colors.append((1.0 - .025*i, .6 - .025*i, 0))

                if i >= 14 and i < 21:
                    colors.append((1.0 - .0035*i, 1.0 - .0045*i, 0.0))

                if i >= 21 and i < 28:
                    colors.append((0.0, 1.0 - .0045*i, 0.0))

                if i >= 28 and i < 35:
                    colors.append((0.0, 0.0, 1.0 - .0055*i))

                if i >= 35 and i < 42:
                    colors.append((.435 - .0055*i, 0.0, 1.0 - .0055*i))    

                if i >= 42:
                    colors.append((0.62 - .005*i, 0, 1.0 - .005*i))
                break
        # CHANGED
        matReal += 1


        xs.append(real)
        ys.append(comp)

        # Points that don't escape are black
        if escaped == False:
            colors.append((0.0, 0.0, 0.0))
    # CHANGED
    matComp+=1
    matReal=0

#CHANGED
fig = plt.figure(figsize=(15,15))
plt.imshow(mat, cmap="RdGy", extent=[-2,2,-2,2])