5.interactive Visualization
5.interactive Visualization
Word Cloud
Wiki
Word Cloud
Word clouds are great for visualizing unstructured text data and
getting insights on trends and patterns.
The bigger and bolder the word appears, the more often it’s mentioned
within a given text and the more important it is.
Words are usually single words, and the importance of each is
shown with font size or color.
Word Cloud Use
https://pypi.org/project/wordcloud/
Word Cloud using a Mask
https://pypi.org/project/wordcloud/
Word Cloud: Pros and Cons
Pros:
Wordclouds are aesthetically pleasing, and people are used to it.
So helps readers understand them quickly.
Cons:
longer words will take more space on the figure by construction which
distorts reality.
It is impossible to translate a font size to an accurate value.
Data Visualization
INTERACTION
Interactive Data Visualization
Pointing
Point Selection:
Mousehover/Mouse click
Touch/Tap
Select Nearby Element (ex: bubble cursor)
Region Selection: (Panning and Zooming)
Select Nearby Element
Rubber-band or Lasso
Area Cursors (“Brushes”)
Basic Interaction
Pointing
Point Selection:
Mousehover/Mouse click
Touch/Tap
Select Nearby Element
Region Selection:
Select Rectangular Area
Rubber-band or Lasso
Brushes
Plotly Lasso Support
Plotly Lasso Support
Other Interactions
Fisheye Lens:
fisheye distortion magnifies the local
region around the mouse, while leaving
the larger graph unaffected for
context.
Cartesian Distortion:
straight lines parallel to the x or y axis
remain straight even after distortion. This
means you can use standard axes in
conjunction with fisheye distortion in
scatterplots:
Examples
https://observablehq.com/d/e328ba0126cac5be
Data Storytelling
Data Storytelling
“Numbers have an important story to tell. They rely on you to give them
a clear and convincing voice”.
Stephen Few
Data Visualization
https://clauswilke.com/dataviz/telling-a-story.htm
Why “Story Telling”?
Example:
Lead: How Machines Destroy (And Create!) Jobs, In 4 Graphs
https://www.npr.org/sections/money/2015/05/07/404991483/how-
machines-destroy-and-create-jobs-in-4-graphs?live=1
How Machines Destroy (And
Create!) Jobs, In 4 Graphs
In the first part of the 20th century,
agricultural technology — the tractor,
chemical fertilizers — meant a single
farmer could suddenly grow much more
food. So we didn't need as many
farmers. Technology destroyed a huge
number of farming jobs.
But that technology also made food
cheaper, so people had more money to
spend on other things, like TVs and radios
and newly invented appliances. Factory
jobs boomed. Other sectors grew as well;
the midcentury economy had lots of
midskill white-collar jobs like secretary
and bookkeeper.
How Machines Destroy (And
Create!) Jobs, In 4 Graphs
The next wave of major
technological change came in the
latter part of the 20th century. Robots
in factories and computers in offices
automated away many of the jobs
that earlier technologies had
created in the earlier part of the
century.
How Machines Destroy (And
Create!) Jobs, In 4 Graphs
In the past several decades, as
factories became more
automated, manufactured goods
got cheaper. That left many
Americans with more money to
spend on services — eating out
more, spending more on health
care and education. These sectors
are still labor-intensive and have
continued to add jobs.
But that may change as computers
get smarter and more ubiquitous.
How Machines Destroy (And
Create!) Jobs, In 4 Graphs
Finally, there are a few sectors where
jobs have been disappearing for well
over a century now. At no point in the
20th century was it a good time to be a
blacksmith.
https://www.npr.org/sections/money/2015/05/07/404
991483/how-machines-destroy-and-create-jobs-in-4-
graphs?live=1
“How Unions Are Winning” in 4 Charts
https://www.vox.com/recode/2022/8/30/23326
654/2022-union-charts-elections-wins-strikes
“How Unions Are Winning” in 4 Charts
https://www.forbes.com/sites/brentdykes/2016/03/31/data-storytelling-the-essential-data-science-skill-everyone-needs
Data Storytelling: Explain
https://www.forbes.com/sites/brentdykes/2016/03/31/data-storytelling-the-essential-data-science-skill-everyone-needs
Data Storytelling: Enlighten
https://www.forbes.com/sites/brentdykes/2016/03/31/data-storytelling-the-essential-data-science-skill-everyone-needs
Data Storytelling: Engage
https://www.forbes.com/sites/brentdykes/2016/03/31/data-storytelling-the-essential-data-science-skill-everyone-needs
Data Storytelling
When you combine the right visuals and narrative with the right
data, you have a data story that can influence and drive change.
https://www.forbes.com/sites/brentdykes/2016/03/31/data-storytelling-the-essential-data-science-skill-everyone-needs
Hans Rosling
1948 –2017
A Swedish physician, academic, statistician, and public speaker.
Well known as a
Global health expert; data visionary
“In Hans Rosling’s hands, data sings”.
Co-founder and chairman of the Gapminder Foundation, which
developed the Trendalyzer software system.
He held presentations around the world, including several TED Talks in
which he promoted the use of data to explore development issues.
https://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_
ever_seen
Dashboards
What is Dashboard?
[Wiki]
What is a Dashboard?
Source: http://www.dashboardinsight.com/articles/digital-dashboards/fundamentals
Dashboard Example
https://www.stateofobesity.org/adult-obesity/
Designing Dashboard
Designing Dashboard
5. Be consistent
Repetition is OK.
6. Highlight the most relevant information
7. Be clear
Acronyms are bad. Legends are good.
8. Start from zero
Chart axes must be used consciously. Sometimes we convey the wrong
message just by forgetting to pay attention to details.
Designing Dashboard
13.Show variations
Don’t let users do the math.
14.Leave the noise off
Don’t suggest relations that don’t exist.
15.Pick the right chart
Each piece of information must be displayed using the right chart in your
dashboard, so choose it wisely.
Scientific Visualization
Purpose
Wiki
Example: Satellite data viz of Eta’s 2nd Florida Landfall
Example: Satellite images show toxic air sweeping across
North India as farm fires rage
https://earthobservatory.nasa.gov/images/152023/modest-ozone-hole-in-2023
Example: Medical Volume Viz
Source: https://www.petscaninmumbai.com/blog/know-about-mri-and-pet-ct-scan/
Example: Medical Volume Viz
Ball-stick visualizations (on the left), Ribbon and Sheet (on the right) of protein DNAPolymerase:
the twists and folds of the overall structure on which hangs the active site that determines their functionality
Example: Molecule Visualization
1D Space:
ex: 𝑓(𝑥) = sin(𝑥) + sin( 𝑥)
Line plot
Visualizing 2D Scalar Field
2 D Space:
ex:
Temperature over earth surface
𝑓(𝑥, 𝑦) = 𝑒 ( )
https://plotly.com/python/3d-surface-plots/
Contouring
Contouring
and Color
Banding
Contouring
Contouring and
Colormapping:
7 contour lines
Constructing Contour
V=0.48
Finding line
segments
within cells
Contouring
3 D Space:
ex:
Temperature inside the earth
most popular scalar visualization techniques
Color mapping
Contouring
Coloring
Contours in 3-D
Ref:
https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-39-volume-rendering-techniques
(recap)Scalar Field Visualizaiton
Coloring
V=0.48
Finding line
segments
within grid
cells
Connection problems
Grid Method of 2D-Contouring
Ref: https://en.wikipedia.org/wiki/Marching_squares
Contouring
Ref: https://en.wikipedia.org/wiki/Marching_cubes
Vector Field Visualization
Vector Field
Gaseous flow:
development of cars, aircraft, spacecraft
design of machines - turbines, combustion engines
Liquid Flow:
naval applications - ship design
civil engineering - harbor design, coastal protection
Chemistry - fluid flow in reactor tanks
Medicine - blood vessels, SPECT, fMRI
Visualizing Vector Fields
An individual field line shows the direction of the vector field but not
the magnitude.
a selection of field lines can be drawn such that the density of field
lines (number of field lines per unit perpendicular area) at any
location is proportional to the magnitude of the vector field at that
point.
Areas in which neighboring field lines are converging (getting closer
together) indicates that the field is getting stronger in that direction.
Streamlines, streaklines, and
Pathlines and Timelines
3 Different Field lines in a fluid flow.
They differ only when the flow changes with time, that is, when the flow is
not steady.
Streamlines are a family of curves that are instantaneously tangent to
the velocity vector of the flow. These show the direction in which a
massless fluid element will travel at any point in time.
Streaklines are the loci of points of all the fluid particles that have
passed continuously through a particular spatial point in the past.
Dye steadily injected into the fluid at a fixed point extends along a
streakline.
Pathlines are the trajectories that individual fluid particles follow. These
can be thought of as "recording" the path of a fluid element in the flow
over a certain period. The direction the path takes will be determined
by the streamlines of the fluid at each moment in time.
https://en.wikipedia.org/wiki/Streamlines,_streaklines,_and_pathlines
Streamlines
https://www.cradle-cfd.com/media/column/a69
Streakline
https://www.cradle-cfd.com/media/column/a69
Pathline
https://www.cradle-cfd.com/media/column/a69
Plotly Support
Streamlines
Plotly Support
Streamtubes
https://plotly.com/javascript/streamtube-plot/
Paraview
https://www.paraview.org/
https://observablehq.com/@spattana/webgl-volume-rendering
D3 for Interactive Viz
D3: Data-Driven Documents
Shapes
Scales and Axes
Selection and Transition:
uses pre-built functions to select elements, create SVG
objects, style them, or add transitions, dynamic effects,
or tooltips. These objects can also be styled using CSS.
Allows the developer to add transition to smoothly interpolate values for
attributes and styles over a certain time.
Interaction
Layout
Geographic Maps
Data Visualization Framework
Data
Mapping
Collection,
Algorithm
Preparation
Lines Curves
Symbols
Graphics and Interaction Layer
HTML
Graphics
SVG: Scalable Vector Graphics
div: A divider, denoted using the <div> tag
context2D: Raster graphics
Interaction
Mouse
Touch
HTML Elements Attribute
} class: .className
Selects all elements belonging to the
General: same id
selector { element, element div, p
property: value; Selects all <div> elements and all
<p> elements
}
element element div p
Selects all <p> elements inside
<div> elements
Sample CSS embedded in a HTML page
<style type="text/css">
p{
font-size: 24px;
font-weight: bold;
background-color: red;
color: white;
}
</style>
<link rel="stylesheet" href="style.css">
HTML page with embedded CSS rule and with reference to an external style sheet.
Sample Rendering 1
Shapes are elements of the page. Each has its individual geometric
attributes.
rect,
Circle, ellipse,
line,
Polyline, polygon,
path.
Ref: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Basic_Shapes
SVG: Basic Shapes
rect:
6 geometric attributes that control the position and shape of the
rectangle on screen
ex: <rect x="10" y="10" width="30" height="30"/>
<rect x="60" y="10" rx="10" ry="10" width="30" height="30"/>
x, y: Coordinates of the top left corner. Default: 0
width, height
rx, ry: Corner radii. Gives a rounded corner.
circle:
3 geometric attributes that control the position and size of the circle on
screen
ex: <circle cx= "30" cy= "30" r= "25"/>
cx, cy: Coordinates of the center.
r: Radius
ellipse:
4 geometric attributes that control the position and size of the ellipse on
screen
ex: <ellipse cx= "30" cy= "30" rx= “25“ ry = “15”/>
rx, ry: the X and Y radii of the ellipse.
Style attributes are similar to that of rect.
SVG: Basic Shapes
line:
4 geometric attributes that control the position of the end points.
ex: <line x1="10" x2="50" y1="110" y2="150"/>
x1, y1, x2, y2 : Coordinates of the end points.
SVG: Basic Shapes
path: Path is the most powerful shapes of SVG and is probably the
most used shape in d3.
1 geometric attribute: d
A list of points and other information about how to draw the path.
ex: <path
fill="none"
stroke="red”
d="M 10,30
A 20 20 0 0 1 50,30
A 20 20 0 0 1 90,30
Q 90,60 50,90
Q 10,60 10,30 z“
stroke-width=“2"/>
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
SVG: path
Arcs
A rx ry x-axis-rotation large-arc-flag sweep-flag x y
or a rx ry x-axis-rotation large-arc-flag sweep-flag dx dy
Z closed path.
SVG: Text
ex:
<text x="20" y="40" style="font-family: Arial; font-size : 34; stroke : #000000; fill : #00ff00; ">
Styled SVG text
</text>
SVG elements (summarize)
Style: font-size
stroke font-weight
stroke-width font-family
fill text-anchor
D3 API
Mouse Event:
selection.on (“mouseover”, mouseOverHandler)
selection.on(“mouseout”, mouseOutHander)
selection.on(“click”,mouseClickHandler)
Touch Event:
selection.on (“touchstart”, touchStartHandler)
selection.on(“touchend”, touchEndHander)
ex:
circles.on("mouseover",handleMouseOver)
.on("mouseout",handleMouseOut);
Ref:
https://github.com/d3/d3-selection#handling-events
Creating SVG elements in
Javascript using D3
svg.append("rect")
.attr("x", i*60)
.attr("height", data[i])
.attr("width",50)
.attr("rx", 10)
.style("fill","green")
.on("mouseover", function(){
d3.select(this).style("fill", "red");
})
.on("mouseout", function(d){
d3.select(this).style("fill", "green");
});
D3 Selection
d3.selectAll("rect") svg.selectAll("rect")
.data(dataArray) .data([50, 100, 150, 200, 250, 300, 350,400, 450, 500])
.join("rect") .join("rect")
.attr(…) .attr("x", function(d, i) { return i*60})
.style(…)
.attr("height", function(d) { return d})
.attr("width",50)
.attr("rx", 10)
.style("fill","green")
.on("mouseover", function(){d3.select(this).style("fill", "red");})
.on("mouseout", function(d){d3.select(this).style("fill", "green");});
D3 Selection
Source http://www.jeromecukier.net/blog/2011/08/11/d3-scales-and-color/
D3 Scale Functions
http://d3indepth.com/scales/
D3 Scale Functions
http://d3indepth.com/scales/
Creating SVG elements in
Javascript using D3
let xScale = d3.scaleBand().domain(d3.range(10)).range([0, width]).padding(0.2);
let yScale = d3.scaleLinear().domain([0,d3.max(data)]).range([height, 0])
svg.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d,i)=>xScale(i))
.attr("y", (d,i)=>yScale(d))
.attr("height", (d,i)=>height-yScale(d))
.attr("width",xScale.bandwidth())
.attr("rx", 10)
.style("fill","green")
D3 Scale Functions
Ref:
https://github.com/d3/d3-scale-chromatic
http://d3indepth.com/scales/
Creating SVG elements in
Javascript using D3
let xScale = d3.scaleBand().domain(d3.range(10)).range([0, width]).padding(0.2);
let yScale = d3.scaleLinear().domain([0,d3.max(data)]).range([height, 0]);
let colorScale = d3.scaleOrdinal().range(d3.schemeCategory10);
chartContainer.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d,i)=>xScale(i))
.attr("y", (d,i)=>yScale(d))
.attr("height", (d,i)=>height-yScale(d))
.attr("width",xScale.bandwidth())
.attr("rx", 10)
.style("fill",(d,i)=>colorScale(i))
Axes
https://github.com/d3/d3-axis
Axes
https://github.com/d3/d3-axis
SVG g element
https://www.pshrmn.com/tutorials/d3/axes/
D3 Scale Functions
http://d3indepth.com/scales/
D3 Scale Functions
http://d3indepth.com/scales/
D3 Scale Functions
http://d3indepth.com/scales/
D3 Scale Functions
http://d3indepth.com/scales/
D3 Scale Functions
http://d3indepth.com/scales/
D3 Scale Functions
Ref:
https://github.com/d3/d3-scale-chromatic
http://d3indepth.com/scales/
D3 Scale Functions
http://d3indepth.com/scales/
scaleBand() vs scalePoint()
scaleBand
scalePoint
https://github.com/d3/d3-scale#ordinal-scales
D3 Scale Functions
http://d3indepth.com/scales/
D3 Scale Functions
Scales with continuous input and discrete output (continued)
d3.scaleQuantile:
Quantile scales map a sampled input domain to a discrete range.
The domain is considered continuous and thus the scale will accept any
reasonable input value; however, the domain is specified as a discrete set of
sample values. The number of values in (the cardinality of) the output range
determines the number of quantiles that will be computed from the domain.
To compute the quantiles, the domain is sorted, and treated as a population
of discrete values
ex: var myData = [0, 5, 7, 10, 20, 30, 35, 40, 60, 62, 65, 70, 80, 90, 100];
d3.scaleQuantile()
.domain(myData)
.range(['lightblue', 'orange', 'lightgreen', 'pink']);
http://d3indepth.com/scales/
D3 Scale Functions
http://d3indepth.com/scales/
D3 Shapes: “circle”
selection.append(“path”)
.attr(“d”, …)
D3 helpers
Line generator: d3.line()
.curve(…) : curveLinear, curveCardinal, closed equivalents
D3 path generator
d3.line()
For example:
let lineGenerator = d3.line()
.x(function(d, i) { return d.day; })
// set the x values for the line generator
.y(function(d) { return d.earning; });
// set the y values for the line generator
d3.line()
.y(function(d) { })
return yScale(d.close);
});
let D = [
{date: "2007-04-23",close: 93.24},
{date: "2008-01-02",close:194.84},
{date: "2009-01-01",close:85.35},
{date: "2010-01-01",close:210.73},
{date: "2011-01-03",close:329.57},
{date: "2012-01-03",close:411.23}
]
d3.line()
.y(function(d) { })
return yScale(d.close);
})
.curve(d3.curveCardinal);
Curve Choices:
curveLinear, curveMonotoneX, curveCardinal,
curveCatmullRom, curveMonotone, curveNatural,
curveStep, …
D3 Symbols
built-in symbol types: circle, cross, diamond, square, star, triangle, and wye
all use “path”
ex: let symbolGenerator = d3.symbol().size(100).type(d3.cross)
selection.append(“path”)
.attr(“d”,function(){
return symbolGenerator();
})
https://github.com/d3/d3-shape#symbols
D3 Path generators:
selection.append(“path”)
“path”
.attr(‘d’,…)
.attr('d', areaGenerator)
.style('fill',function(d,i){return colors[i];});
Further readings on Scales
https://github.com/d3/d3-scale
http://d3indepth.com/scales/
https://www.tutorialspoint.com/d3js/d3js_scales_api.htm