Skip to content

Display r and theta on hover for polar plots #26271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions issue#4568.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def format_coord(self, theta, r):
# docstring inherited
screen_xy = self.transData.transform((theta, r))
screen_xys = screen_xy + np.stack(
np.meshgrid([-1, 0, 1], [-1, 0, 1])).reshape((2, -1)).T
ts, rs = self.transData.inverted().transform(screen_xys).T
delta_t = abs((ts - theta + np.pi) % (2 * np.pi) - np.pi).max()
delta_t_halfturns = delta_t / np.pi
delta_t_degrees = delta_t_halfturns * 180
delta_r = abs(rs - r).max()
if theta < 0:
theta += 2 * np.pi
theta_halfturns = theta / np.pi
theta_degrees = theta_halfturns * 180

# See ScalarFormatter.format_data_short. For r, use #g-formatting
# (as for linear axes), but for theta, use f-formatting as scientific
# notation doesn't make sense and the trailing dot is ugly.
def format_sig(value, delta, opt, fmt):
# For "f", only count digits after decimal point.
prec = (max(0, -math.floor(math.log10(delta))) if fmt == "f" else
cbook._g_sig_digits(value, delta))
return f"{value:-{opt}.{prec}{fmt}}"
#retrieves corresponding formatting information to properly plot on the polar plot.
fmt_theta = self.xaxis.get_major_formatter().format_data
fmt_r = self.yaxis.get_major_formatter().format_data

return ('\N{GREEK SMALL LETTER THETA}={}\N{GREEK SMALL LETTER PI} '
'({}\N{DEGREE SIGN}), r={}').format(
format_sig(fmt_theta(theta), delta_t_halfturns, "", "f"),
format_sig(theta_degrees, delta_t_degrees, "", "f"),
format_sig(fmt_r(r), delta_r, "#", "g"),
)