I am using ax.pcolor() to create a plot in polar coordinates, and I would like to add a radial axis-like line at a fixed angle (e.g., 250 degrees).
Specifically, I would like the axis-like line to:
point along the radial direction;
have an arrowhead at the end;
have radial ticks with a consistent visual length;
be gray;
optionally extend slightly beyond the current radial limit.
I can draw the arrow using:
axs[0].annotate('', xy = (_phi, rs[-1]), xytext = (_phi, rs[0]),
arrowprops = dict(arrowstyle = '->', color = 'gray', lw = 1.5))
_annotation.arrow_patch.set_clip_on(False)
But `annotate` does not connect starting and end points exactly as instructed (it include a shift automatically). Also if I extend the endpoint beyond the current radial limit, the line disappears.
I have also tried `FancyArrowPatch` from `matplotlib.patches`, but it ruins the entire figure:
p_0 = ax.transData.transform((_phi, rs[0]))
p_1 = ax.transData.transform((_phi, rs[-1]))
arrow = FancyArrowPatch(p_0, p_1, arrowstyle = '->', mutation_scale = 12, color='gray', lw = 1.2)
axs[0].add_patch(arrow)
What would be the recommended matplotlib approach for creating such a radial axis with an arrow, while optionally allowing it to extend beyond the polar axes? Thanks in advance!
My MWE and its outcome is attached:
#!/usr/bin/env python
import copy
import numpy as np
import os, sys
import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'
matplotlib.rc('text', usetex = True)
from matplotlib import pyplot as py
from matplotlib.patches import FancyArrowPatch
def plot():
## setup canvas
n_columns, n_rows = 1, 1
figure = py.figure(figsize = (n_columns * 7.5, n_rows * 5.0))
axs = [py.subplot(n_rows, n_columns, _ + 1, projection = 'polar') for _ in range(1)]
## get r and phi values
rs = np.linspace(0.0, 3.0, 50)
phis = np.linspace(0.0, 2.0 * np.pi, 100)
## make plot
r_grid, phi_grid = np.meshgrid(rs, phis)
y_grid = []
for phi in phis:
_y_grid = [np.cos(phi) * qt for qt in rs]
y_grid.append(np.array(_y_grid))
axs[0].pcolor(phi_grid, r_grid, y_grid, cmap = 'bwr')
## draw radial axis
_phi = np.deg2rad(250.0)
# _annotation = axs[0].annotate('', xy = (_phi, 1.2 * rs[-1]), xytext = (_phi, rs[0]),
# arrowprops = dict(arrowstyle = '->', color = 'gray', lw = 1.5))
_annotation = axs[0].annotate('', xy = (_phi, rs[-1]), xytext = (_phi, rs[0]),
arrowprops = dict(arrowstyle = '->', color = 'gray', lw = 1.5))
_annotation.arrow_patch.set_clip_on(False)
for _r in [1.0, 2.0, 3.0]:
d_phi = np.deg2rad(3.0) / _r
axs[0].plot([_phi - d_phi, _phi + d_phi], [_r, _r], color = 'gray', lw = 0.8, zorder = 5)
axs[0].grid(False)
axs[0].set_xticklabels([])
axs[0].set_yticklabels([])
## save figure
name = './test.pdf'
py.subplots_adjust(wspace = 0.0)
py.savefig(name, bbox_inches = 'tight')
py.close()
return name
if __name__ == '__main__':
plot()
