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

Conversation

klorine28
Copy link

@klorine28 klorine28 commented Jul 6, 2023

PR summary

PR checklist

@tacaswell
Copy link
Member

Thank you for your interest in contributing to Matplotilb, however this PR does not address the issue in #4568 .

The core issue is that for Cartesian plots there are functions you can set to control how the mouse over reports (see https://matplotlib.org/stable/gallery/misc/coords_report.html for the updated version of the docs, you will need to use an interactive backend (not matplotlib-inline in a notebook) to see it do anything) the x and y values. The issue reports that those functions are ignored on polar axes (see

def format_coord(self, x, y):
"""Return a format string formatting the *x*, *y* coordinates."""
return "x={} y={}".format(
"???" if x is None else self.format_xdata(x),
"???" if y is None else self.format_ydata(y),
)
and
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}}"
return ('\N{GREEK SMALL LETTER THETA}={}\N{GREEK SMALL LETTER PI} '
'({}\N{DEGREE SIGN}), r={}').format(
format_sig(theta_halfturns, delta_t_halfturns, "", "f"),
format_sig(theta_degrees, delta_t_degrees, "", "f"),
format_sig(r, delta_r, "#", "g"),
)
for updated perma-links from #4568 (comment) ).

The proposed fixes are to do one of the following:

  • add fmt_r and fmt_theta as analogs to the Cartesian versions
  • update the format_coords method on polar axes to look at the fmt_xdata and fmt_ydata methods

See the discussion in the issue for pros and cons of each. The rough consensus seemed to be towards the second option.

@klorine28
Copy link
Author

I am unusre if my solution is of any help as it was not clear to me if the goal was to leave a possibe example or make a standarization to solve an underlying issue. i did however manage to get the example code working with the bits that were asked for.

@klorine28
Copy link
Author

so the issue is that interactive ploting features are ignored when the plot is polar? and the goal of the issue is to modify the code above to make it so that these interactive plotting functions are not ignored?

This modification uses the fmt_xdata and fmt_ydata methods by retrieving the corresponding formatters (fmt_theta and fmt_r) from the x-axis and y-axis, respectively, to format the theta and r values in the format_coord method.
@klorine28
Copy link
Author

is the change I made better?

@jklymak
Copy link
Member

jklymak commented Jul 9, 2023

@klorine28 ive not followed the suggestion here fully. However this is not in the form of either an example (galleries/examples) or what I think was being asked for, which is a modification to the polar axes that would display r and theta by default.

@klorine28
Copy link
Author

I apologise for this is my first interaction with open source that is not documentation. So what I am trying to do is modify the format coords so that r and theta are displayed by default? What I understood from the issue was that in cartesian plots, you can see the info of a point by interacting with it, but this is ignored in polar plots. As such, the solution would be to make it so that when you hover over a point in a polar plot, it displays theta and r. Is that correct?

@klorine28
Copy link
Author

any coment on my question? is the second commit what is wanted?

@melissawm
Copy link
Member

Hi @klorine28 - you may get more attention here if you rename your PR to something more descriptive, for example something like "Display r and theta on hover for polar plots" (please choose the wording that makes more sense to you). In addition, you can then add "Closes #4568" to the description of your PR to make sure it is linked to the original issue here in the GitHub web interface.

Beyond that, there are also a couple of Linting errors that you can fix while you wait for a review: https://github.com/matplotlib/matplotlib/actions/runs/5486484810/job/14858128627?pr=26271

Thanks!

@klorine28 klorine28 changed the title possible solve for issue #4568 Display r and theta on hover for polar plots Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for author
Development

Successfully merging this pull request may close these issues.

4 participants