- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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?
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
- Related Articles
- How to a plot stem plot in Matplotlib Python?
- How to plot matplotlib contour?
- How to plot categorical variables in Matplotlib?
- How to plot a circle in Matplotlib?
- How to plot multiple graphs in Matplotlib?
- How to plot overlapping lines in Matplotlib?
- How to plot hexbin histogram in Matplotlib?
- How to plot blurred points in Matplotlib?
- How to plot a kernel density plot of dates in Pandas using Matplotlib?
- Coloring the Intersection of Circles/Patches in Matplotlib
- How to plot signal in Matplotlib in Python?
- How to plot cdf in Matplotlib in Python?
- How to plot the difference of two distributions in Matplotlib?
- How to plot additional points on the top of a scatter plot in Matplotlib?
- How to plot with different scales in Matplotlib?