2011-06-07 11 views
19

プロット領域の内側に描画されているため、多くのmatplotlibプロットのデータによって軸の目盛りが不明瞭になります。より良いアプローチは、のRのプロットシステムのデフォルトであるように、軸から伸びるティックを外側にとして描くことです。理論的にはmatplotlibでは、軸の外側を指すRスタイルの軸ティックをどのように描画しますか?

、これはx軸とy軸のTICKDOWNTICKLEFTラインスタイルを持つダニラインを再描画することによって行うことができ、それぞれダニ:

import matplotlib.pyplot as plt 
import matplotlib.ticker as mplticker 
import matplotlib.lines as mpllines 

# Create everything, plot some data stored in `x` and `y` 
fig = plt.figure() 
ax = fig.gca() 
plt.plot(x, y) 

# Set position and labels of major and minor ticks on the y-axis 
# Ignore the details: the point is that there are both major and minor ticks 
ax.yaxis.set_major_locator(mplticker.MultipleLocator(1.0)) 
ax.yaxis.set_minor_locator(mplticker.MultipleLocator(0.5)) 

ax.xaxis.set_major_locator(mplticker.MultipleLocator(1.0)) 
ax.xaxis.set_minor_locator(mplticker.MultipleLocator(0.5)) 

# Try to set the tick markers to extend outward from the axes, R-style 
for line in ax.get_xticklines(): 
    line.set_marker(mpllines.TICKDOWN) 

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

# In real life, we would now move the tick labels farther from the axes so our 
# outward-facing ticks don't cover them up 

plt.show() 

しかし、実際には、それは半分だけですget_xticklinesget_yticklinesメソッドは、メジャーのティックラインだけを返します。小目盛りは内側を指しています。

マイナーダニの回避策はありますか?

答えて

29

、matplotlibrcは、あなたが設定することができます。

>> from matplotlib import rcParams 
>> rcParams['xtick.direction'] = 'out' 
>> rcParams['ytick.direction'] = 'out' 
4

あなたは、少なくとも2つの方法で未成年者を得ることができます:小目盛りのラインもMultipleLocatorコールによって、「主要な」場所で描かれているので、38であることを

>>> ax.xaxis.get_ticklines() # the majors 
<a list of 20 Line2D ticklines objects> 
>>> ax.xaxis.get_ticklines(minor=True) # the minors 
<a list of 38 Line2D ticklines objects> 
>>> ax.xaxis.get_minorticklines() 
<a list of 38 Line2D ticklines objects> 

注意。

xtick.direction  : out  # direction: in or out 
ytick.direction  : out  # direction: in or out 

を、これは両方のメジャーとマイナーを外側にデフォルトでダニ、単一のプログラムのためにR.のように、単純に行う描画されます:あなたのmatplotlibの設定ファイルで

+0

これで解決できます。ありがとう。 – pash

関連する問題