Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit065769b

Browse files
authored
Merge pull request#16776 from anntzer/cursor-significant-digits
Make cursor text precision actually correspond to pointing precision.
2 parents5eef7b1 +bfb9cc7 commit065769b

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Cursor text now uses a number of significant digits matching pointing precision
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Previously, the x/y position displayed by the cursor text would usually include
5+
far more significant digits than the mouse pointing precision (typically one
6+
pixel). This is now fixed for linear scales.

‎lib/matplotlib/tests/test_ticker.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,16 @@ def test_scilimits(self, sci_type, scilimits, lim, orderOfMag, fewticks):
556556
tmp_form.set_locs(ax.yaxis.get_majorticklocs())
557557
assertorderOfMag==tmp_form.orderOfMagnitude
558558

559+
deftest_cursor_precision(self):
560+
fig,ax=plt.subplots()
561+
ax.set_xlim(-1,1)# Pointing precision of 0.001.
562+
fmt=ax.xaxis.get_major_formatter().format_data_short
563+
assertfmt(0.)=="0.000"
564+
assertfmt(0.0123)=="0.012"
565+
assertfmt(0.123)=="0.123"
566+
assertfmt(1.23)=="1.230"
567+
assertfmt(12.3)=="12.300"
568+
559569

560570
classFakeAxis:
561571
"""Allow Formatter to be called without having a "full" plot set up."""

‎lib/matplotlib/ticker.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
importlogging
169169
importlocale
170170
importmath
171+
fromnumbersimportIntegral
171172

172173
importnumpyasnp
173174

@@ -584,11 +585,42 @@ def set_powerlimits(self, lims):
584585

585586
defformat_data_short(self,value):
586587
# docstring inherited
588+
ifisinstance(value,np.ma.MaskedArray)andvalue.mask:
589+
return""
590+
ifisinstance(value,Integral):
591+
fmt="%d"
592+
else:
593+
ifself.axis.__name__in ["xaxis","yaxis"]:
594+
ifself.axis.__name__=="xaxis":
595+
axis_trf=self.axis.axes.get_xaxis_transform()
596+
axis_inv_trf=axis_trf.inverted()
597+
screen_xy=axis_trf.transform((value,0))
598+
neighbor_values=axis_inv_trf.transform(
599+
screen_xy+ [[-1,0], [+1,0]])[:,0]
600+
else:# yaxis:
601+
axis_trf=self.axis.axes.get_yaxis_transform()
602+
axis_inv_trf=axis_trf.inverted()
603+
screen_xy=axis_trf.transform((0,value))
604+
neighbor_values=axis_inv_trf.transform(
605+
screen_xy+ [[0,-1], [0,+1]])[:,1]
606+
delta=abs(neighbor_values-value).max()
607+
else:
608+
# Rough approximation: no more than 1e4 pixels.
609+
delta=self.axis.get_view_interval()/1e4
610+
# If e.g. value = 45.67 and delta = 0.02, then we want to round to
611+
# 2 digits after the decimal point (floor(log10(0.02)) = -2);
612+
# 45.67 contributes 2 digits before the decimal point
613+
# (floor(log10(45.67)) + 1 = 2): the total is 4 significant digits.
614+
# A value of 0 contributes 1 "digit" before the decimal point.
615+
sig_digits=max(
616+
0,
617+
(math.floor(math.log10(abs(value)))+1ifvalueelse1)
618+
-math.floor(math.log10(delta)))
619+
fmt=f"%-#.{sig_digits}g"
587620
return (
588-
""ifisinstance(value,np.ma.MaskedArray)andvalue.maskelse
589621
self.fix_minus(
590-
locale.format_string("%-12g", (value,))ifself._useLocaleelse
591-
"%-12g"%value))
622+
locale.format_string(fmt, (value,))ifself._useLocaleelse
623+
fmt%value))
592624

593625
defformat_data(self,value):
594626
# docstring inherited

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp