How it works…

To start the plotting constructions, we use the figure() function, as shown in the following line of code:

plt.figure(figsize=(6,6))

The main purpose of this call is to set the figure size, which needs adjustment, since we plan to make several plots in the same figure. After creating the figure, we add four plots with code, as demonstrated in the following segment:

plt.subplot(2, 2, 3)
yvalues = xvalues ** 3
plt.plot(xvalues, yvalues, color='red')
plt.xlabel('$x$')
plt.ylabel('$x^3$')

In the first line, the plt.subplot(2, 2, 3) call tells pyplot that we want to organize the plots in a two-by-two layout, that is, in two rows and two columns. The last argument specifies that all following plotting commands should apply to the third plot in the array. Individual plots are numbered, starting with the value 1 and counting across the rows and columns of the plot layout.

We then generate the line plot with the following statements:

yvalues = xvalues ** 3
plt.plot(xvalues, yvalues, color='red')

The first line of the preceding code computes the yvalues array, and the second draws the corresponding graph. Notice that we must set options such as line color individually for each subplot.

After the line is plotted, we use the xlabel() and ylabel() functions to create labels for the axes. Notice that these have to be set up for each individual subplot too.

After creating the subplots, we explain the subplots:

  • plt.suptitle('Polynomial Functions') sets a common title for all subplots
  • plt.tight_layout() adjusts the area taken by each subplot, so that axes' legends do not overlap
  • plt.subplots_adjust(top=0.90) adjusts the overall area taken by the plots, so that the title displays correctly
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset