Line Plots
Last updated
Last updated
Why lines? - We are more interested in relative change.
Also, the line emphasize trends and connections across the x-valuels. This means that it is inappropriate to use a line plot if we have a nominal variable along the x axis.
The line plot is a fairly common plot type that is used to plot the trend of one numeric variable against values of a second variable. In contrast to a scatterplot, where all data points are plotted, in a line plot, only one point is plotted for every unique x-value or bin of x-values (like a histogram). If there are multiple observations in an x-bin, then the y-value of the point plotted in the line plot will be a summary statistic (like mean or median) of the data in the bin. The plotted points are connected with a line that emphasizes the sequential or connected nature of the x-values.
If the x-variable represents time, then a line plot of the data is frequently known as a time series plot. Often, we have only one observation per time period, like in stock or currency charts. While there is a seaborn function tsplot
that is intended to be used with time series data, it is fairly specialized and (as of this writing's seaborn 0.8) is slated for major changes.
Instead, we will make use of Matplotlib's errorbar
function, performing some processing on the data in order to get it into its necessary form.
If we just blindly stick a dataframe into the function without considering its structure, we might end up with a mess like the above. The function just plots all the data points as a line, connecting values from the first row of the dataframe to the last row. In order to create the line plot as intended, we need to do additional work to summarize the data.
Since the x-variable ('num_var1') is continuous, we first set a number of bins into which the data will be grouped. In addition to the usual edges, the center of each bin is also computed for later plotting. For the points in each bin, we compute the mean and standard error of the mean. Note that the cut
function call is simpler here than in the previous page, since we don't need to compute individual point weights.
An interesting part of the above summarization of the data is that the uncertainty in the mean generally increases with increasing x-values. But for the largest two points, there are no error bars. Looking at the default errorbar
plot (or the scatterplot below), we can see this is due to there only being one point in each of the last two bins.
Instead of computing summary statistics on fixed bins, you can also make computations on a rolling window through use of pandas' rolling
method. Since the rolling window will make computations on sequential rows of the dataframe, we should use sort_values
to put the x-values in ascending order first.
Note that we're also not limited to just one line when plotting. When multiple Matplotlib functions are called one after the other, all of them will be plotted on the same axes. Instead of plotting the mean and error bars, we will plot the three central quartiles, laid on top of the scatterplot.
Another bivariate application of line plots is to plot the distribution of a numeric variable for different levels of a categorical variable. This is another alternative to using violin plots, box plots, and faceted histograms. With the line plot, one line is plotted for each category level, like overlapping the histograms on top of one another. This can be accomplished through multiple errorbar
calls using the methods above, or by performing multiple hist
calls, setting the "histtype = step" parameter so that the bars are depicted as unfilled lines.
Note that I'm performing the multiple hist
calls through the use of FacetGrid, setting the categorical variable on the "hue" parameter rather than the "col" parameter. You'll see more of this parameter of FacetGrid in the next lesson. I've also added an add_legend
method call so that we can identify which level is associated with each curve.
Unfortunately, the "Alpha" curve seems to be pretty lost behind the other three curves since the relatively low number of counts is causing a lot of overlap. Perhaps connecting the centers of the bars with a line, like what was seen in the first errorbar
example, would be better.
Functions you provide to the map
method of FacetGrid objects do not need to be built-ins. Below, I've written a function to perform the summarization operations seen above to plot an errorbar
line for each level of the categorical variable, then fed that function (freq_poly
) to map
.
**kwargs
is used to allow additional keyword arguments to be set for the errorbar
function.
(Documentation: numpy linspace
)