oldoc63 / learningDS

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

Figures #482

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

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 put two set 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: plt.figure(figsize=(4,10)) Once we've created a figure, we may want to save it so that we can use it in a presentation or a web site. 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'):

plt.figure(figsize=(4,10))
plt.plot(x, parabola)
plt.savefig('tall_and_narrow.png')
oldoc63 commented 1 year ago
  1. First, close all plots to make sure we have no lines already plotted that we've forgotten about.
oldoc63 commented 1 year ago
  1. We created a figure out of the plot word_lenght against years.
  2. On the next line, create a figure with 7 inches of width and 3 inches of height and plot power generated against years. Save a figure in a file called 'power_generated.png'.