Python 如何在x轴上设置日期刻度标签,仅针对matplotlib上的给定点
我试图在x轴上设置日期刻度标签,仅针对给定的点。例如,如果我有一个x轴上值的日期时间列表Python 如何在x轴上设置日期刻度标签,仅针对matplotlib上的给定点,python,date,matplotlib,label,Python,Date,Matplotlib,Label,我试图在x轴上设置日期刻度标签,仅针对给定的点。例如,如果我有一个x轴上值的日期时间列表 x = [ datetime.datetime(..), ... , datetime.datetime()] 我试过使用ax.xaxis.set\u标签(x) 我只想为列表中的六个点中的每一个点绘制日期,但我得到以下结果: 我用于获取此绘图的代码如下所示: # figure's size in inch fig = Figure(figsize=(8, 8)) # axes' position
x = [ datetime.datetime(..), ... , datetime.datetime()]
我试过使用ax.xaxis.set\u标签(x)
我只想为列表中的六个点中的每一个点绘制日期,但我得到以下结果:
我用于获取此绘图的代码如下所示:
# 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)
我做错了什么
感谢设置记号及其标签使用:
x = [ datetime.datetime(..), ... , datetime.datetime()]
ax.xaxis.set_ticks(x)
如果“轴”对象没有“设置刻度”属性,则会失败。。但是
ax.xaxis.set_ticks(x)
works:)谢谢,Ps。把它作为答案写下来,这样我就可以接受了