迭代公式的指数,使用的1+5j,这是个复数。所以是广义mandelbrot集,大家能够自行改动指数,得到其它图形。各种库安装不全的,自行想办法,能够在这个站点找到差点儿全部的python库

http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib

#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:N*1j, x0:x1:N*1j]
c = x + y*1j 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 xrange(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)

广义mandelbrot集,使用python的matplotlib绘制,支持放大缩小的更多相关文章

  1. Python——使用matplotlib绘制柱状图

    Python——使用matplotlib绘制柱状图 1.基本柱状图           首先要安装matplotlib(http://matplotlib.org/api/pyplot_api.htm ...

  2. python使用matplotlib绘制折线图教程

    Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...

  3. Python使用matplotlib绘制三维曲线

    本文主要演示如何使用matplotlib绘制三维图形 代码如下: # -*- coding: UTF-8 -*- import matplotlib as mpl from mpl_toolkits. ...

  4. python包matplotlib绘制图像

    使用matplotlib绘制图像 import matplotlib.pyplot as plt from matplotlib.pyplot import MultipleLocator impor ...

  5. Python 使用 matplotlib绘制3D图形

    3D图形在数据分析.数据建模.图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何在Python中使用 matplotlib进行3D图形的绘制,包括3D散点.3D表面.3D轮廓.3D直线( ...

  6. Python:matplotlib绘制直方图

    使用hist方法来绘制直方图:     绘制直方图,最主要的是一个数据集data和需要划分的区间数量bins,另外你也可以设置一些颜色.类型参数: plt.hist(np.random.randn(1 ...

  7. Python:matplotlib绘制线条图

    线型图是学习matplotlib绘图的最基础案例.我们来看看具体过程:  下面我们将两条曲线绘制到一个图形里:   可以看到这种方式下,两个线条共用一个坐标轴,并且自动区分颜色. plot方法的核心是 ...

  8. 【Python】matplotlib绘制折线图

    一.绘制简单的折线图 import matplotlib.pyplot as plt squares=[1,4,9,16,25] plt.plot(squares) plt.show() 我们首先导入 ...

  9. Python:matplotlib绘制散点图

    与线型图类似的是,散点图也是一个个点集构成的.但不同之处在于,散点图的各点之间不会按照前后关系以线条连接起来. 用plt.plot画散点图     奇怪,代码和前面的例子差不多,为什么这里显示的却是散 ...

随机推荐

  1. kafka producer参数详解

    https://my.oschina.net/u/218540/blog/1794669/

  2. PIE SDK克里金插值法

    1.算法功能简介 克里金插值法基于一般最小二乘法的随机插值技术没用方差图作为权重函数,被应用于任何点数据估计其在地表上分布的现象,被称为空间自协方差最佳插值法,是一种最优内插法也是一种最常用的空间插值 ...

  3. Docker容器日志查看与清理(亲测有效)

    https://blog.csdn.net/yjk13703623757/article/details/80283729 1. 问题 docker容器日志导致主机磁盘空间满了.docker logs ...

  4. Farey Sequence(欧拉函数板子题)

    题目链接:http://poj.org/problem?id=2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. (转)Shell中获取字符串长度的七种方法

    Shell中获取字符串长度的七种方法 原文:http://blog.csdn.net/jerry_1126/article/details/51835119 求字符串操作在shell脚本中很常用,下面 ...

  6. org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.

    今天报了这个异常,这是页面报的 org.springframework.dao.DataIntegrityViolationException: could not execute statement ...

  7. pat03-树3. Tree Traversals Again (25)

    03-树3. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  8. JavaScript字符串去除空格

    /*字符串去除空格*/ String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, "") ...

  9. linux安装git、node、pm2

    一.安装Git 下载:# wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz 解压:# tar -zxvf git-2. ...

  10. dubbo-admin网页管理控制台

    由于近段时间在看dubbo,网上找到的这个war包发布到tomcat报错,故从git(https://github.com/apache/incubator-dubbo-ops)上重新下载编译了版本 ...