2012-10-12 10 views
5

x軸に日付目盛ラベルを設定しようとしています。例えば、私はax.xaxis.set_ticklabels(x)と試みmatplotlibの与えられた点についてのみ、x軸の日付目盛ラベルを設定する方法

x = [ datetime.datetime(..), ... , datetime.datetime()] 

x軸上の値のための日時のリストを持っている場合。

私は唯一のリストに存在する6点のそれぞれ1日付をプロットしたいが、私はこの結果を得る:

plot

私はこのプロットを取得するために使用されるコードは次のとおりです。

# figure's size in inch 
fig = Figure(figsize=(8, 8)) 
# axes' position         
ax = Axes(fig, [.1, .1, .8, .8]) 
ax.errorbar(matplotlib.dates.date2num(x), y, yerr=el['e'], fmt=format_string, label=label) 

# shrinks current axis to 90% 
box = ax.get_position() 
ax.set_position([box.x0, box.y0, box.width * 0.9, box.height]) 
# puts a legend to the right of the current axis 
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) 
# autoscales axes following data limits 
ax.autoscale(tight=False) 

dateFmt = matplotlib.dates.DateFormatter('%Y-%m-%d') 
ax.xaxis.set_major_formatter(dateFmt) 
monthsLoc = matplotlib.dates.MonthLocator() 
daysLoc = matplotlib.dates.DayLocator(interval=1) 
ax.xaxis.set_major_locator(monthsLoc) 
ax.xaxis.set_minor_locator(daysLoc) 
fig.autofmt_xdate(bottom=0.18) 

# I tried to set tick labels with this but with no results 
#ax.xaxis.set_ticklabels(x) 
# adds axes to figure 
fig.add_axes(ax) 
# creates a canvas from figure 
canvas = FigureCanvasAgg(fig) 
# saves figure to filesystem in png format 
canvas.print_figure(settings.MEDIA_ROOT + file_relative_path) 

私は間違っていますか?目盛りとそのラベルを設定するには感謝

+0

てみましたか? – tillsten

+2

''Axes'オブジェクトに属性 'set_ticks'がないと失敗します。しかし、 'ax.xaxis.set_ticks(x)'は動作します:) Thanks Ps。私はそれを受け入れることができるように答えとして書く – gc5

答えて

6

が使用

:あなたはax.set_ticksを

x = [ datetime.datetime(..), ... , datetime.datetime()] 
ax.xaxis.set_ticks(x) 
関連する問題