working w ith 2 d arra y s
play

Working w ith 2 D arra y s IN TR OD U C TION TO DATA VISU AL - PowerPoint PPT Presentation

Working w ith 2 D arra y s IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON Br y an Van de Ven Core De v eloper of Bokeh Reminder : N u mP y arra y s Homogeneo u s in t y pe Calc u lations all at once Inde x ing w ith brackets : A[index]


  1. Working w ith 2 D arra y s IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON Br y an Van de Ven Core De v eloper of Bokeh

  2. Reminder : N u mP y arra y s Homogeneo u s in t y pe Calc u lations all at once Inde x ing w ith brackets : A[index] for 1 D arra y A[index0, index1] for 2 D arra y INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  3. Reminder : slicing arra y s Slicing : 1 D arra y s : A[slice] , 2 D arra y s : A[slice0, slice1] Slicing : slice = start:stop:stride Inde x es from start to stop-1 in steps of stride Missing start : implicitl y at beginning of arra y Missing stop : implicitl y at end of arra y Missing stride : implicitl y stride 1 Negati v e inde x es / slices : co u nt from end of arra y INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  4. 2 D arra y s & images 0.434 0.339 0.337 0.367 ... 0.434 0.421 0.404 0.395 ... 0.350 0.388 0.340 0.340 ... 0.328 0.384 0.308 0.308 ... ... ... ... ... ... INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  5. 2 D arra y s & f u nctions INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  6. Using meshgrid () meshgrids.py : X : [[-2. 0. 2.] import numpy as np [-2. 0. 2.] u = np.linspace(-2, 2, 3) [-2. 0. 2.] v = np.linspace(-1, 1, 5) [-2. 0. 2.] X,Y = np.meshgrid(u, v) [-2. 0. 2.]] Y : [[-1. -1. -1. ] [-0.5 -0.5 -0.5] [ 0. 0. 0. ] [ 0.5 0.5 0.5] [ 1. 1. 1. ]] INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  7. Meshgrid INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  8. Sampling on a grid meshgrids.py Z : [[ 0.41 0.25 0.41 ] import numpy as np [ 0.2225 0.0625 0.2225] import matplotlib.pyplot as plt [ 0.16 0. 0.16 ] [ 0.2225 0.0625 0.2225] u = np.linspace(-2, 2, 3) [ 0.41 0.25 0.41 ]] v = np.linspace(-1, 1, 5) X,Y = np.meshgrid(u, v) Z = X**2/25 + Y**2/4 print(Z) plt.set_cmap('gray') plt.pcolor(Z) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  9. Sampling on a grid INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  10. Orientations of 2 D arra y s & images orientation.py Z : [[1 2 3] import numpy as np [4 5 6]] import matplotlib.pyplot as plt Z = np.array([[1, 2, 3], [4, 5, 6]]) print(z) plt.pcolor(Z) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  11. Let ' s practice ! IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON

  12. Vis u ali z ing bi v ariate f u nctions IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON Br y an Van de Ven Core De v eloper of Bokeh

  13. Bi v ariate f u nctions INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  14. Pse u docolor plot import numpy as np import matplotlib.pyplot as plt u = np.linspace(-2, 2, 65) v = np.linspace(-1, 1, 33) X,Y = np.meshgrid(u, v) Z = X**2/25 + Y**2/4 plt.pcolor(Z) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  15. Color bar plt.pcolor(Z) plt.colorbar() plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  16. Color map plt.pcolor(Z, cmap= 'gray') plt.colorbar() plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  17. Color map plt.pcolor(Z, cmap= 'autumn') plt.colorbar() plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  18. A x is tight plt.pcolor(Z) plt.colorbar() plt.axis('tight') plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  19. Plot u sing mesh grid # X, Y are 2D meshgrid plt.pcolor(X, Y, Z) plt.colorbar() plt.show() a x es determined b y arra y s X , Y INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  20. Conto u r plots plt.contour(Z) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  21. More conto u rs plt.contour(Z, 30) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  22. Conto u r plot u sing meshgrid plt.contour(X, Y, Z, 30) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  23. Filled conto u r plots plt.contourf(X, Y, Z, 30) plt.colorbar() plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  24. More information API has man y ( optional ) ke yw ord arg u ments More in matplotlib . p y plot doc u mentation More e x amples : h � p :// matplotlib . org / galler y. html INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  25. Let ' s practice ! IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON

  26. Vis u ali z ing bi v ariate distrib u tions IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON Br y an Van de Ven Core De v eloper of Bokeh

  27. Distrib u tions of 2 D points 2 D points gi v en as t w o 1 D arra y s x and y Goal : generate a 2 D histogram from x and y INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  28. Histograms in 1 D Choose bins ( inter v als ) Co u nt reali z ations w ithin bins & plot INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  29. Histograms in 1 D counts, bins, patches = plt.hist(x, bins=25) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  30. Bins in 2 D Di � erent shapes a v ailable for binning points Common choices : rectangles & he x agons INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  31. hist 2 d (): Rectang u lar binning # x & y are 1D arrays of same length plt.hist2d(x, y, bins=(10, 20)) plt.colorbar() plt.xlabel('weight ($\\mathrm{kg}$)') plt.ylabel('acceleration ($\\mathrm{ms}^{-2}$)}' plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  32. he x bin (): He x agonal binning plt.hexbin(x, y, gridsize=(15,10)) plt.colorbar() plt.xlabel('weight ($\\mathrm{kg}$)') plt.ylabel('acceleration ($\\mathrm{ms}^{-2}$)}' plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  33. Let ' s practice ! IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON

  34. Working w ith images IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON Br y an Van de Ven Core De v eloper of Bokeh

  35. Images Gra y scale images : rectang u lar 2 D arra y s Color images : t y picall y three 2 D arra y s ( channels ) RGB ( Red - Green - Bl u e ) Channel v al u es : 0 to 1 (� oating - point n u mbers ) 0 to 255 (8 bit integers ) INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  36. Loading images img = plt.imread('sunflower.jpg') print(img.shape) (480, 640, 3) plt.imshow(img) plt.axis('off') plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  37. Red u ction to gra y- scale image collapsed = img.mean(axis=2) print(collapsed.shape) (480, 640) plt.set_cmap('gray') plt.imshow(collapsed, cmap='gray') plt.axis('off') plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  38. Une v en samples # nonuniform subsampling uneven = collapsed[::4,::2] print(uneven.shape) (120, 320) plt.imshow(uneven) plt.axis('off') plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  39. Adj u sting aspect ratio plt.imshow(uneven, aspect=2.0) plt.axis('off') plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  40. Adj u sting e x tent plt.imshow(uneven, cmap='gray', extent=(0,640,0,480)) plt.axis('off') plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON

  41. Let ' s practice ! IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON

Recommend


More recommend