Annotation in a plotly polar plot

202 views Asked by At

I'm trying to add annotations arrows to a plotly polar plot, but that produces another underlying cartesian plot:

arrow added to cartesian plot not polar

Question 1: is there a way to add annotation to polar plots? This page of 2019 suggest that not, is it still the current status?: https://community.plotly.com/t/adding-annotations-to-polar-scatterplots/704

Question 2: is there a hack to produce arrows on a polar plot? (hide aligned overlying transparent cartesian plot? low level draw of arrows with lines in polar plot?)

The code producing the above image is here:

import plotly.express as px
import plotly.graph_objects as go

import pandas as pd

df = pd.DataFrame()

fig = px.scatter_polar(
    df,
    theta=None,
    r=None,
    range_theta=[-180, 180],
    start_angle=0,
    direction="counterclockwise",
    template="plotly_white",
)

x_end = [1, 2, 2]
y_end = [3, 5, 4]
x_start = [0, 1, 3]
y_start = [4, 4, 4]

list_of_all_arrows = []
for x0, y0, x1, y1 in zip(x_end, y_end, x_start, y_start):
    arrow = go.layout.Annotation(dict(
        x=x0,
        y=y0,
        xref="x", yref="y",
        text="",
        showarrow=True,
        axref="x", ayref='y',
        ax=x1,
        ay=y1,
        arrowhead=3,
        arrowwidth=1.5,
        arrowcolor='rgb(255,51,0)', )
    )
    list_of_all_arrows.append(arrow)

fig.update_layout(annotations=list_of_all_arrows)
fig.show()

Edit:

In fine, I would like to have something like that, with annotation relative to polar plot: enter image description here

1

There are 1 answers

1
Aradhna Sonia On

Here is what I coded.

# Import modules
import numpy as np 
import matplotlib.pyplot as plt 
# ------------------------------------ # 
  
# Define the figure
fig = plt.figure(figsize=(16,16))
# Setting the axes projection as polar 
ax = fig.add_subplot(111, projection='polar')

# List of angles to put the arrows
thetas = [30.0, 10.0, -20.0]

# List that stores the color of each arrow
colors = ["yellow", "cyan", "blue"]

# Iterate through the list
for i in range(len(thetas)):
        # Convert to radians
        theta = np.deg2rad(thetas[i])

        # Draw the arrow
        # 
        # plt.arrow(theta_b, r_b, theta_e, r_e)
        # The polar coordinates of the beginning point of arrow is (theta_b, r_b)
        # The polar coordinates of the end point of arrow is (theta_a, r_a)
        # 
        # These values that I have set so that the arrow points towards the center 
        # as you requested. You can play with them to obtain different results.
        #
        plt.arrow(theta, 0.1, 0.0, -0.25,  \
                  width=0.015, lw=3, head_width=0.4, \
                  head_length=0.02, color=colors[i], ec=colors[i]) 


# Annotate does not work. You can use text though
# plt.text(np.pi/6.0, 0.02, "Text")   

# Remove tick labels
ax.set_xticklabels([])
ax.set_yticklabels([])

# Display the plot
plt.show()

This yields, Arrows in polar coords

I hope I have answered your question.