Skip to content

Commit 1e05ddd

Browse files
authored
Merge branch 'master' into master
2 parents f1de8e1 + 0a4fffc commit 1e05ddd

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ If you want to use bashplotlib from python, just import histogram and scatterplo
4141
from bashplotlib.scatterplot import plot_scatter
4242
```
4343
<img src="examples/img/scatterplothelp.png">
44+
4445
```
4546
from bashplotlib.histogram import plot_hist
4647
```
48+
4749
<img src="examples/img/histogramhelp.png">
4850

4951
## examples

bashplotlib/histogram.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ def read_numbers(numbers):
4242
for number in numbers:
4343
yield float(str(number).strip())
4444
else:
45-
for number in open(numbers):
46-
yield float(number.strip())
45+
with open(numbers) as fh:
46+
for number in fh:
47+
yield float(number.strip())
4748

4849

4950
def run_demo():
@@ -106,7 +107,8 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
106107
pch = "o"
107108

108109
if isinstance(f, str):
109-
f = open(f).readlines()
110+
with open(f) as fh:
111+
f = fh.readlines()
110112

111113
min_val, max_val = None, None
112114
n, mean, sd = 0.0, 0.0, 0.0
@@ -182,11 +184,12 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
182184
print(" " * (nlen + 1) + "-" * len(xs))
183185

184186
if xlab:
185-
xlen = len(str(float((max_y) / height) + max_y))
187+
labels = abbreviate([str(b) for b in bins])
188+
xlen = len(labels[0])
186189
for i in range(0, xlen):
187190
printcolour(" " * (nlen + 1), True, colour)
188191
for x in range(0, len(hist)):
189-
num = str(bins[x])
192+
num = labels[x]
190193
if x % 2 != 0:
191194
pass
192195
elif i < len(num):

bashplotlib/scatterplot.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,21 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
6565
cs = None
6666
if f:
6767
if isinstance(f, str):
68-
f = open(f)
69-
70-
data = [tuple(line.strip().split(',')) for line in f]
68+
with open(f) as fh:
69+
data = [tuple(line.strip().split(',')) for line in fh]
70+
else:
71+
data = [tuple(line.strip().split(',')) for line in f]
7172
xs = [float(i[0]) for i in data]
7273
ys = [float(i[1]) for i in data]
7374
if len(data[0]) > 2:
7475
cs = [i[2].strip() for i in data]
7576
elif isinstance(xs, list) and isinstance(ys, list):
7677
pass
7778
else:
78-
xs = [float(str(row).strip()) for row in open(xs)]
79-
ys = [float(str(row).strip()) for row in open(ys)]
79+
with open(xs) as fh:
80+
xs = [float(str(row).strip()) for row in fh]
81+
with open(ys) as fh:
82+
ys = [float(str(row).strip()) for row in fh]
8083

8184
_plot_scatter(xs, ys, size, pch, colour, title, cs)
8285

bashplotlib/utils/helpers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ def drange(start, stop, step=1.0, include_stop=False):
6464
r = round(r, 10)
6565

6666

67+
def abbreviate(labels, rfill=' '):
68+
"""
69+
Abbreviate labels without introducing ambiguities.
70+
"""
71+
max_len = max(len(l) for l in labels)
72+
for i in range(1, max_len):
73+
abbrev = [l[:i].ljust(i, rfill) for l in labels]
74+
if len(abbrev) == len(set(abbrev)):
75+
break
76+
return abbrev
77+
78+
6779
def box_text(text, width, offset=0):
6880
"""
6981
Return text inside an ascii textbox

examples/sample.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ hist --file "${dir}/data/exp.txt" --colour blue
2222
echo 'changing the shape of the point'
2323
hist --file "${dir}/data/exp.txt" --pch .
2424

25+
echo 'adding x-labels'
26+
hist --file "${dir}/data/exp.txt" --pch . --xlab
27+
2528
#echo 'using stdin'
2629
#curl -sL https://dl.dropbox.com/u/49171662/example.txt | hist
2730

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from setuptools import find_packages, setup
44

5+
with open("README.rst") as fh:
6+
long_description = fh.read()
7+
58
setup(
69
name="bashplotlib",
710
version="0.6.5",
@@ -11,7 +14,7 @@
1114
license="BSD",
1215
packages=find_packages(),
1316
description="plotting in the terminal",
14-
long_description=open("README.rst").read(),
17+
long_description=long_description,
1518
entry_points = {
1619
'console_scripts': [
1720
'hist=bashplotlib.histogram:main',

0 commit comments

Comments
 (0)