Skip to content

Instantly share code, notes, and snippets.

@GrantTrebbin
Created February 28, 2016 14:18
Embed
What would you like to do?
Visualizing Periodic Order Schedules
import random
import math
import sqlite3
import numpy
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
def order_trajectory(base, colours, trajectory):
scale = 500
day_trajectory = trajectory / 24
number_of_points = len(day_trajectory)-1
x = numpy.linspace(0,number_of_points, number_of_points*1+1)
interp_x = numpy.linspace(0,number_of_points, number_of_points * scale + 1)
interp_day = numpy.interp(interp_x, x, day_trajectory)
radian_trajectory = 2 * numpy.pi * interp_day / 7
scaled_day = base - 15 * (interp_day - interp_day[0])
points = numpy.array([radian_trajectory, scaled_day]).T.reshape(-1, 1, 2)
segments = numpy.concatenate([points[:-1], points[1:]], axis=1)
cs = [(0.9,0.6,0.1,0.8)] * (number_of_points * scale + 1)
colorlist = []
for a in colours:
start_colour = numpy.asarray(a[0])
end_colour = numpy.asarray(a[1])
for i in range(0, scale):
gradient =((scale - i) * start_colour + i * end_colour) / scale
colorlist.append(tuple(gradient))
lc = LineCollection(segments, colors=colorlist, linewidth=5 )
return lc
plt.style.use('ggplot')
ax = plt.subplot(111, projection='polar')
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.set_xticks(numpy.arange(0, 2 * numpy.pi, 2 * numpy.pi / 7 ))
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_yticks([0, 5, 20, 35, 50, 65, 80, 95, 110, 125 ,140])
ax.set_rlim(0)
colours = [
[ [214/256, 39/256, 40/256, 1.0], [214/256, 39/256, 40/256, 1.0] ],
[ [255/256, 127/256, 14/256, 1.0], [255/256, 127/256, 14/256, 1.0] ],
[ [ 44/256, 160/256, 44/256, 1.0], [ 44/256, 160/256, 44/256, 1.0] ],
[ [ 23/256, 190/256, 207/256, 1.0], [ 23/256, 190/256, 207/256, 0.0] ]
]
or1 = order_trajectory(140, colours, numpy.array([(0 * 24 + 7.5), (2 * 24 + 12), (3 * 24 + 8), (4 * 24 + 8), (6 * 24 + 8)]))
or2 = order_trajectory(140, colours, numpy.array([(1 * 24 + 7.5), (3 * 24 + 12), (4 * 24 + 8), (5 * 24 + 8), (7 * 24 + 8)]))
or3 = order_trajectory(140, colours, numpy.array([(2 * 24 + 7.5), (4 * 24 + 12), (5 * 24 + 8), (6 * 24 + 9), (8 * 24 + 8)]))
or4 = order_trajectory(140, colours, numpy.array([(3 * 24 + 7.5), (5 * 24 + 12), (6 * 24 + 9), (9 * 24 + 9), (11 * 24 + 9)]))
or5 = order_trajectory(140, colours, numpy.array([(4 * 24 + 7.5), (8 * 24 + 12), (9 * 24 + 8), (10 * 24 + 8), (12 * 24 + 8)]))
ax.add_collection(or1)
ax.add_collection(or2)
ax.add_collection(or3)
ax.add_collection(or4)
ax.add_collection(or5)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment