Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Basic Plotting | Matplotlib Introduction
Ultimate Visualization with Python

Swipe to show menu

book
Basic Plotting

With an understanding of the matplotlib architecture, proceed to create the first plot. Two common methods for creating a plot are demonstrated:

  • The scripting approach;

  • The object-oriented approach, involving explicit instantiation of Artist objects.

Scipting Approach

With this approach there is no need for you to explicitly create Figure and Axes object (it is done under the hood).

In 2D space, each point has x and y coordinates. To plot it, import the pyplot submodule, use the plt alias, initialize x and y variables, and call the plot() function with x and y as arguments, along with 'o' for the point marker.

Note
Note

The order of the arguments is important!

The plot is displayed with plt.show():

12345
import matplotlib.pyplot as plt x = 5 y = 4 plt.plot(x, y, 'o') plt.show()
copy

Object-oriented Approach

The only difference here is the use of the object-oriented approach: a Figure and Axes object are created using subplots(), and the .plot() method is called on the Axes object with the same arguments.

123456
import matplotlib.pyplot as plt x = 5 y = 4 fig, ax = plt.subplots() ax.plot(x, y, 'o') plt.show()
copy
Note
Note

The following two lines are equivalent to plt.plot().

Both options remain equivalent even when using other plotting functions instead of plot(). The course will mainly follow the scripting approach, though understanding both approaches is essential. Now, plot a point using the method of your choice.

Task

Swipe to start coding

  1. Import the pyplot submodule from the matplotlib library with the plt alias.
  2. Assign values 10 and 2 to variables x and y respectively.
  3. Pass x and y as arguments to the plot() function, first x, then y.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4
We're sorry to hear that something went wrong. What happened?

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Basic Plotting

With an understanding of the matplotlib architecture, proceed to create the first plot. Two common methods for creating a plot are demonstrated:

  • The scripting approach;

  • The object-oriented approach, involving explicit instantiation of Artist objects.

Scipting Approach

With this approach there is no need for you to explicitly create Figure and Axes object (it is done under the hood).

In 2D space, each point has x and y coordinates. To plot it, import the pyplot submodule, use the plt alias, initialize x and y variables, and call the plot() function with x and y as arguments, along with 'o' for the point marker.

Note
Note

The order of the arguments is important!

The plot is displayed with plt.show():

12345
import matplotlib.pyplot as plt x = 5 y = 4 plt.plot(x, y, 'o') plt.show()
copy

Object-oriented Approach

The only difference here is the use of the object-oriented approach: a Figure and Axes object are created using subplots(), and the .plot() method is called on the Axes object with the same arguments.

123456
import matplotlib.pyplot as plt x = 5 y = 4 fig, ax = plt.subplots() ax.plot(x, y, 'o') plt.show()
copy
Note
Note

The following two lines are equivalent to plt.plot().

Both options remain equivalent even when using other plotting functions instead of plot(). The course will mainly follow the scripting approach, though understanding both approaches is essential. Now, plot a point using the method of your choice.

Task

Swipe to start coding

  1. Import the pyplot submodule from the matplotlib library with the plt alias.
  2. Assign values 10 and 2 to variables x and y respectively.
  3. Pass x and y as arguments to the plot() function, first x, then y.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt