For some people line charts are probably not fancy enough. It is a chart you create the first time you draw any chart in school as a kid. But they are still very helpful during an exploratory data analysis. This post will describe the advantages of an interactive and clean-looking dual axis line chart with Plotly in Python. Several examples of data from two different thermometers will be used to illustrate the usefulness of this type of data visualization.
The Case For Dual Axis Charts
In general, dual axis charts are kind of bewildering. As of now I cannot think of many chart types for which a dual axis view does not generate confusion. Personally, I don’t like to use them for time series in bar charts, because I find it hard to spot correlations. If it comes to more than two dimensions in a chart, humans are maybe able to grasp a third dimension using a color, marker type or 3D visualization, maybe a fourth dimension using the marker size. But two different scales for the same dimension in one chart can be a problem for many viewers.
For me, there is one exception to this: Line charts with two traces displaying a time series of the same or a different quantity. A bunch of use cases from the real world for this type of visualization:
- Finding events in one time series which correlate with pattern changes in another series (i.e., trading volumes and stock prices).
- Understanding how the readings from two sensors in different locations diverge (i.e., volume measurements in two different locations of a residential area).
- Determining the accuracy of two identical sensors where one of them is suspected to have a static error (i.e., humidity).
A Few Examples
In the following example we will compare two correlating time series. The two traces displayed in the upcoming chart are:
- a relative humidity (in percent)
- a temperature (in degrees Celsius … my imperial friends 🙂 )
This type of view allows it to spot correlative events in both series, although both series require different scales, because they are not of the same quantity and unit. In addition, Plotly offers a neat “range selector” (displayed below the actual diagram), which allows dynamically selecting the displayed time range. It makes it much easier to find a certain smaller time range you want to examine in the entirely available time range. The selection buttons in the top left are supportive to select a specific range more quickly without dragging around the range limiters.
This diagram is also useful if you want to compare two quantities of the same unit. In the next example, we are looking at two humidity measurements from thermometers mounted in different locations:
We can make a clear observation: While at the beginning thermometer A shows more humidity than B, over time thermometer B’s humidity readings slightly decrease, whereupon the measurements of thermometer A sharply reduce in value. For me personally, filling the areas below the lines with partly transparent colors makes it simpler to spot this progression.
In a last plot, we want to push it a little further and use SciPy to make a trend more clearly visible. By filtering a time series with a Savitzky-Golay filter (using scipy.signal.savgol_filter) it is possible to smoothen the series and get rid of extreme values making trends more recognizable:
What’s the outcome? Without all the noise we can distinctly spot an upward trend for the temperature measured by thermometer B.
You can find an iPython notebook for this specific chart on GitHub. Refer to the code in the notebook to reproduce above plots on your own. The notebooks describes how to:
- load the sample data
- define a Python function to draw the graphs conveniently
- render several plots from the sample data
The repository’s README file also explains how to quickly fire up a JupyterLab instance that allows hassle-free rendering the notebook.
So often, it doesn’t take an elaborate anomaly detection, heat map or 3D chart, but only a simple, dual axis line chart to extract information from data. Especially Plotly’s interactivity makes analyzing data and presenting results more efficient and fun.
It’s a wrap, until next time!