How to Plot Points in Matplotlib using various Methods

Plot Points in Matplotlib using various Methods

Do you want to plot points in matplotlib ? Then you have come to the right place. In this entire tutorial, you will learn how to plot points in matplotlib using various methods.

What are the points?

For plotting any figure you generally need two values. One for the x coordinate and the other y- coordinate.  If you pair them both then there are called points (x,y). Points can be of two dimensional(x,y) or three dimensional(x,y,z).  There are many figures you can plot using these points like line chart, scatter chart e.t.c. In this tutorial, I will plot a scatter chart.

Steps to Plot Points in Matplotlib

In this section, you will know all the steps required to plot points in Matplotlib. Please follow them for more understanding.

Step 1: Import all the necessary libraries

The first step is to import all the required libraries for this tutorial. I am using NumPy and matplotlib . So let’s import them.

import numpy as np
import matplotlib.pyplot as plt

Step 2: Cream Sample Data Points

Before plotting the data points let’s create sample data points. Here I am creating points in two ways. The first way is x and y have separate values. The second way is to take x and y as pair (x,y).

Let’s create them.

First Sample

x = [-1,-2,-3,-4,1,2,3,4]
y = [1,4,9,16,1,4,9,16]

Second Sample

points = [(-1,1),(-2,4),(-3,9),(-4,16),(1,1),(2,4),(3,9),(4,16)]

Here I take a list of tuples contacting x and y data points values.

In the next step, We will plot the figure using these samples.

Step 3: Plot the data point using Matplotlib

After the creation of the data points, let’s plot them. Here I will plot scatter plot only. You may plot any chart that accepts x and y values.

Plotting Figure using the separate x and y values

Execute the below lines of code and see the output.

import numpy as np
import matplotlib.pyplot as plt

x = [-1,-2,-3,-4,1,2,3,4]
y = [1,4,9,16,1,4,9,16]
plt.scatter(x, y)
plt.show()

Output

Scatter plot using x and y values seprately
Scatter plot using x and y values separately

You can see the plot  It is parabolic that is it follows the equation y=x^2.

Plotting Figure using x and y as pair

Now, let’s take points as a pair and plot scatter plot on them. Just run the below lines of code and see the output.

import numpy as np
import matplotlib.pyplot as plt
points = [(-1,1),(-2,4),(-3,9),(-4,16),(1,1),(2,4),(3,9),(4,16)]
plt.scatter(*zip(*points))

Here *zip(*points) will unpack data points into the list.

Output

Plotting Figure using x and y as pair 2
Plotting Figure using x and y as pair

Conclusion

These are methods to Plot Points in Matplotlib. You can use any method you want. The choice depends on whether you are taking input data points as a pair or separately. Hope you have liked this tutorial and it has solved your queries. Even you are facing difficulties then you can contact us for more help.

Source :

matplotlib.pyplot.scatter

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner