Fundamentals 5 min read

Data Visualization with Matplotlib and Seaborn in Python

This article introduces Python's Matplotlib and Seaborn libraries for data visualization, covering basic and advanced statistical charts, common plot types, customization techniques, and multi‑plot layouts with clear code examples and a comparative summary of each library's strengths.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Data Visualization with Matplotlib and Seaborn in Python

In data analysis, data visualization is essential for understanding data, discovering patterns, and communicating insights. Python offers two powerful libraries: Matplotlib, a basic plotting library, and Seaborn, a high‑level statistical visualization library built on Matplotlib.

1. Matplotlib Basics

Matplotlib is one of the most popular Python plotting libraries, providing a MATLAB‑like interface.

1.1 Basic Chart Plotting

import matplotlib.pyplot as plt
import numpy as np

# Create data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Plot line chart
plt.plot(x, y, label='sin(x)', color='blue', linestyle='--')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Sine function curve')
plt.legend()
plt.grid(True)
plt.show()

1.2 Common Chart Types

Line chart: plt.plot()

Scatter chart: plt.scatter()

Bar chart: plt.bar()

Histogram: plt.hist()

Pie chart: plt.pie()

# Scatter plot example
x = np.random.rand(50)
y = np.random.rand(50)
plt.scatter(x, y, color='red', marker='o')
plt.title('Random scatter plot')
plt.show()

2. Seaborn Advanced Statistical Visualization

Seaborn builds on Matplotlib to provide more attractive and higher‑level statistical charts, especially suited for data analysis.

2.1 Distribution Plot (Distplot & KDE)

import seaborn as sns
data = np.random.randn(1000)
sns.histplot(data, kde=True, color='green')
plt.title('Data distribution (with KDE)')
plt.show()

2.2 Boxplot

tips = sns.load_dataset('tips')  # built‑in dataset
sns.boxplot(x='day', y='total_bill', data=tips)
plt.title('Daily expenditure boxplot')
plt.show()

2.3 Heatmap

flights = sns.load_dataset('flights').pivot('month', 'year', 'passengers')
sns.heatmap(flights, annot=True, fmt='d', cmap='YlOrRd')
plt.title('Flight passengers heatmap')
plt.show()

3. Customization and Multi‑Plot Layout

3.1 Subplots

fig, axes = plt.subplots(1, 2, figsize=(10, 4))

# First subplot: line chart
axes[0].plot(x, np.sin(x), color='blue')
axes[0].set_title('Sine function')

# Second subplot: scatter chart
axes[1].scatter(x, np.cos(x), color='red')
axes[1].set_title('Cosine scatter')

plt.tight_layout()
plt.show()

3.2 Style Beautification

Seaborn provides several built‑in themes:

sns.set_style('darkgrid')  # darkgrid, whitegrid, dark, white, ticks
sns.set_palette('husl')    # husl, Set2, pastel, deep, etc.

4. Summary

Library

Applicable Scenarios

Advantages

Matplotlib

Basic plotting, high customization

Flexible, powerful

Seaborn

Statistical visualization, rapid data exploration

Clean, aesthetic, friendly API

Pythonstatisticsdata visualizationCustomizationMatplotlibseabornPlotting
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.