2017-05-11 3 views
2

ログスケールされた軸で広範囲にプロットしようとしていますが、10^{ - 1}、10^0、10^1をちょうど0.1,1、 ScalarFormatterは、科学的表記の代わりにすべてを整数に変更しますが、ほとんどの目盛りラベルを科学的にしたいと思います。私はラベルのいくつかを変更したいだけです。そうMWEはLogFormatterは科学的な書式の限界に目盛を付けます

import numpy as np 
import matplotlib as plt 
fig = plt.figure(figsize=[7,7]) 
ax1 = fig.add_subplot(111) 
ax1.set_yscale('log') 
ax1.set_xscale('log') 
ax1.plot(np.logspace(-4,4), np.logspace(-4,4)) 
plt.show() 

あり、Iは、0.1、1、10の代わりに10 ^を読み取るために各軸の中間ラベルを必要{ - 1}のいずれかのために、10^0,10^1

おかげ助けて!

答えて

2

set_xscale('log')を設定すると、LogFormatterSciNotationScalarFormatterではなく)を使用しています。 LogFormatterSciNotationをサブクラス化して、ティックとしてマークされた場合は、希望の値0.1,1,10を返すことができます。

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.ticker import LogFormatterSciNotation 

class CustomTicker(LogFormatterSciNotation): 
    def __call__(self, x, pos=None): 
     if x not in [0.1,1,10]: 
      return LogFormatterSciNotation.__call__(self,x, pos=None) 
     else: 
      return "{x:g}".format(x=x) 


fig = plt.figure(figsize=[7,7]) 
ax = fig.add_subplot(111) 
ax.set_yscale('log') 
ax.set_xscale('log') 
ax.plot(np.logspace(-4,4), np.logspace(-4,4)) 

ax.xaxis.set_major_formatter(CustomTicker()) 
plt.show() 

enter image description here


更新は:matplotlibの2.1では new option

がLogFormatterMathtextが今指定するオプションが含まれてLogFormatterMathtext
のためのスカラーとしてフォーマットする最小値を指定して、今がありますスカラーとしてフォーマットするための最小値指数(すなわち、10 -3の代わりに0.001) 。

これはrcParams(plt.rcParams['axes.formatter.min_exponent'] = 2)を使用して、以下のように行うことができる。

import numpy as np 
import matplotlib.pyplot as plt 
plt.rcParams['axes.formatter.min_exponent'] = 2 

fig = plt.figure(figsize=[7,7]) 
ax = fig.add_subplot(111) 
ax.set_yscale('log') 
ax.set_xscale('log') 
ax.plot(np.logspace(-4,4), np.logspace(-4,4)) 

plt.show() 

これは、上記と同様のプロットが得られます。

ただし、この制限は対称的で、1と10だけを設定できますが、0.1では設定できません。したがって、初期の解決策はより一般的です。

+0

ありがとう、これは素晴らしいです、私はあなたの答えを受け入れ、それに応じてタイトルを変更しました! – user1451632

関連する問題