Python Matplotlib Scatter Plot
In this post, you will learn about the Python matplotlib scatter plot.
The scatter plot is a type of plot used to show the relationship between two variables. One of the two variables is scaled horizontally and the other is scaled vertically, and the graph is plotted on a Cartesian plane based on these variable positions. In this plot, each value in the data set is represented by a dot.
Matplotlib provides the scatter() function to plot X versus Y unidimensional array of the same length as the scatter plot. Here is a very basic example of the scatter plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(500)
y = np.random.randn(500)
plt.scatter(x, y);
plt.show()
The np.random.randn() creates an array of a specified shape and fills it with random values as per standard normal distribution. The above code returns the following plot.
Syntax of scatter()
The scatter() function has the following syntax -
plt.scatter(x, y, s, c, marker)
x, y- It specifies the data position,
s- the size of the markers in pixel*pixel. It can be a single value or an array of the same size of X and Y
c- It specifies the points color. It can be a single value or a list of colors eventually of the same size of X and Y.
marker- It specifies the marker to use to plot the points. These are some of the available marker values-
s- square, o - circle, d- diamond, p- pentagon, h- hexagon, 8- Octagon, +- plus, X- cross.
Matplotlib scatter plot Example 1
Here, we have specified a specific color and marker style to each point.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(500)
y = np.random.randn(500)
plt.scatter(x, y, c='#0B0B61', marker='o');
plt.show()
Matplotlib scatter plot Example 2
In the above example, we have specified a single color, but we can also specify some random colors for representing data.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(500)
y = np.random.randn(500)
colors = np.random.rand(500)
plt.scatter(x, y,
c = colors, # Color of the dots
s=100, # Size of the dots
linewidths=1) # Size of edge around the dots
plt.show()
Scatter plot with Label Points
The plt.annotate() method is used to annotate the point x and y with a text label. Here, we have taken three arrays containing x, y, and label data. First, we plotted the graph using plt.scatter() and next iterated over the arrays with points (xs and ys) and called plt.annotate() using the value itself as a label.
import matplotlib.pyplot as plt
import numpy as np
plt.clf()
# x and y data
xs = [5, 13, 19, 18, 7, 11]
ys = [11, 8, 25, 19, 10, 22]
labels = ["Smith", "Priska", "Diane", "Charle", "Anjali", "Abhi"]
# plot the scatter chart
plt.scatter(xs,ys,
color="blue", # dots color
s=100, # dots size
alpha=0.5, # dots alpha
linewidths=1) # edge width around the dots
# zip joins x and y coordinates in pairs
for x,y, label in zip(xs,ys,labels):
# this method is called for each point
plt.annotate(label, # set the text
(x,y), # the point to label
textcoords="offset points", # text positioning
xytext=(0,10), # distance from text to points (x,y)
ha='left', # vertical alignment
va='center') # horizontal alignment
plt.show()
The above code returns the following code-
Related Articles
Simple Matplotlib Animation ExampleMatplotlib Pie Chart
Python Line Plot Using Matplotlib
Python Matplotlib 3D Plot
How to save figure in matplotlib pyplot
Python Pandas Plotting
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples