← Back to Libraries📊 Data Science
📦

Matplotlib in Python

Learn how to harness the power of Matplotlib for your data visualization needs in Python.

pip install matplotlib

Overview

Matplotlib is a comprehensive library used for creating static, animated, and interactive visualizations in Python. It is highly versatile and can be used to generate plots of varying complexity.

Key features of Matplotlib include a wide range of plot types, customization options, and the ability to integrate with other libraries like NumPy and Pandas. It is widely used in data analysis, scientific research, and engineering.

To get started with Matplotlib, install the library using pip, import it in your Python script, and begin creating plots using its extensive API. Common patterns include creating line plots, scatter plots, and histograms.

Code Examples

Line Plot Example

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.ylabel('Squaring')
plt.show()

Scatter Plot Example

import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.scatter(x, y)
plt.show()

Bar Plot Example

import matplotlib.pyplot as plt
x = ['A', 'B', 'C']
y = [3, 7, 5]
plt.bar(x, y)
plt.show()

Histogram Example

import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(1000)
plt.hist(data, bins=30)
plt.show()

Pie Chart Example

import matplotlib.pyplot as plt
labels = ['A', 'B', 'C']
sizes = [15, 30, 45]
plt.pie(sizes, labels=labels)
plt.show()

Common Methods

plot

Creates a line plot.

scatter

Generates a scatter plot of x vs y.

bar

Makes a bar chart.

hist

Draws a histogram.

pie

Creates a pie chart.

xlabel

Sets the label for the x-axis.

ylabel

Sets the label for the y-axis.

title

Adds a title to the plot.

show

Displays the current figure.

subplot

Adds a subplot to the current figure.

More Data Science Libraries