Back to Python Notes
Topic #119

Matplotlib Plotting

plot() draws points and lines from x and y values, the simplest visualisation there is. Customising markers and styles follows naturally.


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.

Example

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()

image

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'.

Example

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints, 'o')
plt.show()

image

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.

Example

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()

image


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:

Example

import matplotlib.pyplot as plt
import numpy as np

  ypoints = np.array([3, 8, 1, 10, 5, 7])

plt.plot(ypoints)
plt.show()

image

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

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Matplotlib Plotting – FAQs

Quick answers about learning Matplotlib Plotting in Python.

This free note from Coding Now Tech Institute explains Matplotlib Plotting in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Matplotlib Plotting, is 100% free with no signup required.
With focused practice, most students grasp Matplotlib Plotting in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now