Plotting with the FacetGrid() method

Let's start by looking at how to plot multi-dimensional plots with the FacetGrid method, a shown in the following code:

g = sns.FacetGrid(df, col="Sex", hue='Survived')
g.map(plt.hist, "Age");
g.add_legend();

The output of the preceding code should look like the following screenshot:

Here, we have used the FacetGrid method to draw two side-by-side histograms for male and female passengers. This side-by-side display helps us to compare the survival rates of male and female passengers by their age. To draw this, we first created a grid using the FacetGrid method. We then passed in our dataset's DataFrame columns as Sex and hue as Survived. Hue stands for the depth of the plot. This then created the grid with two separate plots for male and female passengers. We then called the map method on the grid and passed the plt.hist and Age parameters, which drew our two histograms. Finally, we added the legend with the add_legend method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.216.51