ch2 Slides
ch2 Slides
Working with
2D arrays
Introduction to Data Visualization with Python
… … … … …
Introduction to Data Visualization with Python
Using meshgrid()
meshgrids.py
import numpy as np
u = np.linspace(-2, 2, 3)
v = np.linspace(-1, 1, 5)
X,Y = np.meshgrid(u, v)
Z = X**2/25 + Y**2/4
Introduction to Data Visualization with Python
Using meshgrid()
X:
[[-2. 0. 2.]
[-2. 0. 2.]
[-2. 0. 2.]
[-2. 0. 2.]
[-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 with Python
Meshgrid
Introduction to Data Visualization with Python
Sampling on a grid
meshgrids.py
import numpy as np
import matplotlib.pyplot as plt
u = np.linspace(-2, 2, 3)
v = np.linspace(-1, 1, 5)
X,Y = np.meshgrid(u, v)
Z = X**2/25 + Y**2/4
print('Z:\n', Z)
plt.set_cmap('gray')
plt.pcolor(Z)
plt.show()
Introduction to Data Visualization with Python
Sampling on a grid
Z:
[[ 0.41 0.25 0.41 ]
[ 0.2225 0.0625 0.2225]
[ 0.16 0. 0.16 ]
[ 0.2225 0.0625 0.2225]
[ 0.41 0.25 0.41 ]]
Introduction to Data Visualization with Python
Sampling on a grid
Introduction to Data Visualization with Python
Sampling on a grid
Introduction to Data Visualization with Python
import numpy as np
import matplotlib.pyplot as plt
plt.pcolor(Z)
plt.show()
Introduction to Data Visualization with Python
Let’s practice!
INTRODUCTION TO DATA VISUALIZATION WITH PYTHON
Visualizing
bivariate functions
Introduction to Data Visualization with Python
Bivariate functions
Introduction to Data Visualization with Python
Pseudocolor plot
In [1]: import numpy as np
In [7]: plt.pcolor(Z)
In [8]: plt.show()
Introduction to Data Visualization with Python
Pseudocolor plot
Introduction to Data Visualization with Python
Color bar
In [9]: plt.pcolor(Z)
In [10]: plt.colorbar()
In [11]: plt.show()
Introduction to Data Visualization with Python
Color bar
Introduction to Data Visualization with Python
Color map
In [12]: plt.pcolor(Z, cmap= 'gray')
In [13]: plt.colorbar()
In [14]: plt.show()
Introduction to Data Visualization with Python
Color map
Introduction to Data Visualization with Python
Color map
In [15]: plt.pcolor(Z, cmap= 'autumn')
In [16]: plt.colorbar()
In [17]: plt.show()
Introduction to Data Visualization with Python
Color map
Introduction to Data Visualization with Python
Axis tight
In [18]: plt.pcolor(Z)
In [19]: plt.colorbar()
In [20]: plt.axis('tight')
In [21]: plt.show()
Introduction to Data Visualization with Python
Axis tight
Introduction to Data Visualization with Python
In [23]: plt.colorbar()
In [24]: plt.show()
Introduction to Data Visualization with Python
Axes determined by
mesh grid arrays X, Y
Introduction to Data Visualization with Python
Contour plots
In [25]: plt.contour(Z)
In [26]: plt.show()
Introduction to Data Visualization with Python
Contour plots
Introduction to Data Visualization with Python
More contours
In [27]: plt.contour(Z, 30)
In [28]: plt.show()
Introduction to Data Visualization with Python
More contours
Introduction to Data Visualization with Python
In [30]: plt.show()
Introduction to Data Visualization with Python
In [32]: plt.colorbar()
In [33]: plt.show()
Introduction to Data Visualization with Python
More information
● API has many (optional) keyword arguments
● More in matplotlib.pyplot documentation
● More examples: h!p://matplotlib.org/gallery.html
INTRODUCTION TO DATA VISUALIZATION WITH PYTHON
Let’s practice!
INTRODUCTION TO DATA VISUALIZATION WITH PYTHON
Visualizing
bivariate
distributions
Introduction to Data Visualization with Python
Distributions of 2D points
● 2D points given as two 1D arrays x & y
● Goal: generate a 2D histogram from x & y
Introduction to Data Visualization with Python
Histograms in 1D
● Choose bins (intervals)
● Count realizations within bins & plot
Introduction to Data Visualization with Python
Histograms in 1D
In [1]: counts, bins, patches = plt.hist(x, bins=25)
In [2]: plt.show()
Introduction to Data Visualization with Python
Bins in 2D
● Different shapes available for binning points
● Common choices: rectangles & hexagons
Introduction to Data Visualization with Python
In [2]: plt.colorbar()
In [5]: plt.show()
Introduction to Data Visualization with Python
In [2]: plt.colorbar()
In [5]: plt.show()
Introduction to Data Visualization with Python
Let’s practice!
INTRODUCTION TO DATA VISUALIZATION WITH PYTHON
Working
with images
Introduction to Data Visualization with Python
Images
● Grayscale images: rectangular 2D arrays
● Color images: typically three 2D arrays (channels)
● RGB (Red-Green-Blue)
● Channel values:
● 0 to 1 (floating-point numbers)
● 0 to 255 (8 bit integers)
Introduction to Data Visualization with Python
Loading images
In [1]: img = plt.imread('sunflower.jpg')
In [2]: print(img.shape)
(480, 640, 3)
In [3]: plt.imshow(img)
In [4]: plt.axis('off')
In [5]: plt.show()
Introduction to Data Visualization with Python
Loading images
Introduction to Data Visualization with Python
In [7]: print(collapsed.shape)
(480, 640)
In [8]: plt.set_cmap('gray')
In [10]: plt.axis('off')
In [11]: plt.show()
Introduction to Data Visualization with Python
Uneven samples
In [12]: uneven = collapsed[::4,::2] # nonuniform subsampling
In [13]: print(uneven.shape)
(120, 320)
In [14]: plt.imshow(uneven)
In [15]: plt.axis('off')
In [16]: plt.show()
Introduction to Data Visualization with Python
Uneven samples
Introduction to Data Visualization with Python
In [18]: plt.axis('off')
In [19]: plt.show()
Introduction to Data Visualization with Python
Adjusting extent
In [20]: plt.imshow(uneven, cmap='gray', extent=(0,640,0,480))
In [21]: plt.axis('off')
In [22]: plt.show()
Introduction to Data Visualization with Python
Adjusting extent
INTRODUCTION TO DATA VISUALIZATION WITH PYTHON
Let’s practice!