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

Scorri per mostrare il menu

book
Basic Plotting

Now that you are familiar with the matplotlib architecture you are ready to create your first plot, congratulations! We’ll walk through two possible ways to make a plot:

  • using scripting approach;

  • using object-oriented approach (explicit definition instances 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

The order of the arguments is important!

Finally, we display the plot using 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 that instead of directly calling the plot() function, we create a Figure and Axes object using the subplots() functions, and then use the .plot() method 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

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

python

In fact, both options are still equivalent if we use any other plotting function instead of plot().

Further in the course we’ll mostly use a scripting approach, however, it is still important for you to know both of them. Now it’s your turn to plot a point.

Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

book
Basic Plotting

Now that you are familiar with the matplotlib architecture you are ready to create your first plot, congratulations! We’ll walk through two possible ways to make a plot:

  • using scripting approach;

  • using object-oriented approach (explicit definition instances 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

The order of the arguments is important!

Finally, we display the plot using 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 that instead of directly calling the plot() function, we create a Figure and Axes object using the subplots() functions, and then use the .plot() method 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

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

python

In fact, both options are still equivalent if we use any other plotting function instead of plot().

Further in the course we’ll mostly use a scripting approach, however, it is still important for you to know both of them. Now it’s your turn to plot a point.

Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4
Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt