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

2024-04-20 02:30:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想弄清楚如何在极坐标图中的两点之间创建一条弧,但我画的直线是一条连接它们的直线,即使图是极坐标的。在

是否需要使用不同的绘图功能来代替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


Tags: inforplotwithnpfigpltax
1条回答
网友
1楼 · 发布于 2024-04-20 02:30:57

极轴投影意味着不再使用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

相关问题 更多 >