Fundamentals 10 min read

Creating Various Charts with Matplotlib and Seaborn in Python

This guide demonstrates how to generate line, bar, scatter, and box charts using Matplotlib, as well as line, bar, scatter, box, heatmap, and joint plots with Seaborn, providing complete Python code snippets for each visualization type.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Creating Various Charts with Matplotlib and Seaborn in Python

This article provides step‑by‑step examples for creating a range of common data visualizations in Python using the Matplotlib and Seaborn libraries. Each section includes a brief description of the chart type followed by a ready‑to‑run code snippet.

Matplotlib Line Chart

import pandas as pd
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320]
}
df = pd.DataFrame(data)
plt.figure(figsize=(10, 5))
plt.plot(df['Month'], df['Sales'], marker='o')
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()

Matplotlib Bar Chart

import pandas as pd
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320]
}
df = pd.DataFrame(data)
plt.figure(figsize=(10, 5))
plt.bar(df['Month'], df['Sales'], color='skyblue')
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

Matplotlib Scatter Plot

import pandas as pd
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320],
    'Advertising': [100, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220]
}
df = pd.DataFrame(data)
plt.figure(figsize=(10, 5))
plt.scatter(df['Sales'], df['Advertising'])
plt.title('Sales vs Advertising')
plt.xlabel('Sales')
plt.ylabel('Advertising')
plt.grid(True)
plt.show()

Matplotlib Box Plot

import pandas as pd
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320]
}
df = pd.DataFrame(data)
plt.figure(figsize=(8, 6))
plt.boxplot(df['Sales'], vert=False)
plt.title('Sales Distribution')
plt.xlabel('Sales')
plt.yticks([])
plt.grid(axis='x', linestyle='--', alpha=0.7)
plt.show()

Seaborn Line Plot

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320]
}
df = pd.DataFrame(data)
plt.figure(figsize=(10, 5))
sns.lineplot(x='Month', y='Sales', data=df, marker='o', markersize=8, linewidth=2)
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()

Seaborn Bar Plot

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320]
}
df = pd.DataFrame(data)
plt.figure(figsize=(10, 5))
sns.barplot(x='Month', y='Sales', data=df, palette='Blues_d')
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

Seaborn Scatter Plot

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320],
    'Advertising': [100, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220]
}
df = pd.DataFrame(data)
plt.figure(figsize=(10, 5))
sns.scatterplot(x='Sales', y='Advertising', data=df, hue='Month', palette='Set2')
plt.title('Sales vs Advertising')
plt.xlabel('Sales')
plt.ylabel('Advertising')
plt.grid(True)
plt.show()

Seaborn Box Plot

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320]
}
df = pd.DataFrame(data)
plt.figure(figsize=(8, 6))
sns.boxplot(y='Sales', data=df, palette='pastel')
plt.title('Sales Distribution')
plt.xlabel('Sales')
plt.grid(axis='x', linestyle='--', alpha=0.7)
plt.show()

Seaborn Heatmap

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320],
    'Advertising': [100, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220]
}
df = pd.DataFrame(data)
plt.figure(figsize=(8, 6))
corr_matrix = df.corr()
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()

Seaborn Joint Plot

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 创建示例数据
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    'Sales': [200, 250, 270, 300, 330, 380, 400, 450, 420, 390, 350, 320],
    'Advertising': [100, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220]
}
df = pd.DataFrame(data)
sns.jointplot(x='Sales', y='Advertising', data=df, kind='hex', marginal_kws=dict(bins=10, fill=True))
plt.show()

These examples cover the most frequently used chart types for exploratory data analysis and reporting, illustrating how to set figure size, titles, axis labels, grid lines, and color palettes in both Matplotlib and Seaborn.

PythonData VisualizationMatplotlibpandasseabornchart
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

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.