【发布时间】:2021-10-21 07:15:35
【问题描述】:
我在使用 matplotlib 在 python 中绘制极坐标方程时遇到问题。
按照我的理解,我将创建一个变量来表示 theta.. 或要在图中使用的所有角度。在我的例子中,从 0 到 2(pi),中间有 1000 步。
那么我应该能够将极坐标方程输入为 R 并绘制它。
我的问题是我知道这个方程*应该是一个圆圈。但是我的代码没有画一个圆圈。
*r = 2sinθ + 2cosθ
这是 wolfram alpha 产生的结果,我知道这是正确的图表,与我的结果相比
Wolfram 阿尔法Expected graph
我的python代码的结果Python output
现在如果我把 r 改成这样:
r = abs(2 * np.cos(theta) + 2 * np.sin(theta))
生成的图如图here
这个图表“上半部分”是我对原始代码的期望,并且无法弄清楚为什么图表会产生心形而不是圆形
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()
【问题讨论】:
-
主要的困惑是 Matplotlib 不会在负侧绘制负 r 值。相反,它使 r 范围从 -3 到 3。
标签: python numpy matplotlib math plot