matplotlib意外结果极地情节

时间:2017-03-31 05:00:53

标签: python numpy matplotlib

我试图使用matplotlib绘制简单函数r = 3 * sin(2 * theta):

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()

这是我得到的结果(不正确):

matplolib

这是我期望看到的(wolfram alpha):wolfram alpha

我错过了什么吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

这会修补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()