"""this example demonstrates how to image 3D numpy array indices are colored in yellow if their sum is divisible by 3""" import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray A = np.array([[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]) B = np.array([[[2, 3, 0], [3, 1, 2]]]) fig = plt.figure() ax = fig.add_subplot(1, 3, 1, projection='3d') va = VisualArray(A, fig=fig, ax=ax) coords = va.get_indices() va.set_colors(coords.T, color='yellow', basecolor='lightblue') va.vizualize(fixview=True) va.ax.set_title('A = array of shape (4,2,3)') va.ax.dist = 12.5 ax = fig.add_subplot(1, 3, 2, projection='3d') va = VisualArray(B, fig=fig, ax=ax) coords = va.get_indices() va.set_colors(coords.T, color='yellow', basecolor='lightblue') va.vizualize(fixview=True) va.ax.set_title('B = array of shape (1,2,3)') va.ax.dist = 12.5 np.put_along_axis(A, B, 1, 0) ax = fig.add_subplot(1, 3, 3, projection='3d')
import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray v = np.array([[2, 5, 3, 5, 1, 8], [4, 6, 2, 7, 5, 9], [1, 8, 2, 3, 1, 4], [2, 8, 1, 4, 3, 5], [5, 7, 2, 3, 7, 8], [1, 2, 4, 6, 3, 5], [3, 5, 2, 8, 1, 4]]) s = np.sort(v, axis=1) arr1 = v[(s[:, :-1] != s[:, 1:]).all(1)] arr2 = arr1.reshape(-1, 3, 2) fig = plt.figure() ax = fig.add_subplot(1, 2, 1, projection='3d') ax.set_title('Initial array') va = VisualArray(arr1, fig=fig, ax=ax) va.set_colors(va.get_indices().T, color='yellow', basecolor='lightblue') va.vizualize(fixview=True, axis_labels=(None, 'axis=0', 'axis=1')) ax.dist = 12 ax = fig.add_subplot(1, 2, 2, projection='3d') ax.set_title('Reshaped array') va = VisualArray(arr2, fig=fig, ax=ax) va.set_colors(va.get_indices().T, color='yellow', basecolor='lightblue') va.vizualize(fixview=True) ax.dist = 12 plt.show()
import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray a = np.array([[1, 2, 3], [3, 4, 5]]) b = np.array([[[1, 0, 1], [1, 1, 0], [0, 1, 1]], [[1, 1, 1], [0, 1, 0], [0, 0, 1]]]) c = np.array([1, 2, 3]) fig = plt.figure() ax = fig.add_subplot(2, 3, 1, projection='3d') va1 = VisualArray(a[:, None, :], fig=fig, ax=ax) va1.ax.set_title(f'a[:,None,:] of shape=(2, 1, 3)') va1.set_colors(va1.get_indices().T, color='lightgreen', basecolor='aqua') va1.vizualize(fixview=True, usetex=False, scale=1.2) va1.ax.dist = 12 ax = fig.add_subplot(2, 3, 2, projection='3d') va2 = VisualArray(b, fig=fig, ax=ax) va2.ax.set_title(f'b of shape=(2, 3, 3)') va2.set_colors(va2.get_indices().T, color='lightgreen', basecolor='aqua') va2.vizualize(fixview=True, usetex=False, scale=1.2) va2.ax.dist = 12 ax = fig.add_subplot(2, 3, 3, projection='3d') va3 = VisualArray(c, fig=fig, ax=ax) va3.ax.set_title(f'c of shape=(3,)') va3.set_colors(va3.get_indices().T, color='lightgreen', basecolor='aqua') va3.vizualize(fixview=True,
import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray import matplotlib.gridspec as gridspec arr = np.random.randint(99, size=24).reshape((3, 4, 2)) fig = plt.figure(constrained_layout=True) spec = gridspec.GridSpec(ncols=5, nrows=1, figure=fig) ax = fig.add_subplot(spec[0], projection='3d') ax.set_title(f'body of shape={arr.shape}') va = VisualArray(arr, fig=fig, ax=ax) va.set_colors(va.get_indices().T, color='lawngreen', basecolor='aqua') va.mix_colors(va.get_indices_chequerwise((1, 1, arr.shape[2])).T, 'black') va.vizualize(fixview=True, axis_labels=('axis=0', 'axis=1', 'axis=2')) ax = fig.add_subplot(spec[1:], projection='3d') ax.set_title(f'body of shape={arr.flatten().shape}') va2 = VisualArray(va.arr.flatten(), fig=fig, ax=ax) va2.set_colors(va2.get_indices().T, color='lawngreen', basecolor='aqua') va2.mix_colors(va2.get_indices_chequerwise((1, 1, arr.shape[2])).T, 'black') va2.vizualize(fixview=True, axis_labels=(None, None, 'axis=0')) ax.dist = 8 plt.get_current_fig_manager().window.state('zoomed') plt.show()
arr = np.asarray(arr, dtype='uint32') hexarr = np.vectorize(tohexarr) return hexarr((arr[:, :, 0] << 16) + (arr[:, :, 1] << 8) + arr[:, :, 2]) def tolabels(arr): def tolabelarr(x): return r'\begin{array}{l}\,\,\sharp ' + x[1:4] + r'\\ \,\,\,\, ' + x[ 4:7] + r'\\ ' + r'\\ ' + r'\\ ' + r'\end{array}' labelarr = np.vectorize(tolabelarr) return labelarr(arr) from PIL import Image test_image = Image.open('cat.jpg') test_image = test_image.resize((32, 32), Image.ANTIALIAS) test_image = np.array(test_image).astype(int) arr = tohex(test_image) va = VisualArray(arr) cells = va.get_indices() x, y, z = cells.T va.set_colors(cells.T, color=va.arr[x, y, z]) va.arr = tolabels(va.arr) va.vizualize(fixview=True, scale=0.35, axis_labels=(None, None, None)) va.ax.dist = 11.5 #zoom out a little; change to 3.5 for higher zoom plt.get_current_fig_manager().window.state('zoomed') plt.show()