๐งฎโ๏ธ๐ Plotting 1D Functionsยถ
This page demonstrates how to plot 1D arrays in Python, which we might want to do either if we have some discrete data points that we'd like to visualize or if we want to draw smooth curve for a mathematical function.
import matplotlib.pyplot as plt
import numpy as np
How do we plot one array against another?ยถ
Let's construct two 1D arrays, and plot one as x and one as y, using a few different plotting styles. We'll start by constructing two simple arrays.
x = np.array([1,1,2,3,5,8])
y = np.array([3,1,4,1,5,9])
Next, we'll plot them. The plt.plot function will by default draw lines without points, and the plt.scatter function will draw points without lines.
plt.plot(x, y);
plt.scatter(x, y);
Both tools accept keyword arguments to customize the appearance of the plotted data. Here are a few tiny examples; notice the keyword names are different between plt.plot and plt.scatter.
plt.plot(x, y, linewidth=3, color='orchid', marker='s', markersize=10)
[<matplotlib.lines.Line2D at 0x116d9b800>]
plt.scatter(x, y, c='orchid', marker='s', s=100);
One snazzy feature of plt.scatter is that you can change the color or size of the points based on arrays, allowing you to express additional dimensions on the same plot. The symbol areas will scale with the numbers in s=, and numbers in c= will be translated to color via a colormap.
plt.scatter(x, y, s=100*x**2, c=x+y, cmap='cividis');
How do we plot smooth curves of mathematical functions?ยถ
We can use the same tools to plot smooth mathematical functions, but we have to create the grid of values on which it will be evaluated ourselves. Three tools are commonly very useful for creating these grids.
np.arange(start, stop, step) will create an array that goes linearly from start up to (but not including) stop in steps of step. It's useful if exactly specifying the step size is what matters most to you.
np.arange(5, 7, 0.2)
array([5. , 5.2, 5.4, 5.6, 5.8, 6. , 6.2, 6.4, 6.6, 6.8])
np.linspace(start, stop, num) will create an array that goes linearly from start up to (and including) stop in num total steps. It's useful if the total number of elements in your array is what matters most to you.
np.linspace(2, 4, 6)
array([2. , 2.4, 2.8, 3.2, 3.6, 4. ])
np.logspace(start, stop, num) will create an array that goes logarithmically from 10**start up to (and including) 10**stop in num total steps. It's useful if you're making a plot with logarithmic axes but still want the spacing to appear uniform.
np.logspace(0, 2, 5)
array([ 1. , 3.16227766, 10. , 31.6227766 ,
100. ])
To plot a function, we specify the array using one of those tools and do whatever calculation we want.
theta = np.linspace(-5*np.pi, 5*np.pi, 1000)
y = np.sin(theta)/(10 + theta**2)
If your calculation is more complicated than just a simple function, it might be useful to check that your dependent variable (y) has the same shape as your independent one (theta).
theta.shape
(1000,)
y.shape
(1000,)
Then, we can just plot those two arrays against each other.
plt.plot(theta, y);
Of course, we should also add human-friendly aspects like labels, titles, and colorbars.
plt.scatter(theta, y, c=np.sin(theta), s=49*(1 + np.cos(theta)))
plt.xlabel(r'$\theta$ (radians)');
plt.ylabel('y (some amazing unit)')
plt.title('What a swell plot!')
plt.colorbar();
How can we learn more?ยถ
These tools are enormously powerful, but you may need to do a little research to figure out what options to use and how to combine commands to achieve what effects you desire. To see what's available for each function, you can run plt.plot? or plt.scatter? (or type the function name and then hit <tab>) from within a jupyter cell. Or, for all the lovely details, peruse the online documentation for matplotlib.pyplot.