Public
Edited
Mar 13, 2023
Insert cell
Insert cell
// Define the data
species = ['Blue Shark', 'Blacktip Shark', 'Smooth Dogfish', 'Blackfin Tuna', 'Yellowfin Tuna', 'Bluefin Tuna', 'White Marlin', 'Blue Marlin', 'Sailfish', 'Skipjack Tuna', 'Dolphinfish']

Insert cell
weight = [[75, 400], [35, 50], [20, 30], [10, 35], [35, 300], [450, 550], [50, 150], [250, 750], [100, 250], [15, 45], [25, 50]];

Insert cell

// Create the SVG element
Plot.plot({
marks: [
Plot.dot(species, {
x: weight,
y: "economy (mpg)",
fill: "economy (mpg)",
r: "power (hp)",
symbol: "triangle"
}),
Plot.ruleY([0])
],
x: { label: "Horsepower" },
y: { label: "Gas mileage (mpg)" },
color: { scheme: "magma", legend: true, label: "mpg" },
grid: true
})

Insert cell

// Define the scales
xScale = d3.scaleBand()
.domain(species)
.range([50, 750])
.paddingInner(0.1);


Insert cell
yScale = d3.scaleLinear()
.domain([0, d3.max(weight, function(d) { return d[1]; })])
.range([550, 50]);

Insert cell

// Create the bars
svg.selectAll('rect')
.data(weight)
.enter()
.append('rect')
.attr('x', function(d, i) { return xScale(species[i]); })
.attr('y', function(d) { return yScale(d[1]); })
.attr('width', xScale.bandwidth())
.attr('height', function(d) { return yScale(d[0]) - yScale(d[1]); })
.attr('fill', 'steelblue');

Insert cell

// Add the axes
svg.append('g')
.attr('transform', 'translate(50, 550)')
.call(d3.axisBottom(xScale));

Insert cell

svg.append('g')
.attr('transform', 'translate(50, 50)')
.call(d3.axisLeft(yScale));

Insert cell
// Add the chart title
svg.append('text')
.attr('x', 400)
.attr('y', 30)
.attr('text-anchor', 'middle')
.text('Fish Weight Chart');
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more