Published
Edited
Nov 19, 2021
1 fork
Insert cell
# Scatter Plot
Insert cell
url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRgABhzZkXwZihskMCzNWKcL5lqRwyHRF-bygd7uxZ1rq7DbgcZXpSLClyh9FPJlw_JroNhDK73QUq-/pub?gid=0&single=true&output=tsv"
Insert cell
dataset = d3.tsv(url, d3.autoType)
Insert cell
{
const width = 400;
const height = 200;
const svg = d3.create('svg').attr('width', width).attr('height', height);

const margin = ({ top: 10, right: 10, bottom: 20, left: 30 });

const xScale = d3
.scaleLinear()
.domain([0, d3.max(dataset, (d) => d.x)])
.range([margin.left, width - margin.right]);

const yScale = d3
.scaleLinear()
.domain([0, d3.max(dataset, (d) => d.y)])
.range([height - margin.bottom, margin.top]);

const xAxis = d3.axisBottom()
.scale(xScale)
.ticks(5)
const yAxis = d3.axisLeft()
.scale(yScale)
.ticks(5);
svg.append("g")
.attr('transform', `translate(0, ${height - margin.bottom})`)
.call(xAxis);
svg.append("g")
.attr('transform', `translate(${margin.left}, 0)`)
.call(yAxis);

svg
.selectAll("circle")
.data(dataset)
.join("circle")
.attr("cx", (d) => xScale(d.x))
.attr("cy", (d) => yScale(d.y))
.attr("r", 2);

return svg.node();
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more