How to Use Matplotlib with Seaborn for Data Visualization
You can use
seaborn to create statistical plots easily and customize them with matplotlib functions. First, import both libraries, then create a plot with seaborn and adjust it using matplotlib commands like plt.title() or plt.xlabel().Syntax
To use matplotlib with seaborn, first import both libraries. Use seaborn functions to create plots, then use matplotlib functions to customize them.
import seaborn as sns: imports seaborn for plotting.import matplotlib.pyplot as plt: imports matplotlib's plotting interface.sns.plot_function(data): creates a seaborn plot.plt.function(): modifies the plot (title, labels, limits).plt.show(): displays the final plot.
python
import seaborn as sns import matplotlib.pyplot as plt sns.scatterplot(data=dataframe, x='x_column', y='y_column') plt.title('My Plot Title') plt.xlabel('X Axis Label') plt.ylabel('Y Axis Label') plt.show()
Example
This example shows how to create a scatter plot with seaborn and customize it with matplotlib by adding a title and axis labels.
python
import seaborn as sns import matplotlib.pyplot as plt # Load example dataset iris = sns.load_dataset('iris') # Create scatter plot with seaborn sns.scatterplot(data=iris, x='sepal_length', y='sepal_width', hue='species') # Customize plot with matplotlib plt.title('Iris Sepal Dimensions') plt.xlabel('Sepal Length (cm)') plt.ylabel('Sepal Width (cm)') # Show the plot plt.show()
Output
A scatter plot showing iris sepal length vs sepal width with points colored by species, titled 'Iris Sepal Dimensions' with labeled axes.
Common Pitfalls
Common mistakes when using matplotlib with seaborn include:
- Not calling
plt.show()to display the plot. - Calling seaborn plotting functions after
plt.show(), which resets the figure. - Trying to customize plots before creating them with seaborn.
- Overlapping commands that reset the plot unintentionally.
Always create the seaborn plot first, then customize with matplotlib, and finally call plt.show().
python
import seaborn as sns import matplotlib.pyplot as plt # Wrong order example plt.title('Wrong Order') # This does nothing because no plot exists yet sns.scatterplot(data=sns.load_dataset('iris'), x='sepal_length', y='sepal_width') plt.show() # Correct order example sns.scatterplot(data=sns.load_dataset('iris'), x='sepal_length', y='sepal_width') plt.title('Correct Order') plt.show()
Output
First plot shows no title; second plot shows title 'Correct Order'.
Quick Reference
Tips for using matplotlib with seaborn:
- Import seaborn as
snsand matplotlib.pyplot asplt. - Create plots with seaborn functions like
sns.scatterplot(),sns.barplot(), etc. - Use matplotlib functions like
plt.title(),plt.xlabel(),plt.ylabel(), andplt.legend()to customize. - Call
plt.show()once after all customizations to display the plot.
Key Takeaways
Use seaborn to create plots and matplotlib to customize them.
Always call plt.show() after all plotting and customization commands.
Create the seaborn plot before applying matplotlib customizations.
Import seaborn as sns and matplotlib.pyplot as plt for standard usage.
Matplotlib functions control titles, labels, legends, and figure display.