Home / Relationships / Scatter Plot

Scatter Plot in HTML, CSS and JavaScript

A scatter plot is a chart that shows the relationship between two numbers. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows used car prices against mileage.

Open live demoDownload HTML fileView code on GitHub
Scatter Plot made with HTML and JavaScript showing used car prices against mileage

What is a scatter plot?

A scatter plot is a chart that shows the relationship between two numbers. Each item is a dot, placed by one value across the bottom and another up the side.

The pattern of dots tells the story. Dots that slope down from left to right mean that as one number goes up, the other goes down, like car prices falling as the miles add up. A trend line sums up that pattern in one line.

At a glance

Best for
Seeing if two numbers move together
Data you need
Two numbers for each item
Skip it when
Data over time. Use a line chart.
Made with
HTML, CSS and vanilla JavaScript. No library.

When to use a scatter plot

  • Price against age, size or mileage
  • Ad spend against sales
  • Study hours against test scores
  • Height against weight, or any two measures

When to pick another chart

What you get in this template

How the code works

Here is a short version of the idea behind this scatter plot. The full file adds the axis, labels, tooltips, sorting and resizing on top of it.

const cars = [[12000, 24500], [45000, 19800], [78000, 15200], [101000, 12900], [130000, 9100]];
const x = m => 40 + m / 150000 * 540, y = p => 280 - p / 35000 * 260;
cars.forEach(([miles, price]) => drawCircle(x(miles), y(price), 6, '#e4572e'));

// trend line (least squares)
const n = cars.length, mx = cars.reduce((a, c) => a + c[0], 0) / n, my = cars.reduce((a, c) => a + c[1], 0) / n;
const b = cars.reduce((a, c) => a + (c[0] - mx) * (c[1] - my), 0) / cars.reduce((a, c) => a + (c[0] - mx) ** 2, 0);
const a = my - b * mx;
drawLine(x(0), y(a), x(150000), y(a + b * 150000), '#16222e', 2, '7 5');

The example uses four tiny helpers that create SVG shapes. Put them above the code and it runs as is.

const svg = document.querySelector('svg');
function make(tag, attrs) {
  const n = document.createElementNS('http://www.w3.org/2000/svg', tag);
  for (const k in attrs) n.setAttribute(k, attrs[k]);
  svg.appendChild(n);
  return n;
}
function drawRect(x, y, width, height, fill) { return make('rect', { x, y, width, height, fill }); }
function drawCircle(cx, cy, r, fill) { return make('circle', { cx, cy, r, fill }); }
function drawLine(x1, y1, x2, y2, stroke, width, dash) {
  return make('line', { x1, y1, x2, y2, stroke, 'stroke-width': width || 1, 'stroke-dasharray': dash || 'none' });
}
function drawText(x, y, str, anchor) {
  const t = make('text', { x, y, 'text-anchor': anchor || 'start' });
  t.textContent = str;
  return t;
}

How to add it to your website

  1. Download the file. Click Download HTML file above. You get one file called scatter-plot.html with everything inside.
  2. Change the data. Open the file in any code editor and find the DATA list near the bottom. Replace the names and numbers with your own.
  3. Change the colors and text. Colors are at the top of the style tag. The title, subtitle and main point are plain HTML near the top of the body.
  4. Put it online. Upload the file to any host, such as GitHub Pages, Netlify or your own server, or paste it into your site. There is no build step.

Questions people ask

What is a scatter plot used for?

It shows whether two numbers are related. If the dots form a clear slope, one number tends to change when the other does.

How do I add a trend line to a scatter plot?

Work out a least squares line: find the average of each number, then the slope that best fits the dots. This template shows the exact code in about five lines.

Does a scatter plot show cause and effect?

No. It shows that two numbers move together, not that one causes the other. Something else may drive both.

What if my dots overlap too much?

Make them smaller and see through, or switch to a hexbin plot, which counts dots into hexagons.

More relationships