Skip to content

Commit f277b39

Browse files
author
Director of Analytics
committed
added scatterplot support
1 parent 011a6cb commit f277b39

3 files changed

Lines changed: 92 additions & 13 deletions

File tree

bin/getlatlng.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SELECT
2+
lat::varchar || ',' || lng
3+
FROM
4+
sf_coords
5+
WHERE
6+
lng < -107
7+
;

bin/hist.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import math
22
from collections import Counter
33
import optparse
4+
import sys
45

56

67
def drange(start, stop, step=1.0):
@@ -18,14 +19,23 @@ def calc_bins(n, min_val, max_val, h=None):
1819
for b in drange(min_val, max_val, bin_width):
1920
yield b
2021

21-
def plot_hist(f, height=20, bincount=None):
22+
def read_numbers(numbers):
23+
if isinstance(numbers, list):
24+
for n in numbers:
25+
yield float(n.strip())
26+
else:
27+
for n in open(numbers):
28+
yield float(n.strip())
29+
30+
def plot_hist(f, height=20, bincount=None, pch="o"):
2231
"plot a histogram given a file of numbers"
2332
#first apss
33+
if pch is None:
34+
pch = "o"
2435
min_val, max_val = None, None
2536
n = 0.
26-
for number in open(f):
37+
for number in read_numbers(f):
2738
n += 1
28-
number = float(number)
2939

3040
if not min_val or number < min_val:
3141
min_val = number
@@ -34,8 +44,7 @@ def plot_hist(f, height=20, bincount=None):
3444

3545
bins = list(calc_bins(n, min_val, max_val, bincount))
3646
hist = Counter()
37-
for number in open(f):
38-
number = float(number)
47+
for number in read_numbers(f):
3948
for i, b in enumerate(bins):
4049
if number < b:
4150
hist[i-1] += 1
@@ -60,7 +69,7 @@ def plot_hist(f, height=20, bincount=None):
6069

6170
for i in range(len(hist)):
6271
if y < hist[i]:
63-
print "o",
72+
print pch,
6473
else:
6574
print " ",
6675
print
@@ -71,14 +80,14 @@ def plot_hist(f, height=20, bincount=None):
7180
for i in range(0, nlen):
7281
print " "*(nlen+1),
7382
for x in range(0, len(hist)):
74-
n = str(bins[x])
83+
num = str(bins[x])
7584
if x%2==0:
7685
print " ",
77-
elif i < len(n):
78-
print n[i],
86+
elif i < len(num):
87+
print num[i],
7988
print
8089

81-
summary = "Summary\n--------\nMax: %s\nMin:%s" % (min_val, max_val)
90+
summary = "Summary\n--------\nMax: %s\nMin: %s\nCount: %s" % (min_val, max_val, int(n))
8291
print summary
8392

8493

@@ -89,15 +98,15 @@ def plot_hist(f, height=20, bincount=None):
8998
default=None, dest='f')
9099
parser.add_option('-b', '--bins', help='number of bins in the histogram',
91100
default=None, dest='b')
92-
parser.add_option('-s', help='height of the histogram (in lines)',
101+
parser.add_option('-s', '--height', help='height of the histogram (in lines)',
93102
default=20, dest='h')
94-
103+
parser.add_option('-p', '--pch', help='shape of each bar', default='o', dest='p')
95104
(opts, args) = parser.parse_args()
96105

97106
if opts.f is None:
98107
opts.f = args[0]
99108

100-
plot_hist(opts.f, opts.h, opts.b)
109+
plot_hist(opts.f, opts.h, opts.b, opts.p)
101110

102111

103112

bin/scatter.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import csv
2+
3+
def drange(start, stop, step=1.0):
4+
"generate between 2 numbers w/ optional step"
5+
r = start
6+
while r < stop:
7+
yield r
8+
r += step
9+
10+
def get_scale(series, is_y=False, steps=20):
11+
min_val = min(series)
12+
max_val = max(series)
13+
scaled_series = []
14+
for x in drange(min_val, max_val, (max_val-min_val)/steps):
15+
if x > 0 and scaled_series and max(scaled_series) < 0:
16+
scaled_series.append(0.0)
17+
scaled_series.append(x)
18+
19+
if is_y:
20+
scaled_series.reverse()
21+
return scaled_series
22+
23+
24+
f = "test.txt"
25+
f = "bigtest.txt"
26+
f = "coords.txt"
27+
f = "texas.txt"
28+
29+
if f:
30+
data = [tuple(map(float, line)) for line in csv.reader(open(f))]
31+
else:
32+
xs = xs
33+
ys = ys
34+
xs = [i[0] for i in data]
35+
ys = [i[1] for i in data]
36+
37+
size = 20
38+
39+
plotted = set()
40+
41+
print "-"*(2*len(get_scale(xs, False, size))+2)
42+
for y in get_scale(ys, True, size):
43+
print "|",
44+
for x in get_scale(xs, False, size):
45+
point = " "
46+
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
47+
if xp <= x and yp >= y and (xp, yp) not in plotted:
48+
point = "x"
49+
#point = str(i)
50+
plotted.add((xp, yp))
51+
if x==0 and y==0:
52+
point = "o"
53+
elif x==0:
54+
point = "|"
55+
elif y==0:
56+
point = "-"
57+
print point,
58+
print "|"
59+
print "-"*(2*len(get_scale(xs, False, size))+2)
60+
61+
62+
63+

0 commit comments

Comments
 (0)