Skip to content

Instantly share code, notes, and snippets.

@jsundram
jsundram / heatmat.py
Created January 18, 2013 19:52
Heatmap in python like this one on flowingdata, see stackoverflow for context: http://stackoverflow.com/questions/14391959/heatmap-in-matplotlib-with-pcolor
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4,4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
@jsundram
jsundram / running_stats.py
Created January 22, 2013 21:27
Input a number at a time, and find out the running mean and median.
import numpy
def running():
numbers = []
while True:
try:
i = input("Enter Number: ")
numbers.append(i)
print "Current median: %s" % (numpy.median(numbers))
print "Current average: %s" % (numpy.average(numbers))
@jsundram
jsundram / bug.py
Last active December 14, 2015 13:58
Using plot_date along with the cairo backend results in misaligned x-axis labels. Reported this issue: https://github.com/matplotlib/matplotlib/issues/1810
import datetime
import random
import matplotlib; matplotlib.use('cairo')
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, MonthLocator, DateFormatter, date2num
"""
Minimal(ish) repro for the x-axis label alignment bug
Collecting info suggested here: http://matplotlib.org/faq/troubleshooting_faq.html
@jsundram
jsundram / BoxyLady2.py
Last active December 15, 2015 22:19
Ported from my cleanup (https://gist.github.com/jsundram/1671003) of analogpixel's sketch: http://www.analogpixel.org/blog/2011/09/13/msced-148-boxy-lady-take-2/#more-764 About as straighforward a translation of processing into nodebox as possible.
from nodebox.graphics import *
from numpy.random import randint # random.randint is v. slow
import numpy
MINS, MAXS = 1, 40
grow = False
box_size = MAXS
counter = 1
# Use a numpy array of pixels because pic.get is too slow.
@jsundram
jsundram / draw_circle.py
Last active December 16, 2015 10:08
Port of Pavel Sountsov's DrawCircle to python. Basic idea -- avoid using sin/cos a lot (like a naive implementation) when drawing a circle.
import math
def estimate_segments(radius, segment_length=0.25):
""" Basically want segments that are about the size of .25 pixel for the given radius.
Derive as follows:
segment_length as a function of theta:
segment_length = (1 - cos(theta)) * radius
Solving for theta given segment_length:
theta = math.acos(1 - (float(segment_length) / radius))
"""
@jsundram
jsundram / alignment.py
Last active December 16, 2015 10:49 — forked from mdboom/alignment.py
allow dynamic backend setting; name files after backend they were rendered from for clarity.
import matplotlib, sys
backend = sys.argv[1]
matplotlib.use(backend) # need to call use() before importing pyplot
from matplotlib.pyplot import *
text(0.1, 0.5, "Top Tj", verticalalignment="top")
text(0.2, 0.5, "Bottom Tj", verticalalignment="bottom")
text(0.3, 0.5, "Base Tj", verticalalignment="baseline")
text(0.4, 0.5, "Center Tj", verticalalignment="center")
@jsundram
jsundram / Readme.md
Last active December 16, 2015 13:29
Follow the instructions in Readme.md to use this.

This code allows you to open up an image file and replace its pixels with stars as you move the mouse over the screen. Useful, right?

  1. Download and unzip gist from https://gist.github.com/jsundram/5441782/download
  2. Navigate to folder you unzipped this code to in Terminal
  3. chmod +x install.sh
  4. ./install.sh (this will take a while)
  5. python starry.py "filename.jpg" from the command line. Press Ctrl-S to save.
@jsundram
jsundram / BoxyLady2.pjs
Last active December 17, 2015 11:59
BoxyLady2.pjs
// code taken from here:
// http://www.analogpixel.org/blog/2011/09/13/msced-148-boxy-lady-take-2/#more-764
// and formatted for readability.
// then ported to pjs, and optimized/tweaked a bit
// NEW: added gif.js
PImage pic;
PGraphics pg;
boolean grow = false;
int MINS = 1;
int MAXS = 40;
@jsundram
jsundram / BoxyLady2.py
Last active December 17, 2015 13:38
implementation of BoxyLady2 aimed at pypy
from nodebox.graphics import *
from random import randint
import os
MINS, MAXS = 1, 40
grow = False
box_size = MAXS
counter = 1
output_dir = '/Users/%s/Downloads/nb/' % os.getlogin()
@jsundram
jsundram / image_average.py
Created June 11, 2013 18:12
Takes a folder full of images and computes their average and median. Assumes they all have the same dimensions. Makes no attempt at alignment.
from glob import glob
import numpy as np
import os
import sys
# You'll need scikit-image and PIL installed to use this
try:
import skimage.io
except ImportError:
sys.exit("please 'pip install scikit-image' to use this script")