The world’s leading publication for data science, AI, and ML professionals.

Dressing Up Your Data Visualization

How to Turn a Basic Plot Into an Awesome Plot in the Wolfram Language

Learning Wolfram: Dressing Up Your Data Visualization

How to Turn a Basic Plot Into an Awesome Plot

(image by the author using photos by Yasong Zhou and NOAA on Unsplash)
(image by the author using photos by Yasong Zhou and NOAA on Unsplash)

Data visualizations can be a tricky thing. You want your data to shine, but most programming languages (including the Wolfram Language) provide a very basic look to their default visualizations. The process of dressing up a visualization to maximize its appeal is usually one of trial and error. This story explores how to make an appealing visualization using a few tips and tricks to make things easy.

Let’s start off with the simplest plot of them all, a simple sin(x) plot where x spans the range from zero to 2π:

Plot[ Sin[x], {x,0,Pi} ]
(image by author)
(image by author)

Clearly this is a useful and effective rendering of a sine plot, but it is a bit bare. Adding a frame, labels, and filling provide context to the visualization and helps the viewer with interpreting what is important:

Plot[Sin[x], {x, 0, 2 Pi}, 
 Frame -> True, Filling -> Axis, 
 PlotLabel -> "f(x) = sin(x)", FrameLabel -> {"x", "sin(x)"}]
(image by author)
(image by author)

Finally adding gridlines and a change of colors give the plot a custom look and make it easy to embed in online dashboards with a given style:

Plot[Sin[x], {x, 0, 2 Pi}, 
 Frame -> True, Filling -> Axis,
 PlotLabel -> "f(x) = sin(x)", FrameLabel -> {"x", "sin(x)"},
 GridLines -> Automatic, Background -> Black, PlotStyle -> Orange]
(image by author)
(image by author)

To make this even easier, the Wolfram Language comes preloaded with eight themes. Each theme has a unique set of appearance settings to create common styles of plots:

Let’s take this a few more steps further and work with an actual dataset. I am using the Wolfram Data Repository to select one. This repository has over 800 datasets that have been contributed by Wolfram and its user community. I picked a very nice dataset called "Hadley Center Central England Temperature (HadCET)":

Hadley Center Central England Temperature (HadCET) Dataset

You can get this dataset by simply referring to its name. To reduce jitteriness in the plot, I run the data through a moving average computation:

data = ResourceData["Hadley Center Central England Temperature (HadCET) Dataset"];
data = MovingAverage[data, Quantity[5, "Years"]]

The result is a TimeSeries object, which can be used directly with visualization functions such as DateListPlot:

(image by author)
(image by author)

As before with Plot, the basic DateListPlot is very simple:

DateListPlot[data]
(image by author)
(image by author)

We can add the same stylings as we did before with the sine plot, but there are two more interesting options called Prolog and Epilog. The Prolog option lets you insert graphics primitives (lines, polygons, images) after the axes and frames are rendered but before the data is drawn. The Epilog option lets you draw final graphics primitives that sit on top of everything else including the data layer.

DateListPlot[data, 
 PlotStyle -> {White, AbsoluteThickness[1]}, 
 Prolog -> Inset[image1, Center, Center, Scaled[{1.2, 1.2}]],
 Epilog -> Inset[image2, Scaled[{1, 0}], {Right, Bottom}, Scaled[{0.2, 0.2}]],
 Filling -> Axis,
 FillingStyle -> Opacity[.3],
 PlotLabel -> Style["Hadley Center Dataset", Bold, 24, FontFamily -> "Copperplate Gothic"],
 FrameLabel -> {"Year", "Temperature"},
 PlotRangePadding -> None
]

Here image1 and image2 are:

(image by the author using photos by Yasong Zhou and NOAA on Unsplash)
(image by the author using photos by Yasong Zhou and NOAA on Unsplash)

The resulting plot shows the various layers:

(image by the author using photos by Yasong Zhou and NOAA on Unsplash)
(image by the author using photos by Yasong Zhou and NOAA on Unsplash)

It’s that easy! For more details on all the Wolfram Language data visualization functions, check out this guide page in the reference documentation.


Related Articles