Scatter plotting of a circle in polar coordinates produces... cardioid

I’m happy to withdraw this issue if I’m doing something wrong, but what’s expected to be a circle gets drawn as a cardioid, I can’t see a reason why.

The code is

import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

N = 250
sample_phi = 2 * np.pi * np.random.rand(N)
sample_rho = 2 * np.cos(sample_phi)

fig = plt.figure()
ax = fig.add_subplot(projection=“polar”)
sample_phi, sample_rho,

Wolfram alpha gets it right though.

Which data points do you think are drawn at the wrong location? Wolfram seem to be plotting np.abs(2*cos(sample_phi)), sample_phi but just the positive side.

Ahh, took a little bit, but I think I figured out what is going on…

Wolfram is translating negative R values to be opposite theta values (e.g. 45 deg with a value of -.75 is being plotted at 225 deg) with zero always staying at the center.

What matplotlib is doing is allowing r = 0 to be a circle instead of just the single point at the center.

Note that the axis label for the r-axis goes from -2 to 2

1 Like

explicitly setting the r-lim mitigates the problem somewhat (The shape looks as you expected, but the points with negative r values are not plotted)

ax.set_rlim(0, 2)

1 Like

Incredible finding. Thank you so much for taking a look! Should not something be done by default in matplotlib to handle such situations, so that people won’t get confused in the future?