Skip to content
Advertisement

How to scatter plot two concentric circles with Numpy and Matplotlib

I’m trying to recreate the following plot with Numpy and Matplotlib as part of a question, but I’m struggling to get the “random aspect” of the plot:

this plot

I’ve already managed to create the two circles and plot them with this code:

JavaScript

But I don’t know how to make them get this random scattering, like in the example image. I know I need to use Numpy’s random number generation, but I don’t know how or where to use it, exactly.

Advertisement

Answer

You might need more than 100 points, so let’s say

JavaScript

You have the right idea regarding how to build a circle, so add a random radius to each point:

JavaScript

This is not necessarily the best way to do it, since the points will be biased towards the center, but it suffices to illustrate the idea.

JavaScript

On second inspection of your images, you may want to replace np.random.uniform with np.random.normal with a standard deviation of 1.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement