English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Although it is easy to quickly generate plots using the matplotlib.pyplot module, it is recommended to use the object-oriented method because it can better control and customize plots. Most functions are also provided in the matplotlib.axes.Axes class.
The main idea behind using a more formal object-oriented method is to create a graph object and then only call the methods or properties of the object. This method helps better handle canvases with multiple drawings.
In the object-oriented interface, Pyplot is used only for some functions, such as graph creation, and users explicitly create and track graph and axis objects. At this level, users create graphs using Pyplot, through which one or more axis objects can be created. Then, these axis objects are used for most drawing operations.
First, create a graph instance that provides an empty canvas.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 fig = plt.figure()
Add the axis to the graph. The add_axes() method requires a4a list object containing elements, corresponding to the left, bottom, width, and height of the graph. Each number must be between 0 and1between -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 ax=fig.add_axes([0,0,1,1])
Set the labels and title of the x and y axes -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 ax.set_title("sine wave") ax.set_xlabel('angle') ax.set_ylabel('sine')
call the plot() method of the axes object.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 ax.plot(x,y)
If you are using Jupyter notebook, then you need to add the %matplotlib inline command; the show() function of the pyplot module displays the graph.
Read and execute the following code -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 from matplotlib import pyplot as plt import numpy as np import math # Display Chinese settings... plt.rcParams['font.sans']-serif] = ['SimHei'] # Step 1 (Replacement of sans)-serif font) plt.rcParams['axes.unicode_minus'] = False # Step 2 (Resolving the display problem of negative numbers on the axis) %matplotlib inline x = np.arange(0, math.pi*2, 0.05) y = np.sin(x) fig = plt.figure() ax = fig.add_axes([0,0,1,1]) ax.plot(x,y) ax.set_title("Sine Wave") ax.set_xlabel('Angle') ax.set_ylabel('Sine') plt.show()
The following code generates the following output above -
on Jupyter notebookrun the same code as when running in -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 from matplotlib import pyplot as plt import numpy as np import math # Display Chinese settings... plt.rcParams['font.sans']-serif] = ['SimHei'] # Step 1 (Replacement of sans)-serif font) plt.rcParams['axes.unicode_minus'] = False # Step 2 (Resolving the display problem of negative numbers on the axis) %matplotlib inline x = np.arange(0, math.pi*2, 0.05) y = np.sin(x) fig = plt.figure() ax = fig.add_axes([0,0,1,1]) ax.plot(x,y) ax.set_title("Sine Wave") ax.set_xlabel('Angle') ax.set_ylabel('Sine') plt.show()
The output is displayed as follows -