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.







python9912345678910111213import pandas as pdimport seaborn as snsfrom matplotlib import pyplot as pltdf = pd.read_csv('filename.csv')# Creating the FacetGrid variableg = sns.FacetGrid(df, col = 'column_name', row = 'row_name')# The main approach for visualizing data on this grid is with the FacetGrid.map() methodg.map(sns.scatterplot, 'column_name')plt.show()
Tarea
Swipe to start coding
-
Set the
'whitegrid'
style with the'cornsilk'
axes.facecolor
. -
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 equals3
.
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
equals4
; - Display the plot.
Solución
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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?
¡Gracias por tus comentarios!
Sección 5. Capítulo 1
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla