首页 > 解决方案 > 如何使用 matplotlib 在极坐标图中绘制曲线/弧线?

问题描述

我试图弄清楚如何在极坐标图中的 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])

在此处输入图像描述

标签: pythonmatplotlibfigurepolar-coordinates

解决方案


极坐标投影意味着您不再使用 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()

在此处输入图像描述


推荐阅读