[英]How to put a text box on python figure when the xaxis is a datetime
I have made a figure with the x-axis as datetime
(2012-8-1, 2012-10,7....) 我用x轴作为
datetime
制作了一个图(2012-8-1,2012-10,7 ....)
I want to put a textbox to label this figure 我想放一个文本框来标记这个数字
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)
But I can't see the labels at all. 但是我根本看不到标签。
How can I realize it? 我怎么知道呢?
You need to either enter the x-coordinate in the same format (a date) as for the other plot methods, or transform the coordinates. 您需要以与其他绘图方法相同的格式(日期)输入x坐标,或变换坐标。 If you want to stick with the x=.25 you could use:
如果要坚持使用x = .25,则可以使用:
ax.text(0.25,ymax[i],id[i], transform=ax.transAxes, fontsize=15)
It will place the text at a quarter of the ax's width. 它将文本放置在斧头宽度的四分之一处。
An approach is to convert the date to a number and use the number in subsequent calls: 一种方法是将日期转换为数字,并在后续调用中使用该数字:
dates = matplotlib.dates.date2num([x])
ax.text(dates[0], y, message, color='red')
where x is the datetime you want to convert, y is assumed not to be in datetime format but in numerical format, and message contains the string you want to have displayed. 其中x是要转换的日期时间,假定y不是日期时间格式,而是数字格式,并且message包含要显示的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.