Skip to content

Commit 9dde899

Browse files
author
TrungNT
committed
re-write scatter plot, update docs
1 parent ea5e87f commit 9dde899

File tree

2 files changed

+90
-37
lines changed

2 files changed

+90
-37
lines changed

bashplotlib/histogram.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,42 @@ def run_demo():
8787

8888

8989
def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="default", title="", xlab=None, showSummary=False, regular=False):
90-
"""
91-
Make a histogram
92-
93-
Arguments:
94-
height -- the height of the histogram in # of lines
95-
bincount -- number of bins in the histogram
96-
binwidth -- width of bins in the histogram
97-
pch -- shape of the bars in the plot
98-
colour -- colour of the bars in the terminal
99-
title -- title at the top of the plot
100-
xlab -- boolen value for whether or not to display x-axis labels
101-
showSummary -- boolean value for whether or not to display a summary
102-
regular -- boolean value for whether or not to start y-labels at 0
103-
"""
90+
''' Plot histogram.
91+
1801| oo
92+
1681| oo
93+
1561| oooo
94+
961| oooo
95+
841| oooo
96+
721| ooooo
97+
601| oooooo
98+
241| oooooo
99+
121| oooooooo
100+
1| oooooooooooooo
101+
--------------
102+
Parameters
103+
----------
104+
f : list(number), numpy.ndarray, str(filepath)
105+
input array
106+
height : float
107+
the height of the histogram in # of lines
108+
bincount : int
109+
number of bins in the histogram
110+
binwidth : int
111+
width of bins in the histogram
112+
pch : str
113+
shape of the bars in the plot, e.g 'o'
114+
colour : str
115+
white,aqua,pink,blue,yellow,green,red,grey,black,default,ENDC
116+
title : str
117+
title at the top of the plot, None = no title
118+
xlab : boolean
119+
whether or not to display x-axis labels
120+
showSummary : boolean
121+
whether or not to display a summary
122+
regular : boolean
123+
whether or not to start y-labels at 0
124+
125+
'''
104126
if pch is None:
105127
pch = "o"
106128

bashplotlib/scatterplot.py

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,58 @@ def get_scale(series, is_y=False, steps=20):
2828
return scaled_series
2929

3030

31-
def plot_scatter(f, xs, ys, size, pch, colour, title):
31+
def plot_scatter(xs, ys, size=None, pch='o', colour='red', title=None, print_func=print):
32+
''' Scatter plot.
33+
----------------------
34+
| * |
35+
| * |
36+
| * |
37+
| * |
38+
| * |
39+
| * |
40+
| * |
41+
| * |
42+
-----------------------
43+
Parameters
44+
----------
45+
xs : list, numpy.ndarray
46+
list of x series
47+
ys : list, numpy.ndarray
48+
list of y series
49+
size : int
50+
width of plot
51+
pch : str
52+
any character to represent a points
53+
colour : str, list(str)
54+
white,aqua,pink,blue,yellow,green,red,grey,black,default,ENDC
55+
title : str
56+
title for the plot, None = not show
57+
print_func : callable
58+
function for print out a string
59+
60+
'''
61+
plotted = set()
62+
cs = colour
63+
64+
if title:
65+
print_func(box_text(title, 2 * len(get_scale(xs, False, size)) + 1))
66+
67+
print_func("-" * (2 * len(get_scale(xs, False, size)) + 2))
68+
for y in get_scale(ys, True, size):
69+
print_func("|", end=' ')
70+
for x in get_scale(xs, False, size):
71+
point = " "
72+
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
73+
if xp <= x and yp >= y and (xp, yp) not in plotted:
74+
point = pch
75+
plotted.add((xp, yp))
76+
if isinstance(cs, list):
77+
colour = cs[i]
78+
printcolour(point, True, colour)
79+
print_func(" |")
80+
print_func("-" * (2 * len(get_scale(xs, False, size)) + 2))
81+
82+
def _plot_scatter(f, xs, ys, size, pch, colour, title):
3283
"""
3384
Form a complex number.
3485
@@ -60,27 +111,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
60111
cs = [str(row).strip() for row in open(colour)]
61112
else:
62113
cs = colour
63-
64-
plotted = set()
65-
66-
if title:
67-
print(box_text(title, 2 * len(get_scale(xs, False, size)) + 1))
68-
69-
print("-" * (2 * len(get_scale(xs, False, size)) + 2))
70-
for y in get_scale(ys, True, size):
71-
print("|", end=' ')
72-
for x in get_scale(xs, False, size):
73-
point = " "
74-
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
75-
if xp <= x and yp >= y and (xp, yp) not in plotted:
76-
point = pch
77-
plotted.add((xp, yp))
78-
if isinstance(cs, list):
79-
colour = cs[i]
80-
printcolour(point, True, colour)
81-
print(" |")
82-
print("-" * (2 * len(get_scale(xs, False, size)) + 2))
83-
114+
plot_scatter(xs, ys, size=size, pch=pch, colour=cs, title=title)
84115

85116
def main():
86117

@@ -101,7 +132,7 @@ def main():
101132
opts.f = sys.stdin.readlines()
102133

103134
if opts.f or (opts.x and opts.y):
104-
plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour, opts.t)
135+
_plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour, opts.t)
105136
else:
106137
print("nothing to plot!")
107138

0 commit comments

Comments
 (0)