English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib Simple Plotting

In this chapter, we will learn how to create a simple graph using Matplotlib. Suppose we want to display a simple angle line graph in Matplotlib, corresponding to the sine values, in radians. First, import the pyplot module from the Matplotlib package and use the alias plt.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
import matplotlib.pyplot as plt

Next, a set of numbers is needed to draw. The NumPy library defines various array functions, which are imported using the np alias.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
import numpy as np

Use the arange() function from the NumPy library to obtain 0 to2ndarray object between π.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
x = np.arange(0, math.pi*2, 0.05)

An ndarray object is used as the values on the x-axis of the graph. The corresponding sine values of the angles in the x are obtained by the following statement -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
y = np.sin(x)

Use the plot() function to draw the values of two arrays.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
plt.plot(x, y)

You can set the plotting title and the labels for the x and y axes.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
plt.xlabel("angle")
 plt.ylabel("sine")
 plt.title('sine wave')

The show() function calls the plotting viewer window -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
plt.show()

The complete program code is as follows -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 # coding=utf-8
 import matplotlib.pyplot as plt
 import numpy as np
 import math # needed for definition of pi
 # Display Chinese settings...
 plt.rcParams['font.sans-serif'] = ['SimHei']  # Step 1 (replace sans-serif font)
 plt.rcParams['axes.unicode_minus'] = False  # Step 2 (solve the problem of displaying negative signs on the axis)
 x = np.arange(0, math.pi*2, 0.05)
 y = np.sin(x)
 plt.plot(x, y)
 plt.xlabel(u"angle")
 plt.ylabel("Sine")
 plt.title('Sine Wave')
 plt.show()

Executing the above example code gives the following result -

Next, run the above code in the Jupyter Notebook environment.

As mentioned earlier, start Jupyter Notebook from the Anaconda Navigator or command line. In the input cell, enter the import statements to import Pyplot and NumPy -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from matplotlib import pyplot as plt
 import numpy as np

To display the plotting output within the Jupyter Notebook (not in a separate viewer), please enter the following statement -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
%matplotlib inline

The complete code is shown as follows -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 # coding=utf-8
 import matplotlib.pyplot as plt
 import numpy as np
 import math # needed for definition of pi
 %matplotlib inline
 # Display Chinese settings...
 plt.rcParams['font.sans-serif'] = ['SimHei']  # Step 1 (replace sans-serif font)
 plt.rcParams['axes.unicode_minus'] = False  # Step 2 (solve the problem of displaying negative signs on the axis)
 x = np.arange(0, math.pi*2, 0.05)
 y = np.sin(x)
 plt.plot(x, y)
 plt.xlabel("Angle")
 plt.ylabel("Sine")
 plt.title('Sine Wave')
 plt.show()

Execute the above example code in Jupyter Notebook to get the following results -