Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python plotting polar equation

I am having an issue plotting a polar equation in python using matplotlib.

The way I understand it, I am to create a variable that represents theta.. or all angles that is to be used in the plot. In my case from 0 to 2(pi), with 1000 steps in between.

Then I should just be able to input the polar equation as R and, plot it.

My issue is that I know this equation* is supposed to be a circle. But my code does not plot a circle.

*r = 2sinθ + 2cosθ

Here is what is produces by wolfram alpha, which I know is the correct graph, vs my result

Wolfram Alpha Expected graph

Result of my python code Python output

Now if I change r to be this instead:

r = abs(2 * np.cos(theta) + 2 * np.sin(theta))

The graph produced is as shown here

This graphs "top half" is what i expect from my original code, and cannot figure out why the graph produces a cardioid instead of a circle

import numpy as np
from matplotlib import pyplot as plt

#Produce theta variable
theta = np.arange(0, 2*np.pi, .01)[1:]

#Input polar equation of 2sinθ + 2cosθ 
r = 2 * np.cos(theta) + 2 * np.sin(theta) 
# Adding "()" around eq doesn't change anything

#Make plt figure
fig = plt.figure()

#Make sub-plot with attribute "polar"
ax = fig.add_subplot(polar=True)

#Plot function
ax.plot(theta, r)

#Show plot
plt.show()
like image 361
matrucious Avatar asked Nov 02 '25 15:11

matrucious


1 Answers

The main confusion is that Matplotlib doesn't draw negative r values at the negative side. Instead, it makes an r-range from -3 to 3 and draws everything at the same side.

You can obtain a more conventional interpretation by rotating theta by 180º for negative r and taking the absolute value of r:

import numpy as np
from matplotlib import pyplot as plt

theta = np.arange(0, 2 * np.pi, .01)[1:]
r = 2 * np.cos(theta) + 2 * np.sin(theta)

fig = plt.figure()
ax = fig.add_subplot(polar=True)

# change negative r values to positive, rotating theta by 180º
theta = np.where(r >= 0, theta, theta + np.pi)
r = np.abs(r)
ax.plot(theta, r)

plt.show()

rotating theta 180º for negative r

Here is another example to show the difference between the default and moving negative r-values to the other side, using r = theta - pi for theta between 0 and 2 pi. The part of the curve where r is positive is drawn in blue, the negative in red. Note the labeling of the r axis: from -3 to 3 for the default and from 0 to 3 for the modified version. (With the original example, the red and blue curves occupy the same location.)

import numpy as np
from matplotlib import pyplot as plt

theta = np.arange(0, 2 * np.pi, .01)[1:]
r = theta - np.pi
positive_r = r >= 0

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 5), subplot_kw={'polar': True})

for ax in (ax1, ax2):
    if ax == ax2:
        # change negative r values to positive, rotating theta by 180º
        theta = np.where(r >= 0, theta, theta + np.pi)
        r = np.abs(r)
    ax.plot(theta[positive_r], r[positive_r], color='skyblue')
    ax.plot(theta[~positive_r], r[~positive_r], color='tomato')
ax1.set_title('Default: negative $r$\non same side as $theta$')
ax2.set_title('Negative $r$ on other side')

plt.show()

comparing polar plots with negative r

like image 74
JohanC Avatar answered Nov 04 '25 05:11

JohanC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!