Apollo Apollo - 2 months ago 8
Python Question

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

Answer

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.

Comments