Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aadb2c4

Browse files
authored
Fixed PolarAxes not using fmt_xdata and added simple test (#4568) (#28306)
* Fixed PolarAxes not using fmt_xdata and added simple test (#4568)
1 parent 81756c3 commit aadb2c4

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/matplotlib/projections/polar.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,12 +1447,25 @@ def format_sig(value, delta, opt, fmt):
14471447
cbook._g_sig_digits(value, delta))
14481448
return f"{value:-{opt}.{prec}{fmt}}"
14491449

1450-
return ('\N{GREEK SMALL LETTER THETA}={}\N{GREEK SMALL LETTER PI} '
1451-
'({}\N{DEGREE SIGN}), r={}').format(
1450+
# In case fmt_xdata was not specified, resort to default
1451+
1452+
if self.fmt_ydata is None:
1453+
r_label = format_sig(r, delta_r, "#", "g")
1454+
else:
1455+
r_label = self.format_ydata(r)
1456+
1457+
if self.fmt_xdata is None:
1458+
return ('\N{GREEK SMALL LETTER THETA}={}\N{GREEK SMALL LETTER PI} '
1459+
'({}\N{DEGREE SIGN}), r={}').format(
14521460
format_sig(theta_halfturns, delta_t_halfturns, "", "f"),
14531461
format_sig(theta_degrees, delta_t_degrees, "", "f"),
1454-
format_sig(r, delta_r, "#", "g"),
1462+
r_label
14551463
)
1464+
else:
1465+
return '\N{GREEK SMALL LETTER THETA}={}, r={}'.format(
1466+
self.format_xdata(theta),
1467+
r_label
1468+
)
14561469

14571470
def get_data_ratio(self):
14581471
"""

lib/matplotlib/tests/test_polar.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,33 @@ def test_cursor_precision():
436436
assert ax.format_coord(2, 1) == "θ=0.637π (114.6°), r=1.000"
437437

438438

439+
def test_custom_fmt_data():
440+
ax = plt.subplot(projection="polar")
441+
def millions(x):
442+
return '$%1.1fM' % (x*1e-6)
443+
444+
# Test only x formatter
445+
ax.fmt_xdata = None
446+
ax.fmt_ydata = millions
447+
assert ax.format_coord(12, 2e7) == "θ=3.8197186342π (687.54935416°), r=$20.0M"
448+
assert ax.format_coord(1234, 2e6) == "θ=392.794399551π (70702.9919191°), r=$2.0M"
449+
assert ax.format_coord(3, 100) == "θ=0.95493π (171.887°), r=$0.0M"
450+
451+
# Test only y formatter
452+
ax.fmt_xdata = millions
453+
ax.fmt_ydata = None
454+
assert ax.format_coord(2e5, 1) == "θ=$0.2M, r=1.000"
455+
assert ax.format_coord(1, .1) == "θ=$0.0M, r=0.100"
456+
assert ax.format_coord(1e6, 0.005) == "θ=$1.0M, r=0.005"
457+
458+
# Test both x and y formatters
459+
ax.fmt_xdata = millions
460+
ax.fmt_ydata = millions
461+
assert ax.format_coord(2e6, 2e4*3e5) == "θ=$2.0M, r=$6000.0M"
462+
assert ax.format_coord(1e18, 12891328123) == "θ=$1000000000000.0M, r=$12891.3M"
463+
assert ax.format_coord(63**7, 1081968*1024) == "θ=$3938980.6M, r=$1107.9M"
464+
465+
439466
@image_comparison(['polar_log.png'], style='default')
440467
def test_polar_log():
441468
fig = plt.figure()

0 commit comments

Comments
 (0)