![](/img/trans.png)
[英]Plot Mandelbrot with matplotlib / pyplot / numpy / python
[英]displaying Mandelbrot set in python using matplotlib.pyplot and numpy
我正在嘗試獲取 Mandelbrot 集的圖,但在繪制預期圖時遇到問題。
據我了解,Mandelbrot 集由值 c 組成,如果通過以下等式 z = z**2 + c 進行迭代,這些值會收斂。 我使用了 z = 0 的初始值。
最初,我得到了一條直線。 我在網上尋找解決方案,看看我哪里出錯了。 特別是使用以下鏈接,我試圖改進我的代碼:
https://scipy-lectures.org/intro/numpy/auto_examples/plot_mandelbrot.html
這是我改進的代碼。 我真的不明白使用 np.newaxis 的原因以及為什么我要繪制收斂的最終 z 值。 我誤解了 Mandelbrot 集的定義嗎?
# initial values
loop = 50 # number of interations
div = 600 # divisions
# all possible values of c
c = np.linspace(-2,2,div)[:,np.newaxis] + 1j*np.linspace(-2,2,div)[np.newaxis,:]
z = 0
for n in range(0,loop):
z = z**2 + c
plt.rcParams['figure.figsize'] = [12, 7.5]
z = z[abs(z) < 2] # removing z values that diverge
plt.scatter(z.real, z.imag, color = "black" ) # plotting points
plt.xlabel("Real")
plt.ylabel("i (imaginary)")
plt.xlim(-2,2)
plt.ylim(-1.5,1.5)
plt.savefig("plot.png")
plt.show()
並得到了以下圖像,它看起來比我目前得到的任何圖像都更接近 Mandelbrot 集。 但它看起來更像是一只海星,周圍散布着點。 圖片
作為參考,這是我改進前的初始代碼:
# initial values
loop = 50
div = 50
clist = np.linspace(-2,2,div) + 1j*np.linspace(-1.5,1.5,div) # range of c values
all_results = []
for c in clist: # for each value of c
z = 0 # starting point
for a in range(0,loop):
negative = 0 # unstable
z = z**2 + c
if np.abs(z) > 2:
negative +=1
if negative > 2:
break
if negative == 0:
all_results.append([c,"blue"]) #converging
else:
all_results.append([c,"black"]) # not converging
該圖看起來不正確,因為在問題中的代碼中繪制了z
(即迭代變量)。 迭代z = z*z + c
,Mandelbrot集是由那些真實的,的虛部對給定c
,為此,該系列不發散。 因此,如下所示對代碼的小改動給出了正確的 Mandelbrot 圖:
import pylab as plt
import numpy as np
# initial values
loop = 50 # number of interations
div = 600 # divisions
# all possible values of c
c = np.linspace(-2,2,div)[:,np.newaxis] + 1j*np.linspace(-2,2,div)[np.newaxis,:]
z = 0
for n in range(0,loop):
z = z**2 + c
plt.rcParams['figure.figsize'] = [12, 7.5]
p = c[abs(z) < 2] # removing c values for which z has diverged
plt.scatter(p.real, p.imag, color = "black" ) # plotting points
plt.xlabel("Real")
plt.ylabel("i (imaginary)")
plt.xlim(-2,2)
plt.ylim(-1.5,1.5)
plt.savefig("plot.png")
plt.show()
或者,對問題中的代碼進行另一個小改動,可以使用z
的值來為繪圖着色。 可以存儲n
的值,其中系列的絕對值變得大於 2(意味着它發散),並用它為 Mandelbrot 集之外的點着色:
import pylab as plt
import numpy as np
# initial values
loop = 50 # number of interations
div = 600 # divisions
# all possible values of c
c = np.linspace(-2,2,div)[:,np.newaxis] + 1j*np.linspace(-2,2,div)[np.newaxis,:]
# array of ones of same dimensions as c
ones = np.ones(np.shape(c), np.int)
# Array that will hold colors for plot, initial value set here will be
# the color of the points in the mandelbrot set, i.e. where the series
# converges.
# For the code below to work, this initial value must at least be 'loop'.
# Here it is loop + 5
color = ones * loop + 5
z = 0
for n in range(0,loop):
z = z**2 + c
diverged = np.abs(z)>2
# Store value of n at which series was detected to diverge.
# The later the series is detected to diverge, the higher
# the 'color' value.
color[diverged] = np.minimum(color[diverged], ones[diverged]*n)
plt.rcParams['figure.figsize'] = [12, 7.5]
# contour plot with real and imaginary parts of c as axes
# and colored according to 'color'
plt.contourf(c.real, c.imag, color)
plt.xlabel("Real($c$)")
plt.ylabel("Imag($c$)")
plt.xlim(-2,2)
plt.ylim(-1.5,1.5)
plt.savefig("plot.png")
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.