Scatter Plot

Python · Data Visualization

Theory

A scatter plot draws one dot per record, with one numeric variable on the x-axis and another on the y-axis, so you can see whether they move together. Dots rising from left to right mean a positive relationship, falling means negative, and a shapeless cloud means little relationship. Points far from the rest are outliers.

ax.scatter(x, y) can encode more variables: c= colours each dot by a value or category (with cmap= and fig.colorbar for numbers), s= sets dot size, and alpha=0.5 makes overlapping dots visible. The strength of a straight-line relationship is the correlation coefficient r from np.corrcoef, between -1 and 1, and np.polyfit(x, y, 1) gives the slope and intercept of a trend line you can draw with ax.plot. Correlation alone does not prove that one variable causes the other.

Why we use it

Before modelling, you need to know how two variables relate. A scatter plot shows the direction, strength and shape of the relationship and exposes outliers and clusters that summary numbers miss.

When to use it

Use a scatter plot for two numeric variables measured on the same records, such as experience vs salary or ad spend vs sales. For a variable over time use a line plot; with many thousands of points use alpha, smaller dots or a 2-D histogram.

How it works

  1. Two numeric columns from the same records
  2. ax.scatter(x, y) places one dot per record
  3. Another variable to show? Yes: Encode it with c= (colour) or s= (size). No: Keep plain dots.
  4. Look for direction, strength, clusters and outliers
  5. Optionally add a trend line with np.polyfit
  6. Relationship understood before modelling

Syntax

ax.scatter(x, y, c=values, s=sizes, alpha=0.7, cmap='viridis')
fig.colorbar(sc, ax=ax)
np.corrcoef(x, y)[0, 1]
slope, intercept = np.polyfit(x, y, 1)

Example

fig, ax = plt.subplots()
ax.scatter([1, 3, 5, 7], [4.0, 6.0, 9.0, 11.5])
ax.set_xlabel('Experience (years)')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic