Utilizing .map() and .map_dataframe() for Advanced Visualization
The Key parameters to represent various plot points and provide various kinds of visualizations are:
- Figure: Depicts an entire figure on which plots are produced.
- Axes: Each axis represents a distinct plot or subplot in the illustration.
- Plot: Represents the data being represented and how it is related to the plot’s visual attributes.
- FacetGrid: A grid of charts used to visualize complicated datasets.
Using .map() to Customize Plot Elements
Let’s demonstrate the example for advanced visualizations with seaborn’s in-built tips dataset and understand how to use Seaborn’s FacetGrid
along with .map()
to customize plot elements.
In this example, FacetGrid
is specifying that we want separate columns for different times of day (lunch and dinner), based on the “time” column in the tips
DataFrame and following are the customizations made:
- Plotting KDE to plot kernel density estimates.
- Setting
shade=True
to shade the area under the KDE curve. - Changed the color to “orange” and adjusted the bandwidth (
bw_adjust
) for the KDE plot to control its smoothness.
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="time")
g.map(sns.kdeplot, "total_bill", shade=True, color="orange", bw_adjust=0.5)
g.set_axis_labels("Total Bill", "Density")
g.set_titles("{col_name} Time")
plt.show()
Output:
Using .map_dataframe() with Custom Function
In the example code below, a custom plotting functionn scatterplot is passed with the data argument as the first input to accept the DataFrame directly. The color, marker, and size options are stated for better visualization.
Also, We deleted the explicit ordering of the DataFrame within the scatterplot function and replaced it with data[“total_bill”] and data[“tip”] to access the columns directly. In the .map_dataframe() function, we passed the color, marker, and size arguments to the scatterplot function.
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
# Define a custom plotting function with specific arguments
def scatterplot(data,color, marker='o', size=50):
plt.scatter(data["total_bill"], data["tip"], color=color, marker=marker, s=size)
g = sns.FacetGrid(tips, col="time") # Plot using .map_dataframe()
g.map_dataframe(scatterplot, color="skyblue", marker='o', size=30)
g.set_axis_labels("Total Bill", "Tip")
plt.show()
Output:
Combining .map() and .map_dataframe()
In this example, both .map() and .map_dataframe() are utilized. .map_dataframe() applies a histogram plot to the “age” data for each combination of class and sex, while .map() adds horizontal dashed lines to mark the baseline of each subplot
import seaborn as sns
import matplotlib.pyplot as plt
titanic = sns.load_dataset("titanic")
g = sns.FacetGrid(titanic, col="class", row="sex")
# Use .map_dataframe() with custom function
g.map_dataframe(sns.histplot, x="age", bins=10, kde=True, color="green").set_titles("{row_name} - {col_name}")\
# Use .map() to add titles
g.map(plt.axhline, y=0, color="k", linestyle="--")
Output:
Seaborn’s Object Interface : map() and map_dataframe()
Seaborn, a powerful data visualization library built on top of Matplotlib, offers a convenient Object Interface for creating stunning visualizations with ease. Using .map()
and .map_dataframe()
with Seaborn’s object-oriented interface allows for applying custom functions to plot data.
In this article, we will implement these methods and explore how they can be leveraged to enhance data visualization capabilities.
Seaborn’s Object Interface : map() and map_dataframe()
- Understanding .map() and .map_dataframe()
- Utilizing .map() and .map_dataframe() for Advanced Visualization
- Using .map() to Customize Plot Elements
- Using .map_dataframe() with Custom Function
- Combining .map() and .map_dataframe()
Contact Us