mirror of
https://github.com/tdulcet/Table-and-Graph-Libs.git
synced 2025-09-20 06:28:06 +08:00
Added histogram functions and type and mark options.
This commit is contained in:
@@ -20,7 +20,7 @@ See the [tables.py](tables.py) file for full usage information.
|
||||
|
||||
Complete versions of all of the examples below and more can be found in the [test.py](test.py) file.
|
||||
|
||||
Run with: `python3 test.py`.
|
||||
Run with: `python3 -OO test.py`.
|
||||
|
||||
#### Output str array as table
|
||||
|
||||
@@ -214,9 +214,10 @@ Values:
|
||||

|
||||
4. `style_types.double`: Double
|
||||

|
||||
5. `style_types.light_dashed`: Light Dashed
|
||||
5. `style_types.arc`: Light Arc
|
||||
6. `style_types.light_dashed`: Light Dashed
|
||||

|
||||
6. `style_types.heavy_dashed`: Heavy Dashed
|
||||
7. `style_types.heavy_dashed`: Heavy Dashed
|
||||

|
||||
|
||||
#### Check size
|
||||
@@ -240,10 +241,30 @@ See the [graphs.py](graphs.py) file for full usage information.
|
||||
|
||||
Complete versions of all of the examples below and more can be found in the [test.py](test.py) file.
|
||||
|
||||
Run with: `python3 test.py`.
|
||||
Run with: `python3 -OO test.py`.
|
||||
|
||||
If `height` is `0`, it will be set to the current height of the terminal (number of rows times four). If `width` is `0`, it will be set to the current width of the terminal (number of columns times two).
|
||||
|
||||
#### Output array as histogram
|
||||
|
||||
```py
|
||||
import graphs
|
||||
|
||||
height = 160
|
||||
width = 160
|
||||
|
||||
xmin = -20
|
||||
xmax = 20
|
||||
ymin = -20
|
||||
ymax = 20
|
||||
|
||||
# Set array, can be any sequence data type
|
||||
|
||||
graphs.histogram(height, width, xmin, xmax, ymin, ymax, array)
|
||||
```
|
||||
|
||||
If `xmin` and `xmax` are both `0`, they will be set to the respective minimum and maximum values of x in the array. If `ymin` and `ymax` are both `0`, they will be set to the respective minimum and maximum values of y in the resulting histogram.
|
||||
|
||||
#### Output single array as plot
|
||||
|
||||
```py
|
||||
@@ -259,14 +280,14 @@ ymax = 20
|
||||
|
||||
# Set array, can be any sequence data type, but must have exactly two columns
|
||||
|
||||
graphs.array(height, width, xmin, xmax, ymin, ymax, array)
|
||||
graphs.plot(height, width, xmin, xmax, ymin, ymax, array)
|
||||
```
|
||||
|
||||
If `xmin` and `xmax` are both `0`, they will be set to the respective minimum and maximum values of x in the array. If `ymin` and `ymax` are both `0`, they will be set to the respective minimum and maximum values of y in the array.
|
||||
|
||||

|
||||
|
||||
Use `graphs.arrays()` to plot multiple arrays, which can be of different sizes.
|
||||
Use `graphs.plots()` to plot multiple arrays, which can be of different sizes.
|
||||
|
||||
#### Output single function as graph
|
||||
|
||||
@@ -413,6 +434,27 @@ Formats 2-5 are similar to the respective `--to` options with the [numfmt](https
|
||||
Option: `yunits`\
|
||||
Values: Same as above.
|
||||
|
||||
#### Type
|
||||
|
||||
Option: `type`\
|
||||
Values:
|
||||
|
||||
1. `type_types.braille`: Braille (default)
|
||||
2. `type_types.block`: Block
|
||||
|
||||
The Braille type has the highest resolution of 2×4 pixels per character, while the block type uses 2×2. This option is only used for plots and graphs. Histograms use 1×8 pixels per character.
|
||||
|
||||
#### Mark type
|
||||
|
||||
Option: `mark`\
|
||||
Values:
|
||||
|
||||
1. `mark_types.dot`: Dot (default)
|
||||
2. `mark_types.plus`: Plus
|
||||
3. `mark_types.square`: Square
|
||||
|
||||
The dot mark type uses a single pixel per mark, the plus uses five pixels and the square uses eight pixels. This option is only used for plots and graphs.
|
||||
|
||||
#### Title
|
||||
|
||||
Option: `title`\
|
||||
@@ -435,9 +477,10 @@ Values:
|
||||

|
||||
4. `style_types.double`: Double
|
||||

|
||||
5. `style_types.light_dashed`: Light Dashed
|
||||
5. `style_types.arc`: Light Arc
|
||||
6. `style_types.light_dashed`: Light Dashed
|
||||

|
||||
6. `style_types.heavy_dashed`: Heavy Dashed
|
||||
7. `style_types.heavy_dashed`: Heavy Dashed
|
||||

|
||||
|
||||
#### Graph/Plot Color
|
||||
|
269
python/graphs.py
269
python/graphs.py
@@ -23,6 +23,7 @@ class style_types(IntEnum):
|
||||
light = auto()
|
||||
heavy = auto()
|
||||
double = auto()
|
||||
arc = auto()
|
||||
light_dashed = auto()
|
||||
heavy_dashed = auto()
|
||||
|
||||
@@ -33,6 +34,7 @@ styles = [
|
||||
["─", "│", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light
|
||||
["━", "┃", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"], # Heavy
|
||||
["═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"], # Double
|
||||
["─", "│", "╭", "┬", "╮", "├", "┼", "┤", "╰", "┴", "╯"], # Light Arc
|
||||
["╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light Dashed
|
||||
["╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"] # Heavy Dashed
|
||||
# [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] #No border
|
||||
@@ -83,8 +85,31 @@ dots = [
|
||||
"⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯",
|
||||
"⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾",
|
||||
"⣿"]
|
||||
dotvalues = [[0x1, 0x2, 0x4, 0x40], [0x8, 0x10, 0x20, 0x80]]
|
||||
|
||||
blocks = [" ", "▖", "▗", "▄", "▘", "▌", "▚",
|
||||
"▙", "▝", "▞", "▐", "▟", "▀", "▛", "▜", "█"]
|
||||
blockvalues = [[4, 1], [8, 2]]
|
||||
|
||||
bars = [" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"]
|
||||
|
||||
|
||||
class type_types(IntEnum):
|
||||
braille = 0
|
||||
block = auto()
|
||||
histogram = auto() # Set automatically by the histogram() function
|
||||
|
||||
atype_types = (type_types.braille, type_types.block)
|
||||
|
||||
marks = [[[0, 0]], [[0, 1], [-1, 0], [0, 0], [1, 0], [0, -1]],
|
||||
[[-1, 1], [0, 1], [1, 1], [-1, 0], [1, 0], [-1, -1], [0, -1], [1, -1]]]
|
||||
|
||||
|
||||
class mark_types(IntEnum):
|
||||
dot = 0
|
||||
plus = auto()
|
||||
square = auto()
|
||||
|
||||
values = [[0x1, 0x2, 0x4, 0x40], [0x8, 0x10, 0x20, 0x80]]
|
||||
|
||||
fractions = {
|
||||
"¼": Fraction(1, 4),
|
||||
@@ -174,9 +199,7 @@ def outputunit(number: float, scale: units_types) -> str:
|
||||
anumber = abs(number)
|
||||
anumber += 0.0005 if anumber < 10 else 0.005 if anumber < 100 else 0.05 if anumber < 1000 else 0.5
|
||||
|
||||
strm = ""
|
||||
|
||||
if number != 0 and anumber < 1000 and power > 0:
|
||||
if number and anumber < 1000 and power > 0:
|
||||
strm = f"{number:.{sys.float_info.dig}n}"
|
||||
|
||||
length = 5 + (number < 0)
|
||||
@@ -263,8 +286,7 @@ def outputlabel(label: float, units: units_types) -> Tuple[int, str]:
|
||||
return length, strm
|
||||
|
||||
|
||||
def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, array: List[List[int]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True,
|
||||
axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, style: style_types = style_types.light, title: Optional[str] = None, check: bool = True) -> int:
|
||||
def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, array: List[List[int]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, style: style_types = style_types.light, title: Optional[str] = None, check: bool = True) -> int:
|
||||
"""Output graph."""
|
||||
if not array:
|
||||
return 1
|
||||
@@ -277,8 +299,11 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
|
||||
w = shutil.get_terminal_size()
|
||||
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
ai = 8 if atype == type_types.histogram else 2 if atype == type_types.block else 4
|
||||
aj = 1 if atype == type_types.histogram else 2
|
||||
|
||||
aheight = height // ai
|
||||
awidth = width // aj
|
||||
|
||||
if check:
|
||||
if aheight > w.lines:
|
||||
@@ -303,8 +328,8 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
ystep = (ymax - ymin) / height
|
||||
xaxis = 0 if xmin > 0 else width if xmax < 0 else width - xmax / xstep
|
||||
yaxis = height if ymin > 0 else 0 if ymax < 0 else ymax / ystep
|
||||
xdivisor = 2 * 4 * (width // 160 + 2)
|
||||
ydivisor = 2 * 4 * (height // 160 + 2)
|
||||
xdivisor = 4 * aj * ((((2 * width) // aj) // 160) + 2)
|
||||
ydivisor = 2 * ai * ((((4 * height) // ai) // 160) + 2)
|
||||
|
||||
if title:
|
||||
print(textwrap.fill(title, width=awidth))
|
||||
@@ -320,8 +345,8 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
|
||||
i = 0
|
||||
while i < height:
|
||||
ayaxis = i <= yaxis < i + 4 if yaxis <= height - 4 else i < yaxis <= i + 4
|
||||
yaxislabel = i <= yaxis + 4 < i + 4 if yaxis <= height - 4 else i < yaxis - 4 <= i + 4
|
||||
ayaxis = i <= yaxis < i + ai if yaxis <= height - ai else i < yaxis <= i + ai
|
||||
yaxislabel = i <= yaxis + ai < i + ai if yaxis <= height - ai else i < yaxis - ai <= i + ai
|
||||
|
||||
ylabelstrm = ""
|
||||
ylabellength = 0
|
||||
@@ -332,24 +357,24 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
adivisor = -ydivisor if i < yaxis else ydivisor
|
||||
|
||||
k = yaxis + adivisor
|
||||
while (k >= i if i < yaxis else k < i + 4) and i >= 4 and not output:
|
||||
if i <= k < i + 4:
|
||||
label = ymax - (height if k > height else k) * ystep
|
||||
while (k >= i if i < yaxis else k < i + ai) and i >= ai and not output:
|
||||
if i <= k < i + ai:
|
||||
label = ymax - min(k, height) * ystep
|
||||
|
||||
output = True
|
||||
k += adivisor
|
||||
|
||||
if output:
|
||||
ylabellength, ylabelstrm = outputlabel(label, yunits)
|
||||
ylabellength *= 2
|
||||
ylabellength *= aj
|
||||
|
||||
if border:
|
||||
strm += styles[style][1]
|
||||
|
||||
j = 0
|
||||
while j < width:
|
||||
axaxis = j < xaxis <= j + 2 if xaxis >= 2 else j <= xaxis < j + 2
|
||||
xaxislabel = j < xaxis - 2 <= j + 2 if xaxis >= 2 else j <= xaxis + 2 < j + 2
|
||||
axaxis = j < xaxis <= j + aj if xaxis >= aj else j <= xaxis < j + aj
|
||||
xaxislabel = j < xaxis - aj <= j + aj if xaxis >= aj else j <= xaxis + aj < j + aj
|
||||
|
||||
output = False
|
||||
|
||||
@@ -361,16 +386,16 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if i == 0:
|
||||
strm += styles[style][4]
|
||||
output = True
|
||||
elif i >= height - 4:
|
||||
elif i >= height - ai:
|
||||
strm += styles[style][10]
|
||||
output = True
|
||||
elif axistick:
|
||||
adivisor = -ydivisor if i < yaxis else ydivisor
|
||||
|
||||
k = yaxis + adivisor
|
||||
while (k >= i if i < yaxis else k < i + 4) and i >= 4 and not output:
|
||||
if i <= k < i + 4:
|
||||
strm += styles[style][7 if xaxis >= 2 else 5]
|
||||
while (k >= i if i < yaxis else k < i + ai) and i >= ai and not output:
|
||||
if i <= k < i + ai:
|
||||
strm += styles[style][7 if xaxis >= aj else 5]
|
||||
output = True
|
||||
k += adivisor
|
||||
if not output:
|
||||
@@ -380,17 +405,17 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if j == 0:
|
||||
strm += styles[style][2]
|
||||
output = True
|
||||
elif j >= width - 2:
|
||||
elif j >= width - aj:
|
||||
strm += styles[style][4]
|
||||
output = True
|
||||
elif axistick:
|
||||
adivisor = -xdivisor if j < xaxis else xdivisor
|
||||
|
||||
k = xaxis + adivisor
|
||||
while (k >= j if j < xaxis else k < j + 2) and j < width - 4 and not output:
|
||||
if j <= k < j + 2:
|
||||
while (k >= j if j < xaxis else k < j + aj) and j < width - ai and not output:
|
||||
if j <= k < j + aj:
|
||||
strm += styles[style][3 if yaxis <=
|
||||
height - 4 else 9]
|
||||
height - ai else 9]
|
||||
output = True
|
||||
k += adivisor
|
||||
if not output:
|
||||
@@ -399,62 +424,70 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
elif yaxislabel and xaxislabel and axistick and axisunitslabel and ymin <= 0 <= ymax and xmin <= 0 <= xmax:
|
||||
strm += "0"
|
||||
output = True
|
||||
elif (j >= width - 2 if xaxis <= width - 2 else j == 0) and yaxislabel and axislabel:
|
||||
elif (j >= width - aj if xaxis <= width - aj else j == 0) and yaxislabel and axislabel:
|
||||
strm += "x"
|
||||
output = True
|
||||
elif yaxislabel and axistick and axisunitslabel:
|
||||
label = 0.0
|
||||
adivisor = -xdivisor if j < xaxis else xdivisor
|
||||
if j < xaxis:
|
||||
j += 2
|
||||
j += aj
|
||||
|
||||
k = xaxis + adivisor
|
||||
while (k >= j if j < xaxis else k < j + 2) and j < width - 2 and not output:
|
||||
if j <= k < j + 2:
|
||||
label = (width if k > width else k) * xstep + xmin
|
||||
while (k >= j if j < xaxis else k < j + aj) and j < width - aj and not output:
|
||||
if j <= k < j + aj:
|
||||
label = min(k, width) * xstep + xmin
|
||||
|
||||
output = True
|
||||
k += adivisor
|
||||
|
||||
if adivisor < 0:
|
||||
j -= 2
|
||||
j -= aj
|
||||
|
||||
if output:
|
||||
output = False
|
||||
|
||||
length, astrm = outputlabel(label, xunits)
|
||||
length *= 2
|
||||
if (j >= xaxis or j + length < xaxis - 4) and j + length < width - 2:
|
||||
length *= aj
|
||||
if (j >= xaxis or j + length < xaxis - ai) and j + length < width - aj:
|
||||
strm += astrm
|
||||
|
||||
if length > 2:
|
||||
j += length - 2
|
||||
if length > aj:
|
||||
j += length - aj
|
||||
|
||||
if adivisor < 0:
|
||||
output = True
|
||||
else:
|
||||
j += 2
|
||||
elif (i == 0 if yaxis >= 4 else i >= height - 4) and xaxislabel and axislabel:
|
||||
j += aj
|
||||
elif (i == 0 if yaxis >= ai else i >= height - ai) and xaxislabel and axislabel:
|
||||
strm += "y"
|
||||
output = True
|
||||
elif ylabellength and (xaxislabel if xaxis < 2 else j < xaxis - ylabellength and j + 2 >= xaxis - ylabellength) and (yaxis >= 4 or i < height - 4) and axistick and axisunitslabel:
|
||||
elif ylabellength and (xaxislabel if xaxis < aj else j < xaxis - ylabellength and j + aj >= xaxis - ylabellength) and (yaxis >= ai or i < height - ai) and axistick and axisunitslabel:
|
||||
strm += ylabelstrm
|
||||
output = True
|
||||
if ylabellength > 2:
|
||||
j += ylabellength - 2
|
||||
if ylabellength > aj:
|
||||
j += ylabellength - aj
|
||||
|
||||
if not output:
|
||||
dot = 0
|
||||
color = 0
|
||||
|
||||
for k in range(min(2, width - j)):
|
||||
for l in range(min(4, height - i)):
|
||||
dot += (1 if array[j + k][i + l] else 0) * values[k][l]
|
||||
for k in range(min(aj, width - j)):
|
||||
for l in range(min(ai, height - i)):
|
||||
value = array[j + k][i + l]
|
||||
if value:
|
||||
if atype == type_types.histogram:
|
||||
if not dot:
|
||||
dot = (len(bars) - l) - 1
|
||||
elif atype == type_types.block:
|
||||
dot += blockvalues[k][l]
|
||||
else:
|
||||
dot += dotvalues[k][l]
|
||||
if color:
|
||||
if array[j + k][i + l] and color != array[j + k][i + l]:
|
||||
if value and color != value:
|
||||
color = 1
|
||||
else:
|
||||
color = array[j + k][i + l]
|
||||
color = value
|
||||
|
||||
if color:
|
||||
color -= 1
|
||||
@@ -462,19 +495,19 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if color:
|
||||
strm += colors[color]
|
||||
|
||||
strm += dots[dot]
|
||||
strm += bars[dot] if atype == type_types.histogram else blocks[dot] if atype == type_types.block else dots[dot]
|
||||
|
||||
if color:
|
||||
strm += colors[0]
|
||||
|
||||
j += 2
|
||||
j += aj
|
||||
|
||||
if border:
|
||||
strm += styles[style][1]
|
||||
|
||||
if i < height - 4 or border:
|
||||
if i < height - ai or border:
|
||||
strm += "\n"
|
||||
i += 4
|
||||
i += ai
|
||||
|
||||
if border:
|
||||
strm += styles[style][8]
|
||||
@@ -488,8 +521,78 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
return 0
|
||||
|
||||
|
||||
def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarrays: Sequence[Sequence[Sequence[float]]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True,
|
||||
xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int:
|
||||
def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[Sequence[Sequence[float]]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int:
|
||||
"""Convert one or more arrays to graph and output."""
|
||||
if not aarray:
|
||||
return 1
|
||||
|
||||
w = shutil.get_terminal_size()
|
||||
|
||||
if height == 0:
|
||||
height = w.lines * 4
|
||||
|
||||
if width == 0:
|
||||
width = w.columns * 2
|
||||
|
||||
if check:
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
|
||||
if aheight > w.lines:
|
||||
print(
|
||||
f"The height of the graph ({aheight}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if awidth > w.columns:
|
||||
print(
|
||||
f"The width of the graph ({awidth}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
height *= 2
|
||||
width //= 2
|
||||
|
||||
if xmin == xmax == 0:
|
||||
xmin = min(aarray)
|
||||
xmax = max(aarray)
|
||||
|
||||
if xmin >= xmax:
|
||||
print("xmin must be less than xmax.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
histogram = [0 for i in range(width)]
|
||||
|
||||
xstep = (xmax - xmin) / width
|
||||
|
||||
for x in aarray:
|
||||
if xmin <= x < xmax:
|
||||
index = int((x - xmin) / xstep)
|
||||
histogram[index] += 1
|
||||
|
||||
if ymin == ymax == 0:
|
||||
ymin = min(histogram)
|
||||
ymax = max(histogram)
|
||||
|
||||
if ymin >= ymax:
|
||||
print("ymin must be less than ymax.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
ystep = (ymax - ymin) / height
|
||||
yaxis = ymax / ystep
|
||||
|
||||
aaarray = [[0 for j in range(height)] for i in range(width)]
|
||||
|
||||
acolor = color + 1
|
||||
|
||||
for x, ay in enumerate(histogram):
|
||||
y = 0 if ay >= ymax else int(yaxis - (ay / ystep))
|
||||
while y < yaxis and y < height:
|
||||
aaarray[x][y] = acolor
|
||||
y += 1
|
||||
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, type_types.histogram, style, title)
|
||||
|
||||
|
||||
def plots(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarrays: Sequence[Sequence[Sequence[float]]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, mark: mark_types = mark_types.dot, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int:
|
||||
"""Convert one or more arrays to graph and output."""
|
||||
if not aarrays:
|
||||
return 1
|
||||
@@ -520,13 +623,16 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
f"The width of the graph ({awidth}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if xmin == 0 and xmax == 0:
|
||||
xmin = min(i[0] for aarray in aarrays for i in aarray)
|
||||
xmax = max(i[0] for aarray in aarrays for i in aarray)
|
||||
if atype == type_types.block:
|
||||
height //= 2
|
||||
|
||||
if ymin == 0 and ymax == 0:
|
||||
ymin = min(i[1] for aarray in aarrays for i in aarray)
|
||||
ymax = max(i[1] for aarray in aarrays for i in aarray)
|
||||
if xmin == xmax == 0:
|
||||
xmin = min(x for aarray in aarrays for x, y in aarray)
|
||||
xmax = max(x for aarray in aarrays for x, y in aarray)
|
||||
|
||||
if ymin == ymax == 0:
|
||||
ymin = min(y for aarray in aarrays for x, y in aarray)
|
||||
ymax = max(y for aarray in aarrays for x, y in aarray)
|
||||
|
||||
if xmin >= xmax:
|
||||
print("xmin must be less than xmax.", file=sys.stderr)
|
||||
@@ -546,30 +652,31 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
for j, aarray in enumerate(aarrays):
|
||||
acolor = color + 1 if len(aarrays) == 1 else j % (len(colors) - 2) + 3
|
||||
|
||||
for i in aarray:
|
||||
if i[0] >= xmin and i[0] < xmax and i[1] >= ymin and i[1] < ymax:
|
||||
x = int(i[0] / xstep + xaxis)
|
||||
y = int(yaxis - i[1] / ystep - 1)
|
||||
for x, y in aarray:
|
||||
if xmin <= x < xmax and ymin <= y < ymax:
|
||||
ax = int(x / xstep + xaxis)
|
||||
ay = int(yaxis - y / ystep - 1)
|
||||
|
||||
if aaarray[x][y]:
|
||||
if aaarray[x][y] != acolor:
|
||||
aaarray[x][y] = 1
|
||||
else:
|
||||
aaarray[x][y] = acolor
|
||||
for ix, iy in marks[mark]:
|
||||
x = ax + ix
|
||||
y = ay + iy
|
||||
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border=border, axis=axis, axislabel=axislabel,
|
||||
axistick=axistick, axisunitslabel=axisunitslabel, xunits=xunits, yunits=yunits, style=style, title=title)
|
||||
if x < width and y < height:
|
||||
if aaarray[x][y]:
|
||||
if aaarray[x][y] != acolor:
|
||||
aaarray[x][y] = 1
|
||||
else:
|
||||
aaarray[x][y] = acolor
|
||||
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, title)
|
||||
|
||||
|
||||
def array(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[Sequence[float]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True,
|
||||
axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None) -> int:
|
||||
def plot(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[Sequence[float]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, mark: mark_types = mark_types.dot, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None) -> int:
|
||||
"""Convert single array to graph and output."""
|
||||
return arrays(height, width, xmin, xmax, ymin, ymax, [aarray], border=border, axis=axis, axislabel=axislabel,
|
||||
axistick=axistick, axisunitslabel=axisunitslabel, xunits=xunits, yunits=yunits, style=style, color=color, title=title)
|
||||
return plots(height, width, xmin, xmax, ymin, ymax, [aarray], border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, mark, style, color, title)
|
||||
|
||||
|
||||
def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunctions: Sequence[Callable[[float], float]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True,
|
||||
axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int:
|
||||
def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunctions: Sequence[Callable[[float], float]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int:
|
||||
"""Convert one or more functions to graph and output."""
|
||||
if not afunctions:
|
||||
return 1
|
||||
@@ -596,6 +703,9 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
|
||||
f"The width of the graph ({awidth}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if atype == type_types.block:
|
||||
height //= 2
|
||||
|
||||
if xmin >= xmax:
|
||||
print("xmin must be less than xmax.", file=sys.stderr)
|
||||
return 1
|
||||
@@ -632,12 +742,9 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
|
||||
else:
|
||||
array[ax][ay] = acolor
|
||||
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, array, border=border, axis=axis, axislabel=axislabel,
|
||||
axistick=axistick, axisunitslabel=axisunitslabel, xunits=xunits, yunits=yunits, style=style, title=title)
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, array, border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, title)
|
||||
|
||||
|
||||
def function(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunction: Callable[[float], float], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True,
|
||||
axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None) -> int:
|
||||
def function(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunction: Callable[[float], float], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None) -> int:
|
||||
"""Convert single function to function array and output."""
|
||||
return functions(height, width, xmin, xmax, ymin, ymax, [afunction], border=border, axis=axis, axislabel=axislabel,
|
||||
axistick=axistick, axisunitslabel=axisunitslabel, xunits=xunits, yunits=yunits, style=style, color=color, title=title)
|
||||
return functions(height, width, xmin, xmax, ymin, ymax, [afunction], border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, color, title)
|
||||
|
@@ -21,6 +21,7 @@ class style_types(IntEnum):
|
||||
light = auto()
|
||||
heavy = auto()
|
||||
double = auto()
|
||||
arc = auto()
|
||||
light_dashed = auto()
|
||||
heavy_dashed = auto()
|
||||
|
||||
@@ -31,6 +32,7 @@ styles = [
|
||||
["─", "│", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light
|
||||
["━", "┃", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"], # Heavy
|
||||
["═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"], # Double
|
||||
["─", "│", "╭", "┬", "╮", "├", "┼", "┤", "╰", "┴", "╯"], # Light Arc
|
||||
["╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light Dashed
|
||||
["╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"] # Heavy Dashed
|
||||
# [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] #No border
|
||||
@@ -181,8 +183,7 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
|
||||
return 0
|
||||
|
||||
|
||||
def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] = None, aheadercolumn: Optional[Sequence[Any]] = None, headerrow: bool = False, headercolumn: bool = False,
|
||||
tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
||||
def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] = None, aheadercolumn: Optional[Sequence[Any]] = None, headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
||||
"""Convert array to char array and output as table."""
|
||||
if not aarray:
|
||||
return 1
|
||||
@@ -228,12 +229,10 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] =
|
||||
j = 1 if aheadercolumn else 0
|
||||
aaarray[i][j:] = map(str, aarray[ii][:columns - j])
|
||||
|
||||
return table(aaarray, headerrow=headerrow, headercolumn=headercolumn, tableborder=tableborder,
|
||||
cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style)
|
||||
return table(aaarray, headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style)
|
||||
|
||||
|
||||
def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Callable[[float], float]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True,
|
||||
cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
||||
def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Callable[[float], float]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
||||
"""Convert one or more functions to array and output as table."""
|
||||
if not afunctions:
|
||||
return 1
|
||||
@@ -263,16 +262,13 @@ def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Calla
|
||||
aarray = [[0 for j in range(columns)] for i in range(rows)]
|
||||
|
||||
for i in range(rows):
|
||||
aarray[i][0] = i * xstep + xmin
|
||||
temp = aarray[i][0] = i * xstep + xmin
|
||||
|
||||
aarray[i][1:] = [function(aarray[i][0]) for function in afunctions]
|
||||
aarray[i][1:] = [function(temp) for function in afunctions]
|
||||
|
||||
return array(aarray, aheaderrow, None, headerrow=headerrow, headercolumn=headercolumn, tableborder=tableborder,
|
||||
cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style)
|
||||
return array(aarray, aheaderrow, None, headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style)
|
||||
|
||||
|
||||
def function(xmin: float, xmax: float, xstep: float, afunction: Callable[[float], float], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True,
|
||||
cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
||||
def function(xmin: float, xmax: float, xstep: float, afunction: Callable[[float], float], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
||||
"""Convert single function to array and output as table."""
|
||||
return functions(xmin, xmax, xstep, [afunction], headerrow=headerrow, headercolumn=headercolumn,
|
||||
tableborder=tableborder, cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style)
|
||||
return functions(xmin, xmax, xstep, [afunction], headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style)
|
||||
|
@@ -2,6 +2,8 @@
|
||||
|
||||
# Teal Dulcet, CS546
|
||||
|
||||
# Run: python3 -OO test.py
|
||||
|
||||
import math
|
||||
import random
|
||||
import sys
|
||||
@@ -112,10 +114,18 @@ xmax = 20
|
||||
ymin = -20
|
||||
ymax = 20
|
||||
|
||||
print("\nOutput array as histogram\n")
|
||||
array = [random.gauss(0, 1) for i in range(100)]
|
||||
for style in graphs.style_types:
|
||||
graphs.histogram(height, width, xmin, xmax, ymin, ymax, array, style=style)
|
||||
|
||||
print("\nOutput single array as plot\n")
|
||||
array = [range(i, i + 2) for i in range(10)]
|
||||
for style in graphs.style_types:
|
||||
graphs.array(height, width, xmin, xmax, ymin, ymax, array, style=style)
|
||||
for atype in graphs.atype_types:
|
||||
for mark in graphs.mark_types:
|
||||
for style in graphs.style_types:
|
||||
graphs.plot(height, width, xmin, xmax, ymin, ymax,
|
||||
array, atype=atype, mark=mark, style=style)
|
||||
|
||||
print("\nOutput single function as graph\n")
|
||||
for style in graphs.style_types:
|
||||
|
Reference in New Issue
Block a user