0

I am trying to plot simple function r = 3*sin(2*theta) using matplotlib:

import numpy as np
import matplotlib.pyplot as plt
theta = np.arange(0,2*np.pi,0.01)
r = 3.0*np.sin(2.0*theta)
ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
plt.show()

This is the result I get (it is not correct):

matplolib

This is what I expect to see (wolfram alpha): wolfram alpha

Am I missing something? Thanks!

3
  • 1
    matplotlib polar apparently doesn't do negative r see comment link in stackoverflow.com/questions/42982290/… Commented Mar 31, 2017 at 5:06
  • @f5r5e5d you answered my question Commented Mar 31, 2017 at 5:15
  • @f5r5e5d could you make this your answer? Thanks! Commented Mar 31, 2017 at 5:16

1 Answer 1

1

this patches the polar plot for neg r

import numpy as np
import matplotlib.pyplot as plt
theta = np.arange(0,2*np.pi,0.01)
r = 3.0*np.sin(2.0*theta)
theta = theta + (1 - np.sign(r))*np.pi/2 # add pi to points with negative r values
r = np.abs(r) # make all r values postive to fake out matplotlib
ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.