[英]How to put a text box on python figure when the xaxis is a datetime
我用x軸作為datetime
制作了一個圖(2012-8-1,2012-10,7 ....)
我想放一個文本框來標記這個數字
id = ("(A)","(B)","(C)","(D)","(E)","(Average)")
months = mpl.dates.MonthLocator() # every month
days = mpl.dates.DayLocator(interval=5) # every 10 days
dateFmt=mpl.dates.DateFormatter('%m-%d')
ax.xaxis.set_major_formatter(dateFmt)
ax.xaxis.set_major_locator(months)
#ax.xaxis.set_minor_locator(days)
ax.xaxis_date()
ax.set_ylim(ymin[i],ymax[i])
ax.set_ylabel(ylabels)
ax.axhline(linewidth=0.5,color="k")
ax.yaxis.set_major_locator(tk.MaxNLocator(nbins=3))
ax.yaxis.set_minor_locator(tk.MaxNLocator(nbins=6))
[tickline.set_markersize(3) for tickline in ax.yaxis.get_ticklines(minor=True)]
[tickline.set_markersize(2.5) for tickline in ax.yaxis.get_ticklines(minor=True)]
ax.text(0.25,ymax[i],id[i],fontsize=15)
但是我根本看不到標簽。
我怎么知道呢?
您需要以與其他繪圖方法相同的格式(日期)輸入x坐標,或變換坐標。 如果要堅持使用x = .25,則可以使用:
ax.text(0.25,ymax[i],id[i], transform=ax.transAxes, fontsize=15)
它將文本放置在斧頭寬度的四分之一處。
一種方法是將日期轉換為數字,並在后續調用中使用該數字:
dates = matplotlib.dates.date2num([x])
ax.text(dates[0], y, message, color='red')
其中x是要轉換的日期時間,假定y不是日期時間格式,而是數字格式,並且message包含要顯示的字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.