- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to plot thousands of circles quickly in Matplotlib?
63 Lectures 6 hours
To plot thousands of circles quickly in Matplotlib, we will have to use matplotlib.collections. In this case, we will use CircleCollection.
- Import the collections package from matplotlib along with pyplot and numpy.
- Set the figure size and adjust the padding between and around the subplots.
- Initialize variables "num" for number of small circles and "sizes" for sizes of circles.
- Create a list of circle patches.
- Add circle patch artist on the current axis.
- Set the margins of the axes.
- To display the figure, use show() method.
import numpy as np import matplotlib.pyplot as plt import matplotlib.collections as mc plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True num = 1000 sizes = 50 * np.random.random(num) xy = 10 * np.random.random((num, 2)) patches = [plt.Circle(center, size) for center, size in zip(xy, sizes)] fig, ax = plt.subplots() collection = mc.CircleCollection(sizes, offsets=xy, transOffset=ax.transData, color='green') ax.add_collection(collection) ax.margins(0.01) plt.show()
It will produce the following output
- Coloring the Intersection of Circles/Patches in Matplotlib
- How to plot matplotlib contour?
- How to a plot stem plot in Matplotlib Python?
- How to plot a kernel density plot of dates in Pandas using Matplotlib?
- How to plot multiple graphs in Matplotlib?
- How to plot a circle in Matplotlib?
- How to plot overlapping lines in Matplotlib?
- How to plot hexbin histogram in Matplotlib?
- How to plot categorical variables in Matplotlib?
- How to plot blurred points in Matplotlib?
- Drawing circles on an image with Matplotlib and NumPy
- How to plot the difference of two distributions in Matplotlib?
- How do I format the axis number format to thousands with a comma in Matplotlib?
- How to plot additional points on the top of a scatter plot in Matplotlib?
- How to plot cdf in Matplotlib in Python?