![](/img/trans.png)
[英]Scale matplotlib.pyplot.Axes.scatter markersize by x-scale
[英]Change x axes scale in matplotlib
我使用 Matlab 創建了這個圖
使用 matplotlib,x 軸繪制大數字,例如 100000、200000、300000。我想要 1、2、3 和 10^5 來表示它實際上是 100000、200000、300000。
有沒有一種簡單的方法可以在 matplotlib 中創建這樣的比例?
嘗試使用matplotlib.pyplot.ticklabel_format
:
import matplotlib.pyplot as plt
...
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
這將科學計數法(即ax 10^b
)應用於您的 x 軸刻度線
這與其說是對您原始問題的回答,不如說是對您在問題正文中提出的疑問之一的回答。
一點序言,這樣我的命名就不會奇怪了:
import matplotlib
from matplotlib import rc
from matplotlib.figure import Figure
ax = self.figure.add_subplot( 111 )
如前所述,您可以使用 ticklabel_format 指定 matplotlib 應該對大值或小值使用科學記數法:
ax.ticklabel_format(style='sci',scilimits=(-3,4),axis='both')
您可以使用 rcParams 中的標志(from matplotlib import rcParams)或直接設置它們來影響其顯示方式。 我還沒有找到比以下方法更優雅的在“1e”和“x10^”科學記數法之間轉換的方法:
ax.xaxis.major.formatter._useMathText = True
這應該會給你更多的 Matlab-esc,而且確實可以說是更好的外觀。 我認為以下應該做同樣的事情:
rc('text', usetex=True)
標量格式化程序支持收集指數。 文檔如下:
類 matplotlib.ticker.ScalarFormatter(useOffset=True, useMathText=False, useLocale=None) 基礎:matplotlib.ticker.Formatter
刻度位置是一個普通的舊數字。 如果 useOffset==True 並且數據范圍遠小於數據平均值,則將確定偏移量以使刻度標簽有意義。 科學計數法用於數據 < 10^-n 或數據 >= 10^m,其中 n 和 m 是使用 set_powerlimits((n,m)) 設置的功率限制。 這些的默認值由 axes.formatter.limits rc 參數控制。
你的技術是:
from matplotlib.ticker import ScalarFormatter
xfmt = ScalarFormatter()
xfmt.set_powerlimits((-3,3)) # Or whatever your limits are . . .
{{ Make your plot }}
gca().xaxis.set_major_formatter(xfmt)
要獲得以x10^5
格式顯示的指數,請使用useMathText=True
實例化 ScalarFormatter。
您還可以使用:
xfmt.set_useOffset(10000)
要得到這樣的結果:
我找到了簡單的解決方案
pylab.ticklabel_format(axis='y',style='sci',scilimits=(1,4))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.