We just talked about the advantages of displaying multiple plots next to each other. One common challenge with subplots–as we saw in the last exercise– is getting the graphs and their axes, labels, titles, and legends to display nicely. Luckily, the function plt.figure() can help us out.

We can adjust any single graph in a subplot using its respective code – adding xlabel or title as usual, for example. We can make changes to the whole grid by defining the space around the subplots as a “figure” using matplotlib’s general function plt.figure(), like so:

fig = plt.figure()

plt.figure can take parameters, including:

  • figsize: adjusts the dimensions of the figure
  • tight_layout: adjusts the spacing of plots for an automated “best fit” with a Boolean
  • facecolor: sets the background color
fig = plt.figure(figsize = (10, 4), tight_layout = True, facecolor=’lightgray’)

fig, the instance we’ve defined of plt.figure, also has its own methods, including:

  • suptitle(): adds a centered title at the top of all subplots, with font size and spacing parameters
  • supxlabel(): adds an xlabel to all plots
  • supylabel(): adds a ylabel to all plots
  • subplots_adjust(hspace, wspace): manually adjusts spacing around plots
fig.suptitle(‘Six line graphs’, fontsize=24, x=0.45, y=0.95) fig.supxlabel(‘Time’) fig.subplots_adjust(hspace = 0.5, wspace = 0.25)

These functions are helpful for uncluttering the figure when a single title or label applies to multiple subplots, or when graphs need more padding to prevent crowded labeling.

Let’s return to the subplots we made in the last exercise and see if we can make them visually cleaner.



To start, load the Setup cells. In the cell below, above the graph code from the last exercise, create a figure instance called fig. Give it the dimensions (18,16) and a background color of 'lightgray'. Set the tight_layout=True to optimize the spacing around each subplot. Then run the cell below to improve the graph we made in the last exercise.


The sizing and background are good, but the labels are still very hard to read. Let’s make them fully vertical by changing the xticks label rotation to -90 and removing the horizontal alignment (ha) argument. Also, add the argument fontsize=13 to each xticks function to make the xticks font bigger. Finally, let’s add a title to the whole graph: 'Species Counts in Primary, Secondary, and Selectively Logged Tapajos Forests'.


To make the title more readable, set the font size to 20. Setting tight_layout to True will automatically squeeze the (x,y) positions of everything in the figure, which is why our suptitle is directly on top of the first graph. So, to give it a little breathing room, add the argument y=1 to the suptitle function to explicitly set the title’s y-position a little lower.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?