5
Tôi đang cố gắng đặt nhãn đánh dấu ngày trên trục x, chỉ cho điểm đã cho. Ví dụ: nếu tôi có danh sách ngày giờ cho các giá trị trên trục xCách đặt nhãn đánh dấu ngày trên trục x, chỉ cho các điểm đã cho trên matplotlib
x = [ datetime.datetime(..), ... , datetime.datetime()]
Tôi đã thử với ax.xaxis.set_ticklabels(x)
.
Tôi muốn hẹn hò vẽ chỉ cho mỗi một trong sáu điểm có mặt trong danh sách nhưng tôi nhận được kết quả này:
Code tôi sử dụng để có được âm mưu này là như sau:
# 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)
Tôi đang làm gì sai?
Cảm ơn
Bạn có thử ax.set_ticks? – tillsten
Không thành công với đối tượng 'Trục 'không có thuộc tính' set_ticks''. Nhưng 'ax.xaxis.set_ticks (x)' works :) Cảm ơn Ps. viết nó như một câu trả lời để tôi có thể chấp nhận nó – gc5