oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Subplots #479

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

Sometimes, we want to display two lines side-by-side, rather than in the same set of x- and y-axes. When we have multiple axes in the same picture, we call each set of axes a subplot. The picture or object that contains all of the subplots is called a figure.

We can have many different subplots in the same figure, and we can lay them out in many different ways. We can think of our layouts as having rows and columns of subplots. For instance, the following figure has six subplots split into 2 rows and 3 columns:

https://content.codecademy.com/courses/matplotlib/six_subplots.svg

oldoc63 commented 1 year ago

We can create subplots using .subplot(). The command plt.subplot() needs three arguments to be passed into it:

For instance, the command plt.subplot(2,3,4) would create "Subplot 4" from the figure above.

Any plt.plot() that comes after plt.subplot() will create a line plot in the specified subplot. For instance:

oldoc63 commented 1 year ago
  1. We have defined the lists months, temperature, and flights_to_hawaii. Using the plt.subplot command, plot temperature vs months in the left box of a figure that has 1 row with 2 columns.
oldoc63 commented 1 year ago
  1. Plot flights_to_hawaii vs temperature in the same figure, to the right of your first plot. Add the parameter "o" to the end of your call to plt.plot to make the plot into a scatterplot, if you want.
oldoc63 commented 1 year ago

Sometimes, when we are putting multiple subplots together, some elements can overlap and make the figure unreadable. We can customize the spacing between our subplots to make sure that the figure we create is visible and easy to understand. To do this, we use the plt.subplots_adjust() command. subplots_adjust() has some keyword arguments that can move your plots within the figure:

For example, if we were adding space to the bottom of a graph by changing the bottom margin to 0.2 (instead of the default of 0.1), we would use the command: plt.subplots_adjust(bottom=0.2)

We can also use multiple keyword arguments, if we need to adjust multiple margins. For instance, we could adjust both the top and the hspace: plt.subplots_adjust(top=0.95, hspace=0.25)

oldoc63 commented 1 year ago
  1. We are going to create a figure that has two rows of subplots. It should have:
    • one subplot in the top row
    • two subplots in the bottom row
oldoc63 commented 1 year ago
  1. Plot straight_line vs x in the subplot at the top.
oldoc63 commented 1 year ago
  1. Now, use the plt.subplot() command to select the box in the first column of the second row. Plot parabola vs x in this box.
oldoc63 commented 1 year ago
  1. Now, use the plt.subplot() command to select the box in the second column of the second row. Plot cubic vs x in this box.
oldoc63 commented 1 year ago
  1. Increase the spacing between horizontal subplots to 0.35 and the bottom margin to 0.2.