Skip to content

Commit ea272f0

Browse files
committed
Fixed the data location and made hist -d gracefully quit if not demo file exists
1 parent 877f667 commit ea272f0

File tree

3 files changed

+53
-33
lines changed

3 files changed

+53
-33
lines changed

bashplotlib/histogram.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import math
44
import optparse
5+
import os
6+
from os.path import dirname
57
import sys
68
from utils.helpers import *
79
from utils.commandhelp import hist
@@ -30,32 +32,47 @@ def read_numbers(numbers):
3032
for n in open(numbers):
3133
yield float(n.strip())
3234

35+
3336
def run_demo():
34-
"demo the product"
37+
"""
38+
Run a demonstration
39+
"""
40+
module_dir = dirname(dirname(os.path.realpath(__file__)))
41+
demo_file = os.path.join(module_dir, 'examples/data/exp.txt')
42+
43+
if not os.path.isfile(demo_file):
44+
sys.stderr.write("demo input file not found!\n")
45+
sys.stderr.write("run the downloaddata.sh script in the example first\n")
46+
sys.exit(1)
47+
3548
#plotting a histogram
3649
print "plotting a basic histogram"
37-
print "plot_hist('./data/exp.txt')"
38-
print "hist -f ./data/exp.txt"
39-
print "cat ./data/exp.txt | hist"
40-
plot_hist('./data/exp.txt')
41-
print "*"*80
50+
print "plot_hist('%s')" % demo_file
51+
print "hist -f %s" % demo_file
52+
print "cat %s | hist" % demo_file
53+
plot_hist(demo_file)
54+
print "*" * 80
55+
4256
#with colors
4357
print "histogram with colors"
44-
print "plot_hist('./data/exp.txt', colour='blue')"
45-
print "hist -f ./data/exp.txt -c blue"
46-
plot_hist('./data/exp.txt', colour='blue')
47-
print "*"*80
58+
print "plot_hist('%s', colour='blue')" % demo_file
59+
print "hist -f %s -c blue" % demo_file
60+
plot_hist(demo_file, colour='blue')
61+
print "*" * 80
62+
4863
#changing the shape of the point
4964
print "changing the shape of the bars"
50-
print "plot_hist('./data/exp.txt', pch='.')"
51-
print "hist -f ./data/exp.txt -p ."
52-
plot_hist('./data/exp.txt', pch='.')
53-
print "*"*80
65+
print "plot_hist('%s', pch='.')" % demo_file
66+
print "hist -f %s -p ." % demo_file
67+
plot_hist(demo_file, pch='.')
68+
print "*" * 80
69+
5470
#changing the size of the plot
5571
print "changing the size of the plot"
56-
print "plot_hist('./data/exp.txt', height=35.0, bincount=40)"
57-
print "hist -f ./data/exp.txt -s 35.0 -b 40"
58-
plot_hist('./data/exp.txt', height=35.0, bincount=40)
72+
print "plot_hist('%s', height=35.0, bincount=40)" % demo_file
73+
print "hist -f %s -s 35.0 -b 40" % demo_file
74+
plot_hist(demo_file, height=35.0, bincount=40)
75+
5976

6077
def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="white", title="", xlab=None, showSummary=False, regular=False):
6178
"""make a histogram

examples/downloaddata.sh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/usr/bin/env bash
22

3-
if [ ! -d data ]; then
4-
mkdir data
3+
dir=$(dirname "$0")
4+
5+
if [ ! -d "${dir}data" ]; then
6+
mkdir "${dir}/data"
57
fi
6-
cd data
78

8-
curl -L 'https://dl.dropbox.com/sh/jjeubxlxuqkzkeq/5W6zkUZXww/million.txt?dl=1' > million.txt
9-
curl -L 'https://dl.dropbox.com/s/yuxlaj8okcjta9t/exp.txt?dl=1' > exp.txt
10-
curl -L 'https://dl.dropbox.com/s/cbf5skx34grlwy6/lower48.txt?dl=1' > lower48.txt
11-
curl -L 'https://dl.dropbox.com/s/gsu2y9vqnx5ps5i/texas.txt?dl=1' > texas.txt
12-
curl -L 'https://dl.dropbox.com/s/4zws1nbamorcy9z/x_test.txt?dl=1' > x_test.txt
13-
curl -L 'https://dl.dropbox.com/s/mlt4gfqr6n24kxj/y_test.txt?dl=1' > y_test.txt
9+
curl -L 'https://dl.dropbox.com/sh/jjeubxlxuqkzkeq/5W6zkUZXww/million.txt?dl=1' > "${dir}/data/million.txt"
10+
curl -L 'https://dl.dropbox.com/s/yuxlaj8okcjta9t/exp.txt?dl=1' > "${dir}/data/exp.txt"
11+
curl -L 'https://dl.dropbox.com/s/cbf5skx34grlwy6/lower48.txt?dl=1' > "${dir}/data/lower48.txt"
12+
curl -L 'https://dl.dropbox.com/s/gsu2y9vqnx5ps5i/texas.txt?dl=1' > "${dir}/data/texas.txt"
13+
curl -L 'https://dl.dropbox.com/s/4zws1nbamorcy9z/x_test.txt?dl=1' > "${dir}/data/x_test.txt"
14+
curl -L 'https://dl.dropbox.com/s/mlt4gfqr6n24kxj/y_test.txt?dl=1' > "${dir}/data/y_test.txt"

examples/sample.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
#!/usr/bin/env bash
22

3-
if [ ! -d data ]; then
3+
dir=$(dirname "$0")
4+
5+
if [ ! -d "${dir}/data" ]; then
46
echo 'downloading data'
5-
./downloaddata.sh
7+
"${dir}/downloaddata.sh"
68
fi
79

810
echo 'plotting coordinates'
9-
scatter --file ./data/texas.txt
11+
scatter --file "${dir}/data/texas.txt"
1012

1113
echo 'with x and y coords'
12-
scatter -x ./data/x_test.txt -y ./data/y_test.txt
14+
scatter -x "${dir}/data/x_test.txt" -y "${dir}/data/y_test.txt"
1315

1416
echo 'plotting a histogram'
15-
hist --file ./data/exp.txt
17+
hist --file "${dir}/data/exp.txt"
1618

1719
echo 'with colors'
18-
hist --file ./data/exp.txt --colour blue
20+
hist --file "${dir}/data/exp.txt" --colour blue
1921

2022
echo 'changing the shape of the point'
21-
hist --file ./data/exp.txt --pch .
23+
hist --file "${dir}/data/exp.txt" --pch .
2224

2325
#echo 'using stdin'
2426
#curl -sL https://dl.dropbox.com/u/49171662/example.txt | hist

0 commit comments

Comments
 (0)