Python Tutorials · Python Matplotlib

Matplotlib Plotting

Learn all about Matplotlib Plotting in this comprehensive tutorial.

5 min read intermediate
  • The plot() function is used to draw points (markers) in a diagram.
  • To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.
  • You can plot as many points as you like, just make sure you have the same number of points in both axis.
  • If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 etc.

Plotting x and y points

The plot() function is used to draw points (markers) in a diagram.

By default, the plot() function draws a line from point to point.

The function takes parameters for specifying points in the diagram.

Parameter 1 is an array containing the points on the x-axis.

Parameter 2 is an array containing the points on the y-axis.

If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the plot function.

python
Note: The x-axis is the horizontal axis. The y-axis is the vertical axis.

Plotting Without Line

To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.

python
Note: You will learn more about markers in the next chapter.

Multiple Points

You can plot as many points as you like, just make sure you have the same number of points in both axis.

python

Default X-Points

If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 etc., depending on the length of the y-points.

So, if we take the same example as above, and leave out the x-points, the diagram will look like this:

python
Note: The x-points in the example above are [0, 1, 2, 3, 4, 5].

Module quiz

2 questions
1

Which of the following is true about Matplotlib Plotting?

2

What is the most common pitfall when working with Matplotlib Plotting?

Answer all questions to submit.