12

では、1 つの軸で目盛りmatplotlibを使用するとlog、その軸に大きな目盛りがなく小さな目盛りのみが表示されることがあります。したがって、これは軸全体のラベルが表示されないことを意味します。

マイナー ティックにもラベルが必要であることを指定するにはどうすればよいですか?

私は試した:

plt.setp(ax.get_xticklabels(minor=True), visible=True)

...しかし、それはうまくいきませんでした。

4

3 に答える 3

9

対数プロットでマイナー ティックが適切に機能するように、さまざまな方法を試しました。ティックの値のログを表示しても問題ない場合は、 を使用できますmatplotlib.ticker.LogFormatterExponent。試しmatplotlib.ticker.LogFormatterたことは覚えていますが、あまり好きではありませんでした。よく覚えていれば、すべてをbase^exp(0.1、0、1 も) 入れます。どちらの場合も(他のすべての場合と同様に) 、マイナーティックを取得するmatplotlib.ticker.LogFormatter*ように設定する必要があります。labelOnlyBase=False

カスタム関数を作成して使用することになりましたmatplotlib.ticker.FuncFormatter。私のアプローチでは、ティックが整数値であり、基数 10 のログが必要であると想定しています。

from matplotlib import ticker
import numpy as np

def ticks_format(value, index):
    """
    get the value and returns the value as:
       integer: [0,99]
       1 digit float: [0.1, 0.99]
       n*10^m: otherwise
    To have all the number of the same size they are all returned as latex strings
    """
    exp = np.floor(np.log10(value))
    base = value/10**exp
    if exp == 0 or exp == 1:   
        return '${0:d}$'.format(int(value))
    if exp == -1:
        return '${0:.1f}$'.format(value)
    else:
        return '${0:d}\\times10^{{{1:d}}}$'.format(int(base), int(exp))

subs = [1.0, 2.0, 3.0, 6.0]  # ticks to show per decade
ax.xaxis.set_minor_locator(ticker.LogLocator(subs=subs)) #set the ticks position
ax.xaxis.set_major_formatter(ticker.NullFormatter())   # remove the major ticks
ax.xaxis.set_minor_formatter(ticker.FuncFormatter(ticks_format))  #add the custom ticks
#same for ax.yaxis

大目盛りを削除せず、大目盛りsubs = [2.0, 3.0, 6.0]と小目盛りのフォントサイズが違うのを使うと(これは my での使用が原因かもしれません)text.usetex:Falsematplotlibrc

于 2013-06-20T09:16:57.630 に答える