Mon Sep 17 2018
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
#!/usr/bin/env python3
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import colors
import pandas as pd
%matplotlib inline

def mandelbrot(input, limit=1000):
    # store in an intermediate variable not to confuse input with current state
    buffer = input
    for i in range(limit):
    # dont exceed the limit
    # skip 0th round because output 0 has semantic meaning
        if abs(buffer)>2:
            # assume buffer > 2 means infinity
            # if so, return the iteration at which it "failed"
            return i
        # multiply number by itself and add the constant
        buffer *= buffer
        buffer += input
    # assume if we iterate through limit, the input is in the set
    return 0

mandelbrot(1+1j, 1)


def goManGo(x_boundary, y_boundary, resolution, limit=100):
    """
        x_boundary = (x_min, x_max), floats
        y_boundary = (y_min, y_max), floats
        resolution = (width, height), ints
        limit, int
    """
    complex_x, complex_y = np.linspace(x_boundary[0], x_boundary[1], resolution[0]), np.linspace(y_boundary[0], y_boundary[1], resolution[1])
    rectangle = np.zeros(shape=resolution)
    # make the array and populate with complex numbers
    for i in range(resolution[0]):
        # for the width
        for j in range(resolution[1]):
            # for the height
            rectangle[i, j] = mandelbrot(complex(complex_x[i], complex_y[j]), limit)
    return rectangle

# -2.0,0.5,-1.25,1.25,1000,1000,80
def makePic(mandelbrot_set, cmap):
    plt.contourf(mandelbrot_set.T, cmap=cmap)

def mandelbrot_image(xmin=-2.5, xmax=0.5, ymin=-1, ymax=1,width=10,height=10,maxiter=256):
    resolution = (1000,1000)
    color_list = plt.cm.Set3(np.linspace(0, 1, 12))
    dpi = 72
    img_width, img_height = dpi * width, dpi * height
    z = goManGo((xmin,xmax),(ymin,ymax),resolution,maxiter)
    fig, ax = plt.subplots(figsize=(width, height),dpi=72)
    ticks = np.arange(0,img_width,3*dpi)
    x_ticks, y_ticks = xmin + (xmax-xmin)*ticks/img_width, ymin + (ymax-ymin)*ticks/img_width
    plt.xticks(ticks, x_ticks)
    plt.yticks(ticks, y_ticks)
    plt.colormaps()
    ax.imshow(z.T,cmap='hot',origin='lower')
    fig.savefig('mandelbrot.png')

mandel = goManGo((-2.0,0),(-1.25,0),(10000,10000), 100)
makePic(mandel, 'hot')








mandelbrot_image()