迭代公式的指数,使用的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. 千万不要犯这种愚蠢的错误:Property 'XXX' not found on type java.lang.String

    一定是: <c:forEach var="book" items="${booklist}"> 而不是: <c:forEach var=&qu ...

  2. 一个新时代的UI设计师需要具备这些技能

    如今互联网行业发展如日中天,设计师本就是稀缺人才.再加上未来也将迎接人工智能时代,未来的设计师不只像现在只是做一些网页.APP界面,还会出现更多的UI设计衍生职业.如下列举的几大类: 一.初级阶段 1 ...

  3. oracle 错误实例分析(ORA-01126)

    问题描述 SQL> shutdown immediate ORA-01109: database not open Database dismounted. ORACLE instance sh ...

  4. zabbix-proxy 层级制监控

    一,zabbix服务规划 zabbix-server 10.0.0.71 zabbix-proxy 10.0.0.72 172.16.1.72 web01  172.16.1.7 二,zabbix 客 ...

  5. Ruby初探

    官方网站:https://www.ruby-lang.org/zh_cn/ 标准库API文档:http://ruby-doc.org/stdlib-2.3.0/ 简介特性安装Ruby 命令行选项编码语 ...

  6. How to Setup a Private Proxy Server on EC2 in Under 10 Minutes

    How to Setup a Private Proxy Server on EC2 in Under 10 Minutes I’ve been slacking a bit with regular ...

  7. 设置ibus为默认输入法

    /etc/profile  文件中编辑 export INPUT_METHOD="ibus"

  8. Class.forName和ClassLoader的区别

    一 看名字就知道了,一个是类的创建,一个类加载器 二 再看下Class.forName源码,调用了ClassLoader @CallerSensitive public static Class< ...

  9. PHP通过映射类查找类所在文件

    $reflectObj = new ReflectionClass(类名); $file_name = $reflectObj->getFileName(); var_dump($file_na ...

  10. 程序员心灵鸡汤桌面壁纸1080p 欢迎大家下载,HTML,PHP,node,css,前端

    上段时间在读书的时候看见了一句话:一张地图,不论比例多么精确,它永远不可能带着主人在地面上移动半步, 作为爱思考,爱学习的程序员 突然想到了:一份帮助文档,不论多么详细,它永远不会帮助主人敲一行代码. ...