[英]matplotlib axis tick labels: getting mantissa and exponent
有沒有辦法分別得到軸刻度標簽的尾數和指數,以便我可以操縱它們的顯示方式? 我需要它們看起來像“5x10 -9 ”而不是通常的科學記譜法“5.0e-9”。
如果我在gnuplot中這樣做,我會做類似的事情
set format y "%2.0t{/Symbol \327}10^{%L}"
%2.0t讓我得到了尾數,而%L讓我得到了指數。 我怎樣才能在matplotlib中做同樣的事情?
謝謝
Matplotlib使用所謂的定位器和格式化程序,它們(獨立地)定位刻度並標記它們( 文檔 )。 標准的ScalarFormatter
需要一個額外的關鍵字參數( useMathText
),它完全符合您的要求。
要使用它:
# import the tickers
import matplotlib.ticker as mticker
# your plotting code
# create a new formatter and use it on the y-axis
formatter = mticker.ScalarFormatter(useMathText=True)
ax.yaxis.set_major_formatter(formatter)
我在這里稍微詳細地回答了一個類似的問題
那么使用str.split
:
In [242]: x
Out[242]: 5e-09
In [243]: '{}*10^{}'.format(*map(int, str(x).split('e')))
Out[243]: '5*10^-9'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.