Matplotlib scatter: How to Create Scatter plot in Python

0
10
Matplotlib scatter - The Complete Guide

To create scatter plots in Python, use the Matplotlib.pyplot.scatter() function. The scatter() method plots one dot for each observation. It needs two arrays of the same length, one for the values of the x-axis, and one for values on the y-axis.

Matplotlib scatter

Matplotlib.pyplot.scatter() is a built-in library function that creates a scatter plot for the given points and displays the graph as the output. The scatter plot is similar to a line graph. The main difference between a scatter plot and a line graph is that the points are not continuous and cannot be connected in a line. When the points are scattered, then we can use this scatter plot.

Syntax

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, 
                          vmax=None, alpha=None, linewidths=None, *, edgecolors=None, 
                          plotnonfinite=False, data=None, **kwargs) 

Arguments

The matplotlib.pyplot.scatter() function has two required arguments as parameters:

x, y: These are the required arguments. These arguments take two arrays as a value. This array consists of the data points. 

s: This argument is optional. This argument can take float values or arrays as values. For example, the marker size can be changed using this argument. 

c: This is an optional argument. The array consisting of colors is passed as a value to this argument. This parameter takes a list of elements as values. 

marker: This parameter specifies the style of the marker used in the scatter plot. By default, the marker style is kept as ‘o’. However, the marker style can be modified by passing the marker style in this parameter. 

cmap: This parameter is used only when the c parameter is passed with the floating-point values. By using this parameter, the floating value is converted to the respective color. 

norm: This parameter is used only when the c parameter is passed with the floating-point number. This function is used to normalize the data in the c parameter. The floating-point range is normalized between 0 to 1. 

vmin, vmax: This argument can only be used when the norm parameter is not used. This vmin and vmax are used along with the default norm to map the color array c to the color map array cmap.   

alpha: This is an optional argument. This argument takes values from 0 to 1. 0 is used for transparent, and 1 is used for opaque. 

linewidths: The linewidth of the marker edges are passed in this argument. This argument takes floating-point numbers or arrays as values. 

edgecolors: The edge colors of the marker are passed in this argument.  

plotnonfinite: This is a Boolean value. If True, the infinite points are plotted in the graph. By default, it is set as False.

Return value

The matplotlib scatter() function plots a scatter plot as output. The matplotlib.pyplot.scatter() function creates a scatter plot and displays it in the output. 

Program for creating a scatter plot using matplotlib.pyplot.scatter

# Importing matplotlib.pyplot as plt.
import matplotlib.pyplot as plt

# Importing numpy as np
import numpy as np


# x coordinates are created
x = np.array([5,20,10,67,99,45,32,34,42])

# y coordinates are created
y = np.array([90,80,8,20,10,90,5,99,54])

# scatter plot is created
plt.scatter(x,y)

# x axis is labeled as X-Axis
plt.xlabel('X-Axis')

# y axis is labeled as Y-Axis
plt.ylabel('Y-Axis')

# Title is kept for the Scatter plot
plt.title('Scatter Plot Example')

# Displaying the created graph using the show method
plt.show()

Output

Program for creating a scatter plot using matplotlib.pyplot.scatter

 

In this program, we imported matplotlib.pyplot for plotting the scatter plot. The matplotlib library consists of all the functions for plotting different types of graphs and charts.

Then, we imported a numpy for creating x-coordinates and y-coordinates. Then we have passed these two coordinates into the scatter function. Finally, the scatter() function creates a scatter plot by combining x and y-coordinates.

This scatter plot is used when the data points are scattered unordered. In this example, the data points are unordered; hence this example describes the scatter plot properly. Then we have used the show function to display the generated scatter plot.

Program for creating a scatter plot having multiple markers using matplotlib.pyplot.scatter

# Importing matplotlib.pyplot as plt.
import matplotlib.pyplot as plt

# Importing numpy as np
import numpy as np

# x coordinates are created
x = np.array([5, 20, 10, 67, 99, 45, 32, 34, 42])

# y coordinates are created
y = np.array([90, 80, 8, 20, 10, 90, 5, 99, 54])

# x1 coordinates are created
x1 = np.array([10, 30, 15, 60, 50, 90, 40, 39, 62])

# y1 coordinates are created
y1 = np.array([90, 80, 8, 20, 10, 90, 5, 99, 54])

# scatter plot is created
plt.scatter(x, y, c="pink",
 linewidths=2,
 marker="^",
 edgecolor="green",
 s=50)
plt.scatter(x1, y1, c="blue",
 linewidths=2,
 marker="*",
 edgecolor="yellow",
 s=150)

# x axis is labeled as X-Axis
plt.xlabel('X-Axis')

# y axis is labeled as Y-Axis
plt.ylabel('Y-Axis')

# Title is kept for the Scatter plot
plt.title('Scatter Plot Example')

# displaying the created graph using the show method
plt.show()

Output

Program for creating a scatter plot having multiple markers using matplotlib.pyplot.scatter

 

In this program, we have imported matplotlib.pyplot library for plotting the scatter plot. The matplotlib library consists of all the functions for plotting different types of graphs and charts.

Then we imported numpy for creating x coordinates and y coordinates. Then we have created another set of points called x1 and y1. Then we have passed x and y coordinates into the scatter function with the color pink and the edgecolor as green and the marker as a triangle.

Then we have passed the points x1 and y1 into the scatter function with the color as blue and the edge color as yellow and the marker as a star. This function creates a scatter plot by combining x and y coordinates. Then we used the show() function to display the generated scatter plot. In this example, two coordinates are plotted in a single scatter plot.

That’s it for this tutorial.

Related posts

Matplotlib legend

Matplotlib histogram

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.