2011-07-04 4 views
11

私はプロットを作成しようとしていますが、ログスケールが上記のように表示されているように表示するようにしておきます。私は50、500、2000の小目盛りを表示したいだけです。とにかく小目盛ラベルを指定して表示しますか?私は少しこれを把握しようとしているが、良い解決策を見つけていない。私が考えることができるのは、minorticklabels()を取得し、fontsizeを0に設定することです。これはコードの最初のスニペットの下に示されています。もっと清潔な解決策があることを願っていました。Matplotlibので、ログ軸には指定された点に小さな目盛りのラベルしかありません。また、カラーバーの目盛りラベルのサイズを変更してください

もう1つは、私が把握していないカラーバーのティックラベルのサイズを変更しています。誰かがこれを行う方法を知っていれば私に知らせてください私は簡単にこれを行うカラーバーのメソッドが表示されないため。

まずコード:

fig = figure(figto) 
ax = fig.add_subplot(111) 
actShape = activationTrace.shape 
semitones = arange(actShape[1]) 
freqArray = arange(actShape[0]) 
X,Y = meshgrid(self.testFreqArray,self.testFreqArray) 
Z = sum(activationTrace[:,:,beg:end],axis=2) 
surf = ax.contourf(X,Y,Z, 8, cmap=cm.jet) 
ax.set_position([0.12,0.15,.8,.8]) 
ax.set_ylabel('Log Frequency (Hz)') 
ax.set_xlabel('Log Frequency (Hz)') 
ax.set_xscale('log') 
ax.set_yscale('log') 
ax.xaxis.set_minor_formatter(FormatStrFormatter('%d')) 
ax.yaxis.set_ticks_position('left') 
ax.xaxis.set_ticks_position('bottom') 
ax.tick_params(axis='both',reset=False,which='both',length=8,width=2) 
self.plotSetAxisLabels(ax,22) 
self.plotSetAxisTickLabels(ax,18) 
cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02) 
cbar.set_label('Activation',size=18) 
return ax, cbar 

enter image description here

セカンドコード:カラーバーの場合

fig = figure(figto) 
ax = fig.add_subplot(111) 
actShape = activationTrace.shape 
semitones = arange(actShape[1]) 
freqArray = arange(actShape[0]) 
X,Y = meshgrid(self.testFreqArray,self.testFreqArray) 
Z = sum(activationTrace[:,:,beg:end],axis=2) 
surf = ax.contourf(X,Y,Z, 8, cmap=cm.jet) 
ax.set_position([0.12,0.15,.8,.8]) 
ax.set_ylabel('Log Frequency (Hz)') 
ax.set_xlabel('Log Frequency (Hz)') 
ax.set_xscale('log') 
ax.set_yscale('log') 
ax.xaxis.set_minor_formatter(FormatStrFormatter('%d')) 
ax.yaxis.set_minor_formatter(FormatStrFormatter('%d')) 
ax.yaxis.set_ticks_position('left') 
ax.xaxis.set_ticks_position('bottom') 
ax.tick_params(axis='both',reset=False,which='both',length=8,width=2) 
self.plotSetAxisLabels(ax,22) 
self.plotSetAxisTickLabels(ax,18) 
cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02) 
cbar.set_label('Activation',size=18) 
count = 0 
for i in ax.xaxis.get_minorticklabels(): 
    if (count%4 == 0): 
     i.set_fontsize(12) 
    else: 
     i.set_fontsize(0) 
    count+=1 
for i in ax.yaxis.get_minorticklabels(): 
    if (count%4 == 0): 
     i.set_fontsize(12) 
    else: 
     i.set_fontsize(0) 
    count+=1 
return ax, cbar 

enter image description here

: あなたが気にしない場合は、別の簡単な質問しようとしているためそれを理解するためにb utは完全にはわかりません。私はScalarFormatterで得ることができる科学的表記法を使用したいと思います。小数点以下の桁数と乗数を設定するにはどうすればよいですか?私は8x10^8または.8x10^9のようになり、スペースを節約する代わりに、それらのゼロをすべて入れてください。 Axesオブジェクトの内部でこれを行う方法は複数ありますが、何が最良の方法かと思います。 ScalarFormatterに変更するときに表記を変更する方法を理解できません。

グラフの場合: また、私のデータのポイントは46で始まり、次にそれに2 ^(1/12)を掛けたものが46,49,50,55,58,61 ... 3132 。これらはすべて丸められていますが、2 ^(1/12)の近くにあります。私は主要なティッカーをこれらの数字の近くに配置する方がよいと決めました。固定フォーマッタを使用し、freqArrayで15秒ごとにティッカーを使用する最良の方法です。その後、他のすべての周波数でマイナーティッカーを使用します。これを行い、ログ軸を維持することはできますか?

答えて

80

1)FixedLocatorを使用して、明示的なティックの位置を静的に定義します。

2)カラーバーcbarには、目盛りの書式設定を含む通常の軸方法にアクセスできるようになるax属性があります。

import numpy as np 
import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = fig.add_subplot(111) 
x = np.arange(10,3000,100) 
y = np.arange(10,3000,100) 
X,Y = np.meshgrid(x,y) 
Z = np.random.random(X.shape)*8000000 
surf = ax.contourf(X,Y,Z, 8, cmap=plt.cm.jet) 
ax.set_ylabel('Log Frequency (Hz)') 
ax.set_xlabel('Log Frequency (Hz)') 
ax.set_xscale('log') 
ax.set_yscale('log') 
ax.xaxis.set_minor_formatter(plt.FormatStrFormatter('%d')) 
# defining custom minor tick locations: 
ax.xaxis.set_minor_locator(plt.FixedLocator([50,500,2000])) 
ax.yaxis.set_ticks_position('left') 
ax.xaxis.set_ticks_position('bottom') 
ax.tick_params(axis='both',reset=False,which='both',length=8,width=2) 
cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02) 
cbar.set_label('Activation',size=18) 
# access to cbar tick labels: 
cbar.ax.tick_params(labelsize=5) 
plt.show() 

enter image description here

編集

あなたはダニmarlsをしたいが、あなたは選択のラベルを表示したい場合は、私はset_visibleの代わりに使用することがありますを除いて、私は、あなたの反復と間違って何も見えませんフォントサイズをゼロにします。

あなたは、あなたがそれを示しますかどうかを決定する目盛りの値や位置を使用することができますFuncFormatter使用して、より細かい制御を楽しむことがあります

def show_only_some(x, pos): 
    s = str(int(x)) 
    if s[0] in ('2','5'): 
     return s 
    return '' 

ax.xaxis.set_minor_formatter(plt.FuncFormatter(show_only_some)) 
+0

これは良いものです。私はここでset_visibleを使用するとは思っていませんでしたが、フォントをサイズ0に設定するよりも優れています。これは、フォントを少なくとも1つのピクセルにするためです。私はそこに目盛りが欲しいので、FixedLocatorはこの場合は動作しませんが、私はFuncFormatterの外観が好きです。私は今のところ持っているものに固執しますが、次回はそれを与えるかもしれません。 FuncFormatterの場合、常に2つのパラメータがその順序(値、ティックアップ)で使用されます。 –

+0

すでにFuncFormatterについての私自身の質問に答えていますので、心配はありません。ありがとう。実際にはFuncFormatterが実装されたばかりなので、将来的に変更が必要な場合は非常に簡単で極端な柔軟性があるため、実際の作業は簡単です。 –

+0

ようこそ。喜んで助けてください – Paul

0

私は次の関数を作成し@Paulからの回答をもとに:

def get_formatter_function(allowed_values, datatype='float'): 
    """returns a function, which only allows allowed_values as axis tick labels""" 
    def hide_others(value, pos): 
     if value in allowed_values: 
      if datatype == 'float': 
       return value 
      elif datatype == 'int': 
       return int(value) 
     return '' 
    return hide_others 

これは少し柔軟です。

関連する問題