Other Adaptations of Bivariate PLots
Other Adaptations of Bivariate Plots
You also saw one other way of expanding univariate plots into bivariate plots in the previous lesson: substituting count on a bar chart or histogram for the mean, median, or some other statistic of a second variable. This adaptation can also be done for bivariate plots like the heat map, clustered bar chart, and line plot, to allow them to depict multivariate relationships.
If we want to depict the mean of a third variable in a 2-d histogram, we need to change the weights of points in the hist2d
function similar to how we changed the weights in the 1-d histogram.
Considering how few data points there are in the example, and how cleanly the third variable is distributed, the adapted heat map is a bit excessive in terms of work. The low level of point overlap observed earlier means that the scatterplot with color or size encoding was sufficient for depicting the data. You'll be more likely to use the heat map if there is a lot of data to be aggregated.
The code for the 2-d bar chart doesn't actually change much. The actual heatmap
call is still the same, only the aggregation of values changes. Instead of taking size
after the groupby
operation, we compute the mean
across dataframe columns and isolate the column of interest.
Note how the "cbar_kws" provides an additional argument to the colorbar component of the heat map call.
An alternative approach for two categorical variables and one numeric variable is to adapt a clustered bar chart using the barplot
function instead of the countplot
function:
The "hue" parameter can also be used in a similar fashion in the boxplot
, violinplot
, and pointplot
functions to add a categorical third variable to those plots in a clustered fashion. As a special note for pointplot
, the default rendering aligns all levels of the "hue" categorical variable vertically. Use the "dodge" parameter to shift the levels in a clustered fashion:
As a final example, a line plot can be adapted from previous code showing how to create frequency polygons for levels of a categorical variable. In this case as well, we create a custom function to send to a FacetGrid object's map
function that computes the means in each bin, then plots them as lines via errorbar
.
Last updated