美文网首页Python小哥哥大数据 爬虫Python AI Sql
使用Python的matplotlib绘制广义mandelbro

使用Python的matplotlib绘制广义mandelbro

作者: 14e61d025165 | 来源:发表于2019-05-02 14:53 被阅读0次

    迭代公式的指数,使用的1+5j,这是个复数,所以是广义mandelbrot集,大家可以自行修改指数,得到其他图形。各种库安装不全的,自行想办法

    Python 3.6.7。

    Linux系统:Ubuntu 18.04.2

    完整代码:

    Python学习交流群:1004391443满足你的需求,资料都已经上传群文件,可以自行下载!

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#encoding=utf-8
    import numpy as np
    import pylab as pl
    import time
    from matplotlib import cm
    from math import log
    escape_radius = 10
    iter_num = 20
    def draw_mandelbrot2(cx, cy, d, N=600):
    global mandelbrot
    """
    绘制点(cx, cy)附近正负d的范围的Mandelbrot
    """
    x0, x1, y0, y1 = cx-d, cx+d, cy-d, cy+d
    y, x = np.ogrid[y0:y1:N1j, x0:x1:N1j]
    c = x + y1j
    smooth_mand = np.frompyfunc(smooth_iter_point,1,1)(c).astype(np.float)
    pl.gca().set_axis_off()
    pl.imshow(smooth_mand, cmap=cm.Blues_r, extent=[x0,x1,y1,y0])
    pl.show()
    def smooth_iter_point(c):
    z = c #赋初值
    d = 1+2j #这里,把幂运算的指数,设定成复数1+2j, 就是广义mandelbrot集合, d=2就是标准mandelbrot集,d=3就是三阶的
    for i in range(1, iter_num):
    if abs(z)>escape_radius: break
    z = z
    d+c # 运算符是幂运算
    #下面是重新计算迭代次数,可以获取连续的迭代次数(即正规化)
    absz = abs(z) #复数的模
    if absz > 2.0:
    mu = i - log(log(abs(z),2),2)
    else:
    mu = i
    return mu # 返回正规化的迭代次数
    def draw_mandelbrot(cx, cy, d, N=800):
    """
    绘制点(cx, cy)附近正负d的范围的Mandelbrot
    """
    global mandelbrot
    x0, x1, y0, y1 = cx-d, cx+d, cy-d, cy+d
    y, x = np.ogrid[y0:y1:N
    1j, x0:x1:N
    1j]
    c = x + y
    1j
    # 创建X,Y轴的坐标数组
    ix, iy = np.mgrid[0:N,0:N]
    # 创建保存mandelbrot图的二维数组,缺省值为最大迭代次数
    mandelbrot = np.ones(c.shape, dtype=np.int)*100
    # 将数组都变成一维的
    ix.shape = -1
    iy.shape = -1
    c.shape = -1
    z = c.copy() # 从c开始迭代,因此开始的迭代次数为1
    start = time.clock()
    for i in xrange(1,100):
    # 进行一次迭代
    z *= z
    z += c
    # 找到所有结果逃逸了的点
    tmp = np.abs(z) > 2.0
    # 将这些逃逸点的迭代次数赋值给mandelbrot图
    mandelbrot[ix[tmp], iy[tmp]] = i
    # 找到所有没有逃逸的点
    np.logical_not(tmp, tmp)
    # 更新ix, iy, c, z只包含没有逃逸的点
    ix,iy,c,z = ix[tmp], iy[tmp], c[tmp],z[tmp]
    if len(z) == 0: break
    print ("time="),time.clock() - start
    pl.imshow(mandelbrot, cmap=cm.Blues_r, extent=[x0,x1,y1,y0])
    pl.gca().set_axis_off()
    pl.show()

    鼠标点击触发执行的函数

    def on_press(event):
    global g_size
    print (event)
    print (dir(event))
    newx = event.xdata
    newy = event.ydata
    print (newx)
    print (newy)
    #不合理的鼠标点击,直接返回,不绘制
    if newx == None or newy == None or event.dblclick == True:
    return None
    #不合理的鼠标点击,直接返回,不绘制
    if event.button == 1: #button ==1 代表鼠标左键按下, 是放大图像
    g_size /= 2
    elif event.button == 3: #button == 3 代表鼠标右键按下, 是缩小图像
    g_size *= 2
    else:
    return None
    print (g_size)
    draw_mandelbrot2(newx,newy,g_size)
    fig, ax = pl.subplots(1)
    g_size = 2.5

    注册鼠标事件

    fig.canvas.mpl_connect('button_press_event', on_press)

    初始绘制一个图

    draw_mandelbrot2(0,0,g_size)
    </pre>

    效果图如下:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556779953778" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    相关文章

      网友评论

        本文标题:使用Python的matplotlib绘制广义mandelbro

        本文链接:https://www.haomeiwen.com/subject/oyunnqtx.html