I am trialing the ascii_graph
package for Python. If I assemble the histogram data using numpy.arange
and zip
, the plotting fails. If I assemble the data from primitive literals, it succeeds. Can anyone please explain what the difference is?
import numpy as np
BinMid = np.arange(20) + 1 # Bin edges
BinEdge = np.arange(21) + 0.5 # Bin mid-oints
nDist = np.array( # Bin counts
[ 7083, 73485, 659204, 3511238, 10859771, 22162510,
34511661, 45891902, 55651178, 59153091, 56242073,
48598282, 37947325, 27541907, 19356046, 13630601,
8810979, 4262462, 1227506, 216751], dtype=np.int64 )
# Histogram data
histData = list( zip( BinMid.astype(str) , nDist ) )
# [('1', 7083),
# ('2', 73485),
# ('3', 659204),
# ('4', 3511238),
# ('5', 10859771),
# ('6', 22162510),
# ('7', 34511661),
# ('8', 45891902),
# ('9', 55651178),
# ('10', 59153091),
# ('11', 56242073),
# ('12', 48598282),
# ('13', 37947325),
# ('14', 27541907),
# ('15', 19356046),
# ('16', 13630601),
# ('17', 8810979),
# ('18', 4262462),
# ('19', 1227506),
# ('20', 216751)]
# Create ASCII histograph plotter
from ascii_graph import Pyasciigraph
graph = Pyasciigraph()
# FAILS: Plot using zip expression assigned to histData
#------------------------------------------------------
for line in graph.graph( "Test" ,
list( zip( BinMid.astype(str) , nDist ) ) ):
print(line)
for line in graph.graph( "Test" , histData ): print(line)
# Traceback (most recent call last):
# Cell In[139], line 1
# for line in graph.graph( "Test" , histData ): print(line)
# File ~AppDataLocalanaconda3envspy39libsite-packagesascii_graph__init__.py:399 in graph
# san_data = self._sanitize_data(data)
# File ~AppDataLocalanaconda3envspy39libsite-packagesascii_graph__init__.py:378 in _sanitize_data
# (self._sanitize_string(item[0]),
# File ~AppDataLocalanaconda3envspy39libsite-packagesascii_graph__init__.py:351 in _sanitize_string
# return info
# UnboundLocalError: local variable 'info' referenced before assignment
# SUCCEEDS: Assign pimitive literals to histData
#-----------------------------------------------
histData = [ ('1', 7083),
('2', 73485),
('3', 659204),
('4', 3511238),
('5', 10859771),
('6', 22162510),
('7', 34511661),
('8', 45891902),
('9', 55651178),
('10', 59153091),
('11', 56242073),
('12', 48598282),
('13', 37947325),
('14', 27541907),
('15', 19356046),
('16', 13630601),
('17', 8810979),
('18', 4262462),
('19', 1227506),
('20', 216751) ]
for line in graph.graph( "Test" , histData ): print(line)
# Test
# ###############################################################################
# 7083 1
# 73485 2
# 659204 3
# ███ 3511238 4
# ███████████ 10859771 5
# ████████████████████████ 22162510 6
# █████████████████████████████████████ 34511661 7
# ██████████████████████████████████████████████████ 45891902 8
# █████████████████████████████████████████████████████████████ 55651178 9
# █████████████████████████████████████████████████████████████████ 59153091 10
# █████████████████████████████████████████████████████████████ 56242073 11
# █████████████████████████████████████████████████████ 48598282 12
# █████████████████████████████████████████ 37947325 13
# ██████████████████████████████ 27541907 14
# █████████████████████ 19356046 15
# ██████████████ 13630601 16
# █████████ 8810979 17
# ████ 4262462 18
# █ 1227506 19
# 216751 20