O.rka
O.rka

Reputation: 30717

How to draw a curved line/arc in a polar plot with matplotlib?

I'm trying to figure out how to create an arc between 2 points in a polar plot but the line that I'm drawing is a straight line connecting them even though the plot is polar.

Is there a different plotting function I need to use instead of ax.plot?

I noticed there are patches in matplotlib which might be what I'm supposed to use but I'm not sure how to add them in this way.

How can I draw a curved line from point A and point B on the polar plot?

# 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

Upvotes: 4

Views: 4629

Answers (1)

Crivella
Crivella

Reputation: 1007

The polar projections means that you don't use the x,y coordinate system anymore, but the polar one. Nevertheless a plot between 2 points will still be a straight line between them.
What you want to do is define the arc yourself like this:

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

Upvotes: 6

Related Questions