Skip to content

Instantly share code, notes, and snippets.

@NT7S
Last active March 10, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NT7S/09624501d78a79fc7e8f to your computer and use it in GitHub Desktop.
Save NT7S/09624501d78a79fc7e8f to your computer and use it in GitHub Desktop.
Draw the Lorenz system in Python/GTK
# lorenz.py
# 5 March 2016
#
# Copyright 2016 Jason Milldrum
#
# Draw the Lorenz system in a GTK window
# See https://en.wikipedia.org/wiki/Lorenz_system
import math
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Lorenz Attractor")
self.move(100, 100)
self.drawing = Gtk.DrawingArea()
self.drawing.set_size_request(400, 400)
self.drawing.connect("draw", self.draw)
self.add(self.drawing)
self.coords = []
def draw(self, widget, cr):
center_x = self.get_size()[0] / 2
center_y = self.get_size()[1] / 2
scale_x = self.get_size()[0] / 60
scale_y = self.get_size()[1] / 60
offset_y = center_y * 0.9
cr.set_line_width(1)
cr.set_source_rgb(0.0, 0.0, 0.0)
cr.move_to(center_x, center_y - offset_y)
for i in self.coords:
# Draw the X-Z view
cr.line_to((i[0] * scale_x) + center_x, (i[2] * scale_y) + center_y - offset_y)
cr.stroke()
def lorenz(self):
h = 0.01
sigma = 10.0
rho = 28.0
beta = 8.0 / 3.0
x0 = 0.1
y0 = 0.0
z0 = 0.0
for i in range(10000):
x1 = x0 + h * sigma * (y0 - x0)
y1 = y0 + h * (x0 * (rho - z0) - y0)
z1 = z0 + h * (x0 * y0 - beta * z0)
x0 = x1
y0 = y1
z0 = z1
self.coords.append([x0, y0, z0])
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
win.lorenz()
Gtk.main()
@NT7S
Copy link
Author

NT7S commented Mar 5, 2016

lorenz attractor_002

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment