如何改善使用 matplotlib 制作的 Julia 集的不良视觉效果?
How to improve poor visuals of Julia sets made using matplotlib?
如果你 运行 我在下面包含的代码 (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
改进这个情节主要有以下三种方式:
1 - 创建一个 N × N 矩阵而不是散点图,其中每个点的值决定该点的颜色。然后使用 plt.imshow(...)
2 - 尝试不同的颜色图 (plt.imshow(...cmap="RdGy")
)
3 - 增加点数以提高清晰度。也就是说,在定义 c_range
和 r_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])
如果你 运行 我在下面包含的代码 (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
改进这个情节主要有以下三种方式:
1 - 创建一个 N × N 矩阵而不是散点图,其中每个点的值决定该点的颜色。然后使用 plt.imshow(...)
2 - 尝试不同的颜色图 (plt.imshow(...cmap="RdGy")
)
3 - 增加点数以提高清晰度。也就是说,在定义 c_range
和 r_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])