Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Visualise lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Explain the evidence · V26 · 8 MIN

Axes and scales

Choose limits, ticks and transformations consciously.

Exercises within this concept

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

Understand the idea

An axis scale controls the spacing of values; limits control the visible range. Neither changes the underlying observations.

Concept sketch: equal x spacing = equal ratios on log scale0110100log x; y starts at 0equal x spacing = equal ratios on log scale
Illustration · not the exercise output

A small example

On a log axis, 1→10 and 10→100 take equal space: each is a tenfold increase.

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="requests", y="response_ms", ax=ax)
ax.set_xscale("log")
ax.set_ylim(bottom=0)
ax.tick_params(axis="x", labelrotation=30)
ax.set(title="Service load test", xlabel="Requests", ylabel="Response time (ms)")
fig.tight_layout()
plt.show()

What each part does

ax.set_xscale("log")
logarithmic x axis
ax.set_ylim(bottom=0)
y begins at zero
Other choices for later exercises
ax.tick_params(axis="x", labelrotation=30)
rotate x labels

Your inputs

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

Service load test · 6 synthetic rows
requestsresponse_ms
1035
3037
10042
30060
1000110
3000240

Your task · Follow

  1. The supplied df spans several orders of magnitude in request count.
  2. Plot requests against response_ms on a logarithmic x-axis and a linear y-axis starting at zero.
  3. Rotate x tick labels by 30 degrees.
  4. Use: set_xscale().
  5. Chart: title "Service load test"; x "Requests"; y "Response time (ms)".
Hint

Choose the observations first, then map the requested measurements to the chart.

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="requests", y="response_ms", ax=ax)
ax.set_xscale("log")
ax.set_ylim(bottom=0)
ax.tick_params(axis="x", labelrotation=30)
ax.set(title="Service load test", xlabel="Requests", ylabel="Response time (ms)")
fig.tight_layout()
plt.show()
Your task · Follow
  1. The supplied df spans several orders of magnitude in request count.
  2. Plot requests against response_ms on a logarithmic x-axis and a linear y-axis starting at zero.
  3. Rotate x tick labels by 30 degrees.
  4. Use: set_xscale().
  5. Chart: title "Service load test"; x "Requests"; y "Response time (ms)".

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.