Skip to content

Fixed PolarAxes not using fmt_xdata and added simple test (#4568) #28306

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

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 16 additions & 3 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,12 +1447,25 @@ def format_sig(value, delta, opt, fmt):
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(
# In case fmt_xdata was not specified, resort to default

if self.fmt_ydata is None:
r_label = format_sig(r, delta_r, "#", "g")
else:
r_label = self.format_ydata(r)

if self.fmt_xdata is None:
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"),
r_label
)
else:
return '\N{GREEK SMALL LETTER THETA}={}, r={}'.format(
self.format_xdata(theta),
r_label
)

def get_data_ratio(self):
"""
Expand Down
27 changes: 27 additions & 0 deletions lib/matplotlib/tests/test_polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,33 @@ def test_cursor_precision():
assert ax.format_coord(2, 1) == "θ=0.637π (114.6°), r=1.000"


def test_custom_fmt_data():
ax = plt.subplot(projection="polar")
def millions(x):
return '$%1.1fM' % (x*1e-6)

# Test only x formatter
ax.fmt_xdata = None
ax.fmt_ydata = millions
assert ax.format_coord(12, 2e7) == "θ=3.8197186342π (687.54935416°), r=$20.0M"
assert ax.format_coord(1234, 2e6) == "θ=392.794399551π (70702.9919191°), r=$2.0M"
assert ax.format_coord(3, 100) == "θ=0.95493π (171.887°), r=$0.0M"

# Test only y formatter
ax.fmt_xdata = millions
ax.fmt_ydata = None
assert ax.format_coord(2e5, 1) == "θ=$0.2M, r=1.000"
assert ax.format_coord(1, .1) == "θ=$0.0M, r=0.100"
assert ax.format_coord(1e6, 0.005) == "θ=$1.0M, r=0.005"

# Test both x and y formatters
ax.fmt_xdata = millions
ax.fmt_ydata = millions
assert ax.format_coord(2e6, 2e4*3e5) == "θ=$2.0M, r=$6000.0M"
assert ax.format_coord(1e18, 12891328123) == "θ=$1000000000000.0M, r=$12891.3M"
assert ax.format_coord(63**7, 1081968*1024) == "θ=$3938980.6M, r=$1107.9M"


@image_comparison(['polar_log.png'], style='default')
def test_polar_log():
fig = plt.figure()
Expand Down
Loading