1 min read

Python Matplotlib Module

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB.

Installation

pip install matplotlib

Importing the Module

import matplotlib.pyplot as plt

Basic Plotting

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.show()

Formatting the Plot

You can add titles, labels, and legends.

plt.plot(x, y, label='Line 1')

plt.title("Simple Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.legend()

plt.show()

Common Plot Types

Scatter Plot

plt.scatter(x, y)
plt.show()

Bar Chart

categories = ['A', 'B', 'C']
values = [10, 20, 15]

plt.bar(categories, values)
plt.show()

Histogram

import numpy as np

data = np.random.randn(1000)
plt.hist(data, bins=30)
plt.show()

Saving Figures

plt.savefig("my_plot.png")

programming/python/python