Functional Approach
The functional approach to plotting in Matplotlib is a way of quickly generating a single-axis plot. Often, this is the approach taught to beginners. The functional approach allows the user to customize and save plots as image files in a chosen directory. In the following exercises and activities, you will learn how to build line plots, bar plots, histograms, box-and-whisker plots, and scatterplots using the functional approach.
Exercise 13: Functional Approach – Line Plot
To get started with Matplotlib, we will begin by creating a line plot and go on to customize it:
- Generate an array of numbers for the horizontal axis ranging from 0 to 10 in 20 evenly spaced values using the following code:
import numpy as np
x = np.linspace(0, 10, 20)
- Create an array and save it as object y. The snippet of the following code cubes the values of x and saves it to the array, y:
y = x**3
- Create the plot as follows:
import matplotlib.pyplot as plt
plt.plot(x, y)
plt.show()
See...