在matplotlib中用“X”代替“e”科学记数法显示数字

时间:2015-07-16 11:54:05

标签: python matplotlib

使用matplotlib,我想在我的图上写文字,以正常的科学记数法显示,例如1.92x10 -7 而不是默认的1.92e-7。我已经找到了如何为轴上的数字标记刻度而不是文本功能的帮助。以下是我想要更改的代码示例:

<div class="listing_items">
  <!-- listing item 1 -->
  <div class="listing_display">
    <div class="listing_display_inner">
      <img src="http://lorempixel.com/output/sports-q-c-380-380-10.jpg">

      <div class="listing_item_wrapper">
        <div class="listing_item_description_1">
          <div class="listing_item_square"></div>
          <a href="#" class="listing_item_title">An incredbible baseball display</a>
        </div>
        <div class="listing_item_description_2">
          <a href="#" class="listing_item_city">Zurich</a>
          <a href="#" class="listing_item_country">Switzerland</a>
        </div>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:8)

这样做的一个简单的方法是从Python字符串表示中为数字构建自己的tex字符串。通过下面定义的as_si,你的号码和小数位数,它将产生这个tex字符串:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,0.5)
y = x*(1.0-x)

def as_si(x, ndp):
    s = '{x:0.{ndp:d}e}'.format(x=x, ndp=ndp)
    m, e = s.split('e')
    return r'{m:s}\times 10^{{{e:d}}}'.format(m=m, e=int(e))

a=1.92e-7

plt.figure()
plt.plot(x, y)

plt.text(0.01, 0.23, r"$a = {0:s}$".format(as_si(a,2)), size=20)
plt.show()

enter image description here