Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Visualise lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Make a useful report · V34 · 8 MIN

Save a figure

Export the specific Figure you have finished.

Exercises within this concept

  1. FollowFollow the techniqueCurrent exercise
  2. ChangeAdapt a requirement
  3. TransferCombine earlier skills

Understand the idea

savefig exports a specific Figure to a file. Finish the labels and layout before saving, then display it.

Concept sketch: save this Figure as a PNG filechart.png150 dpisave this Figure as a PNG file
Illustration · not the exercise output

A small example

A 6 × 4 inch Figure at 150 dpi is about 900 × 600 pixels before tight cropping changes the edges.

Follow the code

Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize=(6, 4))
sns.scatterplot(data=df, x="hours", y="score", ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="score")
fig.tight_layout()
fig.savefig("chart.png", dpi=150, bbox_inches="tight")
plt.show()

What each part does

fig.savefig("chart.png", dpi=150, bbox_inches="tight")
export this Figure
.png
raster image format
dpi=150
pixels per inch

Your inputs

The editable setup on the right creates df. Run executes the setup and your work from top to bottom.

Study club · 8 synthetic rows
studentclubhoursscoregroup
AriArt152A
BoCode371A
CyArt265B
DeeCode482B
EliArt589A
FloCode376A
GusArt693B
HanCode261B

Your task · Follow

  1. Using df, scatter hours against score, finish its title and labels, then save chart.png at 150 dpi with a tight bounding box before displaying.
  2. Use title "Study club", x label "hours", y label "score".
  3. Display the chart with plt.show().
  4. Use: savefig().
Hint

Save the explicit finished Figure before display, with the requested resolution.

Reveal solution

One way to do it. Keep any supplied setup in the editor and use this in the Your work section.

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize=(6, 4))
sns.scatterplot(data=df, x="hours", y="score", ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="score")
fig.tight_layout()
fig.savefig("chart.png", dpi=150, bbox_inches="tight")
plt.show()
Your task · Follow
  1. Using df, scatter hours against score, finish its title and labels, then save chart.png at 150 dpi with a tight bounding box before displaying.
  2. Use title "Study club", x label "hours", y label "score".
  3. Display the chart with plt.show().
  4. Use: savefig().

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Each run executes all editor code in a fresh Python session. Display charts with plt.show().

Python starts when you open a lesson.

Output

Run your code to see what Python returns.