Matplotlib Flashcards
Matplotlib
• Powerful plotting library for python. It emulates the plotting style of matlab, a proprietary software similar to numpy commonly used in engineering and science.
Scatter plot
• import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6]) y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt. scatter(x, y)
plt. show()
Line Plot
• plt.plot(ypoints, linestyle = ‘dotted’)
plt.show()
Bar Plot
• plt.bar(x,y)
plt.show()
• plt.barh(x, y)
plt.show()
Histogram
• x = np.random.normal(170, 10, 250)
plt. hist(x)
plt. show()
Heatmap
• Like a 2-D histogram, heatmaps are useful for visually comparing 2-dimensions of data (X and Y axes) versus a metric of interest, shown in color. A common use case is when the metric is correlation, similar to R’s corrplot(). This can be done with plt.imshow(), which takes a matrix of values then represents the matrix visually.
Digital Image
• The function plt.imshow() can also display a digital image, which is itself a 2-D matrix of Red/Green/Blue (RGB) color values. The size of the matrix is also called the resolution of the image, e.g. 1440 x 1080 (HDTV) or 3840 x 2160 (4K). The higher the resolution, the more pixels are used to store the photo, which allows for storing smoother and/or finer color patterns than with fewer pixels.