2012-02-03 5 views
4

私は、左と下の軸だけを持つmatplotlibプロットを作成したいと思います。それらのそれぞれが独自に取り組む上軸と右軸を同時に削除し、ティックを外向きにプロットする方法はありますか?

が、残念ながら、両方のソリューションは、それぞれに互換性がないように見える:私は別に、両方の話題を扱う二つの質問を見つけましたその他。

:いくつかの時間のために私の頭を叩いた後、私は

を言うthe axes_grid documentationに警告が

「をいくつかのコマンド(主にダニ関連)が動作しない」これは私が持っているコードですました(?あるいはハック)

from matplotlib.pyplot import * 
from mpl_toolkits.axes_grid.axislines import Subplot 
import matplotlib.lines as mpllines 
import numpy as np 

#set figure and axis 
fig = figure(figsize=(6, 4)) 

#comment the next 2 lines to not hide top and right axis 
ax = Subplot(fig, 111) 
fig.add_subplot(ax) 

#uncomment next 2 lines to deal with ticks 
#ax = fig.add_subplot(111) 

#calculate data 
x = np.arange(0.8,2.501,0.001) 
y = 4*((1/x)**12 - (1/x)**6) 

#plot 
ax.plot(x,y) 

#do not display top and right axes 
#comment to deal with ticks 
ax.axis["right"].set_visible(False) 
ax.axis["top"].set_visible(False) 

#put ticks facing outwards 
#does not work when Sublot is called! 
for l in ax.get_xticklines(): 
    l.set_marker(mpllines.TICKDOWN) 

for l in ax.get_yticklines(): 
    l.set_marker(mpllines.TICKLEFT) 

#done 
show() 

答えて

7

少しあなたのコードを変更し、トリックを使用してthis linkから、これは動作しているようです:

import numpy as np 
import matplotlib.pyplot as plt 


#comment the next 2 lines to not hide top and right axis 
fig = plt.figure() 
ax = fig.add_subplot(111) 

#uncomment next 2 lines to deal with ticks 
#ax = fig.add_subplot(111) 

#calculate data 
x = np.arange(0.8,2.501,0.001) 
y = 4*((1/x)**12 - (1/x)**6) 

#plot 
ax.plot(x,y) 

#do not display top and right axes 
#comment to deal with ticks 
ax.spines["right"].set_visible(False) 
ax.spines["top"].set_visible(False) 

## the original answer: 
## see http://old.nabble.com/Ticks-direction-td30107742.html 
#for tick in ax.xaxis.majorTicks: 
# tick._apply_params(tickdir="out") 

# the OP way (better): 
ax.tick_params(axis='both', direction='out') 
ax.get_xaxis().tick_bottom() # remove unneeded ticks 
ax.get_yaxis().tick_left() 

plt.show() 

あなたが外側にしたいすべてのプロットにチェックした場合、ダニ方向in the rc file設定する方が簡単かもしれない - 実際xtick.direction

+1

のために、そのページの検索では、これは完璧に動作します。実際には、最後の 'for'ループは' ax.tick_params(axis = 'both'、direction = 'out') 'を使うことで回避できることを発見しました。これは同じくy軸の外側のティックを設定します時間。もう1つのことは、 'ax.get_xaxis()。tick_bottom()'と 'ax.get_yaxis()。tick_left()'をそれぞれ使って、上と下のティックを削除することです。そして 'rc'のチップのおかげで、それは役に立つでしょう! – englebip

+0

@englebipああ、素晴らしい。これを答えに編集しました。 –

関連する問題