Plotting polar function using matplotlib

huangapple go评论66阅读模式
英文:

Plotting polar function using matplotlib

问题

我尝试使用matplotlib绘制这个函数。

如您在Desmos应用中所见,该方程正确地将函数绘制为圆形,但当我尝试将其移植到Python时,我得到了这个结果:

import numpy as np
import matplotlib.pyplot as plt

def fungsi_r4(theta, theta0, r0, a):
  return r0 * np.cos(theta - theta0) + np.sqrt((a ** 2) - (r0 ** 2) * (np.sin(theta - theta0) ** 2))

theta = np.linspace(0, 2 * np.pi, 100)
r = fungsi_r4(theta, 2.4, 5.1, 2.6)

ax = plt.subplot(projection='polar')
ax.plot(theta, r)
英文:

I'm trying to plot this function using matplotlib.

Plotting polar function using matplotlib

As you can see in the Desmos app, the equation correctly plot the function as circle, but when I try to port it to Python, I got this instead:

import numpy as np
import matplotlib.pyplot as plt

def fungsi_r4(theta, theta0, r0, a):
  return r0 * np.cos(theta - theta0) + np.sqrt((a ** 2) - (r0 ** 2) * (np.sin(theta - theta0) ** 2))

theta = np.linspace(0, 2 * np.pi, 100)
r = fungsi_r4(theta, 2.4, 5.1, 2.6)

ax = plt.subplot(projection='polar')
ax.plot(theta, r)

Plotting polar function using matplotlib

My feeling tells me it has something to do with the negative values retuned from the function but I don't know what to do with it.

答案1

得分: 1

以下是翻译好的部分:

两个程序处理负半径的区别在于:Desmos将它们翻转回原点,而matplotlib将径向刻度扩展到负数。

这是一些修改数据点的代码,就像Desmos一样:

def flip_negative_radii(theta, r):
    flip_mask = r < 0
    r[flip_mask] *= -1
    theta[flip_mask] = (theta[flip_mask] - np.pi) % (2*np.pi)
    return theta, r

示例用法:

import numpy as np
import matplotlib.pyplot as plt

def fungsi_r4(theta, theta0, r0, a):
    return r0 * np.cos(theta - theta0) + np.sqrt((a ** 2) - (r0 ** 2) * (np.sin(theta - theta0) ** 2))

theta = np.linspace(0, 2 * np.pi, 100)
r = fungsi_r4(theta, 2.4, 5.1, 2.6)

ax = plt.subplot(projection='polar')
ax.plot(*flip_negative_radii(theta, r))
plt.show()

间隙是由于平方根中的项变为负数并产生NaN导致的。如果可能的话,我建议尝试提出函数的参数表示,这将避免这两个问题。

英文:

The difference is in how the two programs handle negative radii: Desmos flips them back through the origin, while matplotlib extends the radial scale into the negatives.

Here's some code to modify the data points like Desmos does:

def flip_negative_radii(theta, r):
    flip_mask = r &lt; 0
    r[flip_mask] *= -1
    theta[flip_mask] = (theta[flip_mask] - np.pi) % (2*np.pi)
    return theta, r

Example usage:

import numpy as np
import matplotlib.pyplot as plt

def fungsi_r4(theta, theta0, r0, a):
    return r0 * np.cos(theta - theta0) + np.sqrt((a ** 2) - (r0 ** 2) * (np.sin(theta - theta0) ** 2))

theta = np.linspace(0, 2 * np.pi, 100)
r = fungsi_r4(theta, 2.4, 5.1, 2.6)

ax = plt.subplot(projection=&#39;polar&#39;)
ax.plot(*flip_negative_radii(theta, r))
plt.show()

Plotting polar function using matplotlib

The gaps are due to the term in the square root going negative and producing NaNs. If possible, I would try to come up with a parametric representation of the function, which would avoid both of these problems.

huangapple
  • 本文由 发表于 2023年5月7日 19:41:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定