How to draw empty circles on a Scatter Plot in Matplotlib

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# How to draw empty circles on a Scatter Plot in Matplotlib

To draw empty circles on a scatter plot in matplotlib:

  1. Use the numpy.random.randn() method to get samples from the standard normal distribution for the values of x and y
  2. Pass x and y to the pyplot.scatter() method.
main.py
import matplotlib.pyplot as plt import numpy as np x = np.random.randn(90) y = np.random.randn(90) print(x) plt.scatter(x, y, s=80, edgecolors='r', facecolors='none') plt.show()

draw empty circles on scatter plot in matplotlib

The code for this article is available on GitHub

We used the numpy.random.randn() method to get samples from the standard normal distribution.

main.py
import matplotlib.pyplot as plt import numpy as np x = np.random.randn(90) y = np.random.randn(90)

The only argument we passed to the method is how many elements should be contained in the array.

The matplotlib.pyplot.scatter() method creates a scatter plot of y vs x with varying marker size and/or color.

The x and y arguments are the data positions.

The s argument is the marker size in points**2. The default is rcParams['lines.markersize'] ** 2.

main.py
import matplotlib.pyplot as plt import numpy as np x = np.random.randn(90) y = np.random.randn(90) print(x) plt.scatter(x, y, s=80, edgecolors='r', facecolors='none') plt.show()

The edgecolors parameter determines the edge color of the marker.

The parameter has the following possible values:

  • face - the edge color will always be the same as the face color.
  • none - no patch boundary will be drawn.
  • A color or sequence of colors.

You can set the facecolors argument to the string "none" to plot unfilled outlines.

The last step is to call the matplotlib.pyplot.show method to display all open figures.

Make sure to set the facecolors argument to the string "none" and not the value None.

Here is an example of using a different color for the edges.

main.py
import matplotlib.pyplot as plt import numpy as np x = np.random.randn(90) y = np.random.randn(90) print(x) plt.scatter(x, y, s=80, edgecolors='blue', facecolors='none') plt.show()

draw empty circles on scatter plot different edge color

The code for this article is available on GitHub

Here is a more verbose example.

main.py
import matplotlib.pyplot as plt import numpy as np x = np.random.randn(60) y = np.random.randn(60) z = np.random.randn(60) path_collection = plt.scatter(x, y, s=80, c=z, cmap='viridis', edgecolors='b') path_collection.set_facecolor('none') plt.colorbar() plt.show()

draw empty circles on scatter plot with facecolor

The c argument is an array-like list of marker colors.

The cmap argument is the colormap instance or registered colormap name that is used to map scalar data to colors.

The set_facecolor() method sets the face color of the axes.

# Draw empty circles on a Scatter Plot in Matplotlib using plot()

You can also use the pyplot.plot method to draw empty circles on a scatter plot in matplotlib.

main.py
import matplotlib.pyplot as plt import numpy as np x = np.random.randn(90) y = np.random.randn(90) print(x) plt.plot(x, y, 'o', markerfacecolor='none') plt.show()

draw empty circles on scatter plot using plot

The code for this article is available on GitHub

The x and y parameters are the coordinates of the points.

The third parameter enables us to specify basic formatting settings like color, marker and line style.

The string o is used for a circle marker.

The argument consists of a part for color, marker and line:

main.py
fmt = '[marker][line][color]'

Each of the values is optional.

If not provided, the value from the style cycle is used.

Here are some examples:

main.py
'b' # blue markers with default shape 'or' # red circles '-g' # green solid line '--' # dashed line with default color '^k:' # black triangle_up markers connected by a dotted line

The following example sets the edge color of the marker to red.

main.py
import matplotlib.pyplot as plt import numpy as np x = np.random.randn(90) y = np.random.randn(90) print(x) plt.plot(x, y, 'or', markerfacecolor='none') plt.show()

draw empty circles with plot setting edge color

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.