Пример #1
0
def draw_and_save(filename, lines=[], points=[]):

    if lines:
        im = svgwrite.drawing.Drawing()
        lines = shift_lines(lines)
        colors = different_colors(len(lines))

        for i, line in enumerate(lines):
            im.add( im.line(start  = line[:2],\
                            end    = line[2:],\
                            stroke = 'black'))
            #stroke = 'rgb'+ str(colors[ i ]) ))
        im.saveas('img/' + filename + '.svg')

    if points:
        # watch out shift points changed
        size = shift_points(points)[:2]
        #print size
        #print "size {}".format(size)
        im = Image.new("RGB", (500, 500), (255, 255, 255))
        points = shift_points(points)
        #colors = different_colors( len( points ) )

        for i, point in enumerate(points):
            im.putpixel(point[:2], (0, 0, 0))
            im.save('img/' + filename + '.png')

    return
def draw_and_save(filename, lines=[], points=[]):

    if lines:
        im     = svgwrite.drawing.Drawing()
        lines  = shift_lines( lines )
        colors = different_colors( len(lines) )

        for i, line in enumerate( lines ):
            im.add( im.line(start  = line[:2],\
                            end    = line[2:],\
                            stroke = 'black'))
                            #stroke = 'rgb'+ str(colors[ i ]) ))
        im.saveas('img/'+ filename +'.svg')

    if points:
        # watch out shift points changed
        size   = shift_points( points )[:2]
        #print size
        #print "size {}".format(size)
        im     = Image.new("RGB", (500,500), (255,255,255))
        points = shift_points( points )
        #colors = different_colors( len( points ) )

        for i, point in enumerate( points ):
            im.putpixel(point[:2], (0,0,0))
            im.save('img/'+ filename +'.png')

    return
Пример #3
0
def plot_dataset(dataset, filename=''):

    colors = different_colors(len(dataset), real=True)
    for i, data in enumerate(dataset):
        plt.plot(data[0], data[1], 'o', color=colors[i])

    if filename:
        plt.savefig('img/' + filename + '.png')
    else:
        plt.show()

    plt.clf()
    return
Пример #4
0
def plot_dataset(dataset, filename=''):

    colors = different_colors( len(dataset), real=True )
    for i, data in enumerate(dataset):
        plt.plot(data[0], data[1], 'o', color=colors[i])

    if filename:
        plt.savefig('img/'+ filename +'.png')
    else:
        plt.show()

    plt.clf()
    return
Пример #5
0
def newton_fractal(filename='', pol=[1,0,0,-1], frame=[-2, -2, 4]):

    x0 = frame[0]
    y0 = frame[1]
    dt = frame[2]

    im     = Image.new("RGB", (500, 500), (255, 255, 255))
    roots  = np.roots( pol )
    colors = different_colors( len(roots) )

    for s, t in product(xrange(500), xrange(500)):
        z = x0 + s * dt / 500. + (y0 + t * dt / 500.) * 1j

        for i in xrange(100):

            if z**3 == 0:            
                break

            z = z - (z**3 - 1) / (3. * z**2)
            #print z
            #if np.polyval( pol, z ) == 0 or np.polyval( np.polyder( pol ), z) == 0:
            #    break
            #z = z - np.polyval( pol, z) / np.polyval( np.polyder( pol ), z)

        #print colors
        col = (0,0,0)
        for i, root in enumerate(roots):
            
            if abs(z - root) < 0.0001:
                #print abs(z - root)
                col = colors[ i ]
            #else:
            im.putpixel((s, t), col)

    if filename:
        im.save('img/'+ filename +'.png')
    else:
        im.show()
Пример #6
0
def plot_data_clustering(classes, centers, k, filename=''):

    colors = different_colors( k, real=True )

    for i in xrange(k):
        plt.plot( pairs_to_lists(classes[i])[0], pairs_to_lists(classes[i])[1],'o',
                  markersize=5,\
                  color=colors[i])
        tmp_centers = pairs_to_lists( centers[i::k] )

        plt.plot(tmp_centers[0], tmp_centers[1], '-s', color=colors[i])

    title = re.sub(r'_', ' ', filename).title()
    plt.title( title )

    if filename:
        plt.savefig('img/'+ filename +'.png')
    else:
        plt.show()

    plt.clf()
    plot_shifts(centers, k, colors, filename)
    
    return
Пример #7
0
def plot_data_clustering(classes, centers, k, filename=''):

    colors = different_colors(k, real=True)

    for i in xrange(k):
        plt.plot( pairs_to_lists(classes[i])[0], pairs_to_lists(classes[i])[1],'o',
                  markersize=5,\
                  color=colors[i])
        tmp_centers = pairs_to_lists(centers[i::k])

        plt.plot(tmp_centers[0], tmp_centers[1], '-s', color=colors[i])

    title = re.sub(r'_', ' ', filename).title()
    plt.title(title)

    if filename:
        plt.savefig('img/' + filename + '.png')
    else:
        plt.show()

    plt.clf()
    plot_shifts(centers, k, colors, filename)

    return
Пример #8
0
def mandelbrot_set(C=-0.13 + 0.75j, pol=[1,0,0], filename='', julia=False, frame=[-2,-1.5,3], coloring=0):

    x0 = frame[0]
    y0 = frame[1]
    dt = frame[2]

    im     = Image.new("RGB", (1000, 1000), (255, 255, 255))
    colors = different_colors(31)
    weight = 0
    for s, t in product(xrange(1000), xrange(1000)):
        steps = 0
        if julia:
            z = x0 + s * dt / 1000. + (y0 + t * dt / 1000.) * 1j
        else:
            C = x0 + s * dt / 1000. + (y0 + t * dt / 1000.) * 1j
            z = 0

        for i in xrange(30):
            if abs(z) > 2:
                break
            z      = z**2 + C
            steps += 1
        
        if coloring == 0:
            if abs(z) < 2:
                col = tuple(3 * [0])
                #weight += 1
            else:
                col = tuple(3 * [255])
                #weight -= 0.05

        elif coloring == 1:
            # + 15 for nicer color set
            col = colors[(15 + steps) % 30]

        elif coloring == 2:
            if abs(max(z.real, z.imag)) < 2:
                col = tuple(3 * [0])
                weight += 1
            else:
                col = tuple(3 * [255])
                weight -= 0.05

#        HSV_tuples = [(z.real * 1.0 /  31, 1, 0.85) for x in range(31) ]
#        RGB_tuples = map(lambda x: colorsys.hsv_to_rgb( *x ), HSV_tuples)
#        RGB_tuples = map(lambda x: tuple(map(lambda y: int(y * 255),x)),RGB_tuples)
#
#        if abs(z) < 2:
#            col = tuple(3 * [0])
#        col = tuple( map( lambda x: int(x), [255 * z.real, 255*z.imag, 255*(z.real + z.imag)]))
#        col = tuple( map( lambda x: int(x), [255*(steps), 255 * z.real, 255*z.imag]))
#        col = colors[steps]
#        else:
#        col = tuple(3 * [255])

        im.putpixel((s, t), col)

#    if weight < 0:
#        print "Not enough points in the frame"
#        print ">>> Weight: {}".format( weight )
#        print ">>> C:      {}".format( C )
#
#        return
#    else:
#        print "Enough points"
#        print ">>> Weight: {}".format( weight )
#        print "+++ {}".format( filename +'.png' )
#        print ">>> C:      {}".format( C )

    if filename:
        im.save('img/'+ filename +'.png')
    else:
        im.show()