Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating points on a circle

import random
import math
import matplotlib.pyplot as plt


def circle():
    x = []
    y = []
    for i in range(0,1000):
        angle = random.uniform(0,1)*(math.pi*2)
        x.append(math.cos(angle));
        y.append(math.sin(angle));
    plt.scatter(x,y)
    plt.show()
circle()

I've written the above code to draw 1000 points randomly on a unit circle. However, when I run this code, it draws an oval for some reason. Why is this?

enter image description here

like image 794
Apollo Avatar asked Sep 09 '16 00:09

Apollo


People also ask

How do you generate random points in a circle?

Thus a circle which has radius r will have 2 * pi * r "points" on its circumference. The total number of points is pi * R^2. Thus you should give the circle r a probability equal to (2 * pi * r) / (pi * R^2) = 2 * r/R^2.

How do you generate random points on a circle in Matlab?

r = R*sqrt(rand(n,1)); x = x0 + r. *cos(t);

How do you find a point on a circle given the equation?

Distance Formula for a Point and the Center of a Circle: d=√(x−h)2+(y−k)2 d = ( x − h ) 2 + ( y − k ) 2 , where (x, y) is the point and (h,k) is the center of the circle. This formula is derived from the Pythagorean Theorem.

How many points are available on circle?

Three points uniquely define a circle. If you circumscribe a circle around a triangle, the circumcenter of that triangle will also be the center of that circle.


1 Answers

It is a circle -- The problem is that the aspect ratio of your axes is not 1 so it looks like an oval when you plot it. To get an aspect ratio of 1, you can use:

plt.axes().set_aspect('equal', 'datalim')  # before `plt.show()`

This is highlighted in a demo.

like image 82
mgilson Avatar answered Nov 15 '22 16:11

mgilson