Apollo
Apollo

Reputation: 9054

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

Upvotes: 9

Views: 5733

Answers (2)

You can just set figure size:

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.figure(figsize=(5,5))
plt.scatter(x,y)
plt.show()

circle()

Upvotes: 0

mgilson
mgilson

Reputation: 309929

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.

Upvotes: 9

Related Questions