如何使用matplotlib在极坐标图中绘制曲线/弧?

时间:2018-12-11 18:52:21

标签: python matplotlib figure polar-coordinates

我试图找出如何在极坐标图中的2个点之间创建圆弧,但是我所画的线是连接它们的直线,即使该图是极坐标的。

是否需要使用其他绘图功能而不是ax.plot

我注意到matplotlib中有一些补丁,可能是我应该使用的补丁,但是我不确定如何以这种方式添加它们。

如何在极坐标图上从A点和B点绘制一条曲线?

# Create polar plot object
with plt.style.context("seaborn-white"):
    fig = plt.figure(figsize=(5,5))
    ax = fig.add_subplot(111, projection="polar")
    # Draw 3 lines
    for degree in [90, 210, 330]:
        rad = np.deg2rad(degree)
        ax.plot([rad,rad], [0,1], color="black", linewidth=2)
    # Connect two points with a curve
    for curve in [[[90, 210], [0.5, 0.8]]]:
        curve[0] = np.deg2rad(curve[0])
        ax.plot(curve[0], curve[1])

enter image description here

1 个答案:

答案 0 :(得分:2)

极坐标投影意味着您不再使用x,y坐标系,而是极坐标投影。不过,两点之间的关系仍然是它们之间的直线。
您要做的就是自己定义弧:

from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
import numpy as np

with plt.style.context("seaborn-white"):
    fig = plt.figure(figsize=(5,5))
    ax = fig.add_subplot(111, projection="polar")
    # Draw 3 lines
    for degree in [90, 210, 330]:
        rad = np.deg2rad(degree)
        ax.plot([rad,rad], [0,1], color="black", linewidth=2)
    # Connect two points with a curve
    for curve in [[[90, 210], [0.5, 0.8]]]:
        curve[0] = np.deg2rad(curve[0])
        x = np.linspace( curve[0][0], curve[0][1], 500)
        y = interp1d( curve[0], curve[1])( x)
        ax.plot(x, y)

plt.show()

enter image description here

相关问题