"""this example demonstrates how to make a multiple subplots of a VisualArray in multiple sides""" import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray arr = np.random.randint(100, size=60).reshape((2, 6, 5)) coords = np.array(list(np.ndindex(arr.shape))) cells = coords[np.sum(coords, axis=1) % 3 == 0] fig = plt.figure() ax = fig.add_subplot(2, 1, 1, projection='3d') ax.set_title('top') va = VisualArray(arr, fig=fig, ax=ax) va.set_colors(cells.T, color='yellow', basecolor='lightblue') va.vizualize(fixview=True) ax = fig.add_subplot(2, 1, 2, projection='3d') ax.set_title('bottom') va = VisualArray(arr, fig=fig, ax=ax) va.set_colors(cells.T, color='yellow', basecolor='lightblue') va.vizualize(fixview=True) ax.elev = -30 plt.show()
from numpyviz import VisualArray import matplotlib.gridspec as gridspec arr = np.random.randint(99, size=(3, 3, 4)) fig = plt.figure(constrained_layout=True) spec = gridspec.GridSpec(ncols=10, nrows=3, hspace=0.05, wspace=0.05, figure=fig) ax = fig.add_subplot(spec[1, 0], projection='3d') ax.dist = 11 #zoom out a little ax.set_title(f'arr of shape={arr.shape}') va = VisualArray(arr, fig=fig, ax=ax) va.set_colors(np.where(arr > 65), color='lawngreen', basecolor='aqua') va.vizualize(fixview=True, axis_labels=('axis=0', 'axis=1', 'axis=2')) idx = np.indices(arr.shape) for n in range(3): ax = fig.add_subplot(spec[n, 2], projection='3d') ax.dist = 11 #zoom out a little ax.set_title(f'np.indices({arr.shape})[{n}]') va = VisualArray(idx[n], fig=fig, ax=ax) va.set_colors(np.where(arr > 65), color='lightgreen', basecolor='lightblue') va.vizualize(fixview=True, axis_labels=('axis=0', 'axis=1', 'axis=2')) for n in range(3): ax = fig.add_subplot(spec[n, 4:], 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()
arr = np.array([[ r'$\times$', r'$x^2$', r'$y^2$', r'$z^2$', r'$-xy$', r'$-yz$', r'$-xz$' ], [r'$x$', r'$x^3$', r'$xy^2$', r'$xz^2$', r'$-x^2y$', r'$-xyz$', r'$-x^2z$'], [ r'$y$', r'$x^2y$', r'$y^3$', r'$yz^2$', r'$-xy^2$', r'$-y^2z$', r'$-xyz$' ], [ r'$z$', r'$x^2z$', r'$y^2z$', r'$z^3$', r'$-xyz$', r'$-yz^2$', r'$-xz^2$' ]]) va = VisualArray(arr) blue = [(0, i, 0) for i in range(1, 4)] + [(0, 0, i) for i in range(1, 7)] lime = [(0, 1, 1), (0, 2, 2), (0, 3, 3), (0, 1, -2), (0, 2, -1), (0, 3, -3)] va.set_colors(zip(*blue), color='lightblue', basecolor='white') va.set_colors(zip(*lime), color='lime') va.set_colors(zip(*[(0, 2, -2), (0, -1, 2)]), color='#ffff00') va.set_colors(zip(*[(0, 1, 2), (0, 2, 4)]), color='#dddd00') va.set_colors(zip(*[(0, 1, 3), (0, -1, -1)]), color='#bbbb00') va.set_colors(zip(*[(0, 1, 4), (0, 2, 1)]), color='#999900') va.set_colors(zip(*[(0, 1, 6), (0, -1, 1)]), color='#777700') va.set_colors(zip(*[(0, 2, 3), (0, -1, -2)]), color='#555500') va.vizualize(fixview=True, axis_labels=[ None, '$x+y+z$', r'$' + r'\!' * 35 + r' x^2+y^2+z^2-xy-yz-zx$' ], scale=0.4) va.ax.set_title( 'Why $(x+y+z)(x^2+y^2+z^2-xy-yz-zx) = x^3+y^3+z^3-3xyz$? \n Simplify monochromatic pairs of yellow cubes!' )
import matplotlib.pyplot as plt from numpyviz import VisualArray fig = plt.figure() arr = np.array([[[r'$x^3y^2$', r'$x^2y^3$'], [r'$x^2y^3$', r'$xy^4$']], [[r'$x^2y^3$', r'$xy^4$'], [r'$xy^4$', r'$y^5$']], [[r'$x^4y$', r'$x^3y^2$'], [r'$x^3y^2$', r'$x^2y^3$']], [[r'$x^3y^2$', r'$x^2y^3$'], [r'$x^2y^3$', r'$xy^4$']], [[r'$x^4y$', r'$x^3y^2$'], [r'$x^3y^2$', r'$x^2y^3$']], [[r'$x^3y^2$', r'$x^2y^3$'], [r'$x^2y^3$', r'$xy^4$']], [[r'$x^5$', r'$x^4y$'], [r'$x^4y$', r'$x^3y^2$']], [[r'$x^4y$', r'$x^3y^2$'], [r'$x^3y^2$', r'$x^2y^3$']]]) ax = fig.add_subplot(1, 2, 1, projection='3d') va = VisualArray(arr, fig=fig, ax=ax) va.set_colors(np.where(arr == '$x^5$'), color='#6666ff') va.set_colors(np.where(arr == '$x^4y$'), color='#66ff66') va.set_colors(np.where(arr == '$x^3y^2$'), color='#ff6666') va.set_colors(np.where(arr == '$x^2y^3$'), color='#ffff66') va.set_colors(np.where(arr == '$xy^4$'), color='#ff66ff') va.set_colors(np.where(arr == '$y^5$'), color='#66ffff') va.permute(shape=(2, 2, 2, 2, 2)) va.vizualize( fixview=True, axis_labels=[ r'$\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!' + 'x(x+y)\!\!+\!\!y(x+y)$', r'$\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!' + 'x(x+y)\!\!+\!\!y(x+y)$', r'$\!\!x+y$' ], scale=0.6) va.ax.set_title(
import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray arr = np.arange(64).reshape((1, 8, 8)) va = VisualArray(arr) cells = va.get_indices_chequerwise(window=(1, 1, 1)) va.set_colors(cells.T, color='white', basecolor='grey') va.vizualize(fixview=True, axis_labels=(None, None, None)) va.ax.dist = 12.5 #zoom out a little plt.show()
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()
import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray arr = np.array([[11, 12, 13, 14, 15, 16], [21, 22, 23, 24, 25, 26], [31, 32, 33, 34, 35, 36], [41, 42, 43, 44, 45, 46]]) a = arr.copy() fig = plt.figure() ax = fig.add_subplot(2, 3, 1, projection='3d') va = VisualArray(arr, fig=fig, ax=ax) coords = va.get_indices() va.set_colors(coords.T, color='yellow', basecolor='lightblue') va.vizualize(fixview=True, axis_labels=(None, 'axis=0', 'axis=1')) va.ax.set_title('arr = array of shape (4,6)') va.ax.dist = 12.5 arr = a.reshape((4, 3, 2)) ax = fig.add_subplot(2, 3, 2, projection='3d') va = VisualArray(arr, fig=fig, ax=ax) coords = va.get_indices() va.set_colors(coords[coords[:, 1] == 1].T, color='lightgreen', basecolor='lightblue') va.set_colors(coords[coords[:, 1] == 2].T, color='thistle', basecolor='lightblue') va.vizualize(fixview=True) va.ax.set_title('arr1 = array of shape (4, 3, 2)') va.ax.dist = 12.5
"""this is an example that demonstrates how to multiply three polynomials""" import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray arr = np.array([[[r'$x^2y$', r'$xy^2$'], [r'$xy^2$', r'$y^3$']], [[r'$x^3$', r'$x^2y$'], [r'$x^2y$', r'$xy^2$']]]) va = VisualArray(arr) va.set_colors(np.where(arr == '$y^3$'), color='#6666ff') va.set_colors(np.where(arr == '$x^3$'), color='#ff66ff') va.set_colors(np.where(arr == '$x^2y$'), color='#66ff66') va.set_colors(np.where(arr == '$xy^3$'), color='#66ffff') va.vizualize(fixview=False, axis_labels=['$x+y$', '$x+y$', '$x+y$'], scale=0.7) va.ax.set_title('Why $(x+y)^3 = x^3+3x^2y+3xy^2+y^3$?') plt.show()
"""this example demonstrates how to make a multiple subplots of a VisualArray in multiple sides""" import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray arr = np.array([[[1, 2, 13], [12, 2, 32], [61, 2, 6], [1, 23, 3], [1, 21, 3], [91, 2, 38]]]) fig = plt.figure() ax = fig.add_subplot(1, 3, 1, projection='3d') ax.set_title('a = array of shape (1, 6, 3)') va = VisualArray(arr, fig=fig, ax=ax) va.set_colors(np.array(list(np.ndindex(arr.shape))).T, color='yellow', basecolor='lightblue') va.vizualize(fixview=True) arr = arr[:, :, :2] ax = fig.add_subplot(1, 3, 2, projection='3d') ax.set_title('a = a[:,:,:2] \n' 'a is of shape (1, 6, 2) now') va = VisualArray(arr, fig=fig, ax=ax) va.set_colors(np.array(list(np.ndindex(arr.shape))).T, color='yellow', basecolor='lightblue') va.vizualize(fixview=True) arr = arr.squeeze() ax = fig.add_subplot(1, 3, 3, projection='3d') ax.set_title('a = a.squeeze() \n' 'a is of shape (6, 2) now')
form[-len(c):] = list(c) return ''.join(form) arr = np.asarray(arr, dtype='uint32') hexarr = np.vectorize(tohexarr) return hexarr((arr[:, :, 0] << 16) + (arr[:, :, 1] << 8) + arr[:, :, 2]) 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) t = time() va = VisualArray(test_image) coords = va.get_indices() eye = np.eye(3, dtype=int) #input of set_colors can be another array of len(cellsT) for i in range(3): color = np.expand_dims(test_image[:, :, i], axis=2) * eye[i] cellsT = coords[coords[:, 2] == i].T #list of coords where z = i va.set_colors(cellsT, color=tohex(color)[cellsT[0], cellsT[1]]) va.vizualize(fixview=True, scale=0.7, axis_labels=(None, None, None)) va.ax.azim = 40 #change to 40 to see another back side va.ax.elev = 20 va.ax.dist = 8 #zoom in a little print(time() - t) plt.get_current_fig_manager().window.state('zoomed') plt.show()
import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray def argmin_axis2(arr): a3 = np.argmin(arr, axis=2) print(a3.__repr__()) a1, a2 = np.indices((arr.shape[0], arr.shape[1])) x, y, z = zip(*np.broadcast(a1, a2, a3)) return x, y, z arr = np.random.randint(99, size=8 * 8 * 3).reshape((8, 8, 3)) va = VisualArray(arr) va.set_colors(argmin_axis2(arr), color='yellow', basecolor='lightblue') va.vizualize(fixview=True) va.ax.set_title('argmin on axis=-1') plt.show()
import numpy as np import matplotlib.pyplot as plt from numpyviz import VisualArray arr = np.random.randint(99, size=96).reshape((1, 8, 12)) w = (2, 4) fig = plt.figure() ax = fig.add_subplot(1, 3, 1, projection='3d') ax.set_title('image') va = VisualArray(arr, fig=fig, ax=ax) #indices of every cell cells = va.get_indices_chequerwise(window=(1, ) + w) va.set_colors(cells.T, color='lawngreen', basecolor='aqua') va.vizualize(fixview=True, axis_labels=(None, 'axis=1', 'axis=0')) ax = fig.add_subplot(1, 3, 2, projection='3d') va2 = VisualArray(va.arr, va.colors, fig=fig, ax=ax) #shape: (1, 8, 12) shape = (va2.arr.shape[1] // w[0], w[0], va2.arr.shape[2] // w[1], w[1]) ax.set_title(f'image.reshape{shape}') va2.reshape(shape) #shape: (4, 2, 3, 4) new_arr, new_colors = va2.arr.copy(), va2.colors.copy() va2.permute(shape) va2.vizualize(fixview=True, axis_labels=('axis=0', 'axis=1', 'axis=2')) def argmin(arr): #bug... a2 = arr.argmin(axis=1) a4 = arr.argmin(axis=3) a1, a3 = np.indices((arr.shape[0], arr.shape[2])) x, y, z, t = zip(*np.broadcast(a1, a2, a3, a4))
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,
[0., 0., 0., 4., 0., 0., 0., 4., 0., 0., 0., 4.], [1., 0., 3., 0., 1., 0., 4., 0., 1., 0., 5., 0.], [0., 2., 0., 0., 0., 2., 0., 0., 0., 2., 0., 0.], [0., 0., 3., 0., 0., 0., 3., 0., 0., 0., 3., 0.], [0., 0., 0., 4., 0., 0., 0., 4., 0., 0., 0., 4.], [1., 0., 6., 0., 1., 0., 7., 0., 1., 0., 8., 0.], [0., 2., 0., 0., 0., 2., 0., 0., 0., 2., 0., 0.], [0., 0., 3., 0., 0., 0., 3., 0., 0., 0., 3., 0.], [0., 0., 0., 4., 0., 0., 0., 4., 0., 0., 0., 4.]]).reshape( (1, 12, 12)).astype(int) fig = plt.figure() ax = fig.add_subplot(2, 3, 1, projection='3d') va1 = VisualArray(arr, fig=fig, ax=ax) va1.ax.set_title(f'input') va1.set_colors(va1.get_indices().T, color='lightgreen', basecolor='aqua') va1.vizualize(fixview=True, axis_labels=(None, 'axis=0', 'axis=1'), usetex=False, scale=1.2) va1.ax.dist = 12 va1.azim, va1.elev = -90, 45 ax = fig.add_subplot(2, 3, 2, projection='3d') va2 = VisualArray(arr, fig=fig, ax=ax) va2.ax.set_title(f'input <-> input.reshape((4, 3, 4, 3))') w = (3, 3) shape = (arr.shape[1] // w[0], w[0], arr.shape[2] // w[1], w[1]) print(shape) va2.set_colors(va2.get_indices().T, color='lightgreen', basecolor='aqua') va2.permute(shape)
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()