Published
Edited
Feb 14, 2022
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
Insert cell
dfTemp = FileAttachment("temp_anomalies_data.csv").csv({
typed: true
})
Insert cell
Inputs.table(dfTemp)
Insert cell
Plot.plot({
width: width, // Try to fit the width to the web browser width.
marks: [
Plot.line(dfTemp,
{
x: "Year",
y: "Value"
})
]})
Insert cell
Insert cell
dfSeattleWeather = FileAttachment("seattle_weather.csv").csv({typed: true})
Insert cell
dfCars = FileAttachment("cars.json").json({typed: true})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Inputs.table(dfCars)
Insert cell
Plot.plot({
grid: true,
color: {
type: "categorical",
scheme: "Oranges"
},
marks: [
Plot.dot(dfCars, {
x: "Horsepower",
y: "Miles_per_Gallon",
filter: d => d["Name"].includes("amc"), // We only want AMC cars!
fill: "salmon"
}),

Plot.dot(dfCars, {
x: "Horsepower",
y: "Miles_per_Gallon",
filter: d => d["Name"].includes("chevrolet"),
fill: "steelblue"
})
]
})
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
grid: true,
color: {
scheme: "Turbo"
},
marks: [
Plot.dot(dfCars, {
x: "Horsepower",
y: "Miles_per_Gallon",
sort: d => d["Name"].includes("chevrolet"), // Comment this out and see
filter: d => d["Name"].includes("amc") || d["Name"].includes("chevrolet"),
fill: "Name"
}),
Plot.ruleY([20], {strokeOpacity: 0.2}),
Plot.ruleX([90], {strokeOpacity: 0.2})
]
})
Insert cell
Insert cell
Plot.plot({
y: {
grid: true
},
marks: [
Plot.barX(dfCars,
Plot.groupY({x: "count"},
{
y: d => d["Name"].split(" ")[0]
}))
]
})
Insert cell
Insert cell
Insert cell
function extractName(text) {
return text["Name"].split(" ")[0]
}
Insert cell
Plot.plot({
y: {
grid: true
},
marks: [
Plot.barX(dfCars,
Plot.groupY({x: "count"},
{
y: d => extractName(d),
fill: d => extractName(d),
filter: d => {
let name = extractName(d)
let wanted = ["amc", "chevrolet", "ford"]
return wanted.includes(name)
}
}))
]
})
Insert cell
Insert cell
Plot.plot({
color: {
scheme: "category10", // What color schemes are there?
legend: true // Is there a difference if we remove it?
},
marks: [
Plot.dot(dfCars, {
x: "Horsepower",
y: "Origin",
fill: "Origin"
})]
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
grid: true,
caption: "Horsepower and Origin of Cars",
color: {
scheme: "category10", // What color schemes are there?
legend: true // Is there a difference if we remove it?
},
marks: [
Plot.dot(dfCars, {
x: "Horsepower",
y: "Origin",
fill: "Origin"
})]
})
Insert cell
Insert cell
Insert cell
Insert cell
plot = Plot.plot({
grid: true,
color: {
scheme: "category10", // What color schemes are there?
legend: true // Is there a difference if we remove it?
},
marks: [
Plot.dot(dfCars, {
x: "Horsepower",
y: "Origin",
fill: "Origin"
})]
})
Insert cell
<div>
${
plot
}
</div>
Insert cell
Insert cell
<div>
<h2 style="text-align:left; font-family:sans-serif">Horsepower of Cars by Origin</h1>
${ plot }
Insert cell

</div>
Insert cell
Insert cell
plot1 = Plot.plot({
grid: true,
color: {
type: "categorical",
scheme: "Oranges"
},
width: 400,
height: 200,
marks: [
Plot.dot(dfCars, {
x: "Horsepower",
y: "Miles_per_Gallon",
filter: d => d["Name"].includes("amc"), // We only want AMC cars!
fill: "salmon"
}),

Plot.dot(dfCars, {
x: "Horsepower",
y: "Miles_per_Gallon",
filter: d => d["Name"].includes("chevrolet"),
fill: "steelblue"
})
]
})
Insert cell
plot2 = Plot.plot({
grid: true,
color: {
type: "categorical",
scheme: "Oranges"
},
width: 400,
height: 200,
marks: [
Plot.dot(dfCars, {
x: "Horsepower",
y: "Displacement",
filter: d => d["Name"].includes("amc"), // We only want AMC cars!
fill: "salmon"
}),

Plot.dot(dfCars, {
x: "Horsepower",
y: "Displacement",
filter: d => d["Name"].includes("chevrolet"),
fill: "steelblue"
})
]
})
Insert cell
Insert cell
<style>
* {
box-sizing: border-box;
}
.row {
display: flex;
}

/* Create two equal columns that sits next to each other */
.column {
flex: 50%;
padding: 5px;
}
</style>

<html>
<h3 style="font-family:sans-serif;text-align:left">Investigating Horsepower, MPG and Displacement</h1>
<div class="row">
<div class="column">
${plot1}
</div>
<div class="column">
${plot2}
</div>
</div>
</html>
Insert cell
Insert cell
Insert cell
viewof myText = Inputs.text({label: "Message"}) // Try changing input.
Insert cell
Insert cell
myText
Insert cell
There are all sorts of inputs from buttons to sliders to dates. You can check out the entire gamut of them over [here](https://observablehq.com/@observablehq/inputs).
Insert cell
Insert cell
Insert cell
Plot.plot({
grid: true,
width: width, // This is how you extend the graph sideways to fit the web page
marks: [
Plot.line(dfSeattleWeather, {x: "date", y: "temp_max", stroke: "salmon"}),
Plot.line(dfSeattleWeather, {x: "date", y: "temp_min", stroke: "steelblue"}),
]
})
Insert cell
Insert cell
Plot.plot({
grid: true,
width: width,
marks: [
Plot.areaY(dfSeattleWeather, {x: "date", y1: "temp_min", y2: "temp_max", fill: "olive", curve: "step"})
]
})
Insert cell
Insert cell
viewof kValue = Inputs.range([0, 80], {label: "k"})
Insert cell
Plot.plot({
grid: true,
width: width, // This is how you extend the graph sideways to fit the web page
marks: [
Plot.line(dfSeattleWeather,
Plot.windowY({x: "date", y: "temp_max", k: kValue, stroke: "salmon"})),
Plot.line(dfSeattleWeather,
Plot.windowY({x: "date", y: "temp_min", k: kValue, stroke: "steelblue"})),
Plot.areaY(dfSeattleWeather,
Plot.windowY({x: "date", y1: "temp_min", y2: "temp_max", k: kValue, fill: "lightgray", curve: "step"}))

]
})
Insert cell
Insert cell
Plot.plot({
grid: true,
width: width, // This is how you extend the graph sideways to fit the web page
marks: [
Plot.line(dfSeattleWeather,
Plot.windowY({x: "date", y: "temp_max", k: kValue, stroke: "salmon"})),
Plot.line(dfSeattleWeather,
Plot.windowY({x: "date", y: "temp_min", k: kValue, stroke: "steelblue"})),
Plot.areaY(dfSeattleWeather,
Plot.windowY({x: "date", y1: "temp_min", y2: "temp_max", k: kValue, fillOpacity: 0.2, fill: "lightgray", curve: "step"})),
Plot.line(dfSeattleWeather,
Plot.windowY({x: "date",
y: d => {
return (d["temp_max"] + d["temp_min"]) / 2
},
k: kValue}))

]
})
Insert cell
Insert cell
Insert cell
Inputs.table(dfSeattleWeather)
Insert cell
Insert cell
Insert cell
viewof myColor = Inputs.select(["lightsteelblue", "pink", "lightgray", "crimson"], {label: "Select one"})
Insert cell
Plot.plot({
grid: true,
width: width, // This is how you extend the graph sideways to fit the web page
marks: [
Plot.line(dfSeattleWeather,
Plot.windowY({x: "date", y: "temp_max", k: kValue, stroke: "salmon"})),
Plot.line(dfSeattleWeather,
Plot.windowY({x: "date", y: "temp_min", k: kValue, stroke: "steelblue"})),
Plot.areaY(dfSeattleWeather,
Plot.windowY({x: "date", y1: "temp_min", y2: "temp_max", k: kValue, fill: myColor, curve: "step"}))

]
})
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
dfHollywood = FileAttachment("hollywood.csv").csv({typed:true})

Insert cell
Inputs.table(dfHollywood)
Insert cell
hollywoodMakers = new Set()
Insert cell
dfHollywood.forEach(movie => {
console.log(movie)
hollywoodMakers.add(movie["Distributor"])
})
Insert cell
hollywoodMakers
Insert cell
Insert cell
Insert cell
viewof selectedDistributor = Inputs.checkbox(hollywoodMakers, {label: ""})
Insert cell
selectedDistributor
Insert cell
Plot.plot({
y: {
grid: true
},
marks: [
Plot.barX(dfHollywood,
Plot.groupY({x: "count"},
{
y: "Distributor",
fill: "Distributor",
filter: d => selectedDistributor.includes(d["Distributor"])
}))
]
})
Insert cell
Insert cell
Plot.plot({
x: { grid: true },
y: { grid: true },
width: width,
marks: [
Plot.dotX(dfHollywood,
Plot.groupY({x: "max"},
{
x:"International Sales (in $)",
y:"Distributor",
filter: d => selectedDistributor.includes(d["Distributor"])
})),
Plot.ruleX([800000000,1800000000], { strokeOpacity: 0.2 })
]
})
Insert cell
Plot.plot({
x: { grid: true },
y: { grid: true },
width: width,
marks: [
Plot.barX(dfHollywood,
Plot.groupY({x: "max"},
{
x:"International Sales (in $)",
y:"Distributor",
filter: d => selectedDistributor.includes(d["Distributor"]),
fill: 'steelblue',
fillOpacity: 0.2
})),
Plot.barX(dfHollywood,
Plot.groupY({x: "min"},
{
x:"International Sales (in $)",
y:"Distributor",
filter: d => selectedDistributor.includes(d["Distributor"]),
fill: 'salmon',
fillOpacity: 0.2
})),
]
})
Insert cell
Insert cell
viewof selectedDistributor2 = Inputs.select(hollywoodMakers, {label: "Select one"})
Insert cell
selectedDistributor2
Insert cell
Plot.plot({
x: { grid: true },
y: { grid: true },
width: width,
marks: [
Plot.dot(dfHollywood,
{
x:"License",
y:"International Sales (in $)",
fill: 'salmon',
fillOpacity: 0.5,
filter: d => selectedDistributor2.includes(d["Distributor"])
}
)
]
})
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