Learn

When we’re making lots of plots, it’s easy to end up with lines that have been plotted and not displayed. If we’re not careful, these “forgotten” lines will show up in your new plots. In order to be sure that you don’t have any stray lines, you can use the command plt.close('all') to clear all existing plots before you plot a new one.

Previously, we learned how to put two sets of axes into the same figure. Sometimes, we would rather have two separate figures. We can use the command plt.figure() to create new figures and size them how we want. We can add the keyword figsize=(width, height) to set the size of the figure, in inches. We use parentheses (( and )) to pass in the width and height, which are separated by a comma (,).

To create a figure with a width of 4 inches, and height of 10 inches, we would use:

plt.figure(figsize=(4, 10))

It would look tall and skinny, like this:

tall_fig

Once we’ve created a figure, we might want to save it so that we can use it in a presentation or a website. We can use the command plt.savefig() to save out to many different file formats, such as png, svg, or pdf. After plotting, we can call plt.savefig('name_of_graph.png'):

# Figure 2 plt.figure(figsize=(4, 10)) plt.plot(x, parabola) plt.savefig('tall_and_narrow.png')

This will save tall_and_narrow.png to our file system.

Instructions

1.

First, close all plots to make sure we have no lines already plotted that we’ve forgotten about.

2.

Create a figure and plot word_length against years. This dataset represents the lengths of the winning words of the Scripps National Spelling Bee over 11 years. Save this figure in a file called 'winning_word_lengths.png'.

3.

On the next line, create a figure with 7 inches of width and 3 inches of height and plot power_generated against years. This dataset represents the power generated by nuclear plants in the United States over 11 years. Save this figure in a file called 'power_generated.png'.

Sign up to start coding

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?