Python – Matplotlib

Python – Matplotlib

Python – Matplotlib

Matplotlib is a python library used to create 2D graphs and plots by using python scripts. It has a module named pyplot which makes things easy for plotting by providing feature to control line styles, font properties, formatting axes etc. It supports a very wide variety of graphs and plots namely - histogram, bar charts, power spectra, error charts etc. It is used along with NumPy to provide an environment that is an effective open source alternative for MatLab. It can also be used with graphics toolkits like PyQt and wxPython.
Conventionally, the package is imported into the Python script by adding the following statement −

from matplotlib import pyplot as plt

Matplotlib Example

The following script produces the sine wave plot using matplotlib.

Example

import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on a sine curve
x_val = np.arange(0, 3 * np.pi, 0.1)
y_val = np.sin(x_val)
plt.title("sine wave form")
# Plot the points using matplotlib
plt.plot(x_val, y_val)
plt.show()

Its output is as follows −
We will see lots of examples on using Matplotlib library of python in Data science work in the next chapters.

Python – SciPy (Prev Lesson)
(Next Lesson) Python – Data Operations