9
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

Apollo
  • 8,874
  • 32
  • 104
  • 192

1 Answers1

7

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.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • I don't quite understand...why wouldn't it just draw a circle inside the 1.5 to 1.5 axes? – Apollo Sep 09 '16 at 00:46
  • While the axes span the same range (3 units) on both axes, the physical space that the canvas takes up in the x and y axes differs. You need to tell matplotlib to make it the same explicitly. – mgilson Sep 09 '16 at 01:04
  • Seems to me that this also needs to be before `plt.scatter(x, y)` – Toby S Sep 06 '22 at 07:50