Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende FacetGrid | Multi-Plot Grids
Deep Dive into the seaborn Visualization

book
FacetGrid

The time to learn the cool plots we were talking about!

A useful approach to exploring medium-dimensional data is by drawing multiple instances of the same plot on different subsets of your dataset.

FacetGrid object takes a DataFrame as input and the names of the variables that will form the grid's row, column, or hue dimensions. The variables should be categorical, and the data at each level of the variable will be used for a facet along that axis.

carousel-imgcarousel-imgcarousel-imgcarousel-imgcarousel-imgcarousel-imgcarousel-img
python
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

df = pd.read_csv('filename.csv')

# Creating the FacetGrid variable
g = sns.FacetGrid(df, col = 'column_name', row = 'row_name')

# The main approach for visualizing data on this grid is with the FacetGrid.map() method
g.map(sns.scatterplot, 'column_name')

plt.show()
Tarea

Swipe to start coding

  1. Set the 'whitegrid' style with the 'cornsilk' axes.facecolor.

  2. Create a FacetGrid variable g:

  • Set the data for the FacetGrid;
  • Set the col parameter equals the 'day';
  • Set the row parameter equals the 'smoker';
  • Set the height parameter equals 3.

Implement the .map() function to build histplots:

  • Create histplots;
  • Set the 'olive' color for the histplots;
  • Add the kde parameter;
  • Disable the fill parameter;
  • Set the binwidth equals 4;
  • Display the plot.

Solución

# Importing libraries needed
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Reading the file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/66ba0c8e-8422-413c-b7e1-74bd24c61656/tips.csv')

# Set the 'whitegrid' style with the 'cornsilk' facecolor
sns.set_style('whitegrid', {'axes.facecolor': 'cornsilk'})
# Create a FacetGrid variable
g = sns.FacetGrid(# Set the data
df,
# Set the col
col = 'day',
# Set the row
row = 'smoker',
# Set the height
height = 3)
# Build histplots using the .map() function
g.map(# Create histplots
sns.histplot,
'total_bill',
# Set the color
color = 'olive',
# Set the kde
kde = True,
# Set the fill
fill = False,
# Set the binwidth
binwidth = 4)

# Display the plot
plt.show()

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 1
# Importing libraries needed
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Reading the file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/66ba0c8e-8422-413c-b7e1-74bd24c61656/tips.csv')

# Set the 'whitegrid' style with the 'cornsilk' facecolor
sns.set_style('___', {'axes.facecolor': '___'})
# Create a FacetGrid variable
g = sns.___(# Set the data
___,
# Set the col
col = '___',
# Set the row
row = '___',
# Set the height
___ = 3)
# Build histplots using the .map() function
g.___(# Create histplots
sns.___,
'total_bill',
# Set the color
___ = '___',
# Set the kde
kde = ___,
# Set the fill
fill = ___,
# Set the binwidth
___ = 4)

# Display the plot
___

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt