简体   繁体   English

如何在Pyplot注释中设置有效数字

[英]How to set significant figures in pyplot annotation

P-values and D-Values from a KS test need to be displayed on a pylot histogram. KS测试的P值和D值需要显示在Pylot直方图中。 Thankfully, my p-values are low, but are so low they display as 0.0 when I use round . 幸运的是,我的p值很低,但是很低,当我使用round时它们显示为0.0 I also tried numpy.set_printoptions(precision=3) to limit my significant digits to the thousands place, but this only changed my output printing to the interpreter, and not to the graph. 我还尝试了numpy.set_printoptions(precision=3)将有效数字限制在数千位,但这仅将输出打印更改为解释器,而不更改为图形。

 ks_D, ks_p = ks_2samp(cntrl, test)

 ks_summ = "K-S D-value = {}\nK-S p-value = {}".format(round(ks_D, 3), round(ks_p, 3))
pyplot.annotate(ks_summ, xy=(0.70, 0.75), xycoords='axes fraction')

So how can I display 1.2228387401e-24 ( <class 'numpy.float64'> ) as 1.223e-24 in a graph annotation? 那么如何在图形注释1.2228387401e-24<class 'numpy.float64'> )显示为1.223e-24呢?

Here is my graph with p-value showing as 0.0: 这是我的图,p值显示为0.0: 在此处输入图片说明

You need modify the line 您需要修改行

ks_summ = "K-S D-value = {}\nK-S p-value = {}".format(round(ks_D, 3), round(ks_p, 3))

to read 读书

ks_summ = "K-S D-value = {}\nK-S p-value = {:.3e}".format(round(ks_D, 3), ks_p)

The {:.3e} format means 3 decimal places with exponential notation. {:.3e}格式表示以指数表示的3个小数位。 And since the rounding to 3 decimal places in included in this formatting notation, we can drop the code to round ks_p . 而且由于此格式表示法中的四舍五入到小数点后三位,我们可以将代码舍入到ks_p For example: 例如:

>>> ks_p = 1.2228387401e-24
>>> '{:.3e}'.format(ks_p)
'1.223e-24'

Finally, note that you can apply a string format to ks_D , as well. 最后,请注意,您也可以将字符串格式应用于ks_D Something {:.3f} may do the trick (ie three decimal place rounding). {:.3f}可能会解决问题(即,将小数点后三位四舍五入)。

>>> ks_D = 0.38210101
>>> '{:.3f}'.format(ks_D)
'0.382'   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM