简体   繁体   English

当xaxis是日期时间时如何在python图形上放置文本框

[英]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)")

X-axis X轴

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()

Y-axis Y轴

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)]

Text 文本

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.

相关问题 如何在 python 中使用 matplotlib 将文本放入图中 - How to put text in figure using matplotlib in python Python / Matplotlib - 如何将文本放在相等方面图的角落 - Python/Matplotlib - How to put text in the corner of equal aspect figure 如何将数字标签格式化为 python 中 plotly x 轴上的日期时间标签? - How to format numerical labels into datetime labels on a plotly xaxis in python? 在跟踪中设置hoverinfo时,Python Plotly xaxis悬停文本消失 - Python Plotly xaxis hover text disappears when hoverinfo is set in a trace 使用python matplotlib时如何更改图中的所有文本大小? - How to change all text sizes in figure when using python matplotlib? 使用 Plotly Python 将 Xaxis 置于 Y 坐标 0 - Put the Xaxis at the Y coordinate 0 with Plotly Python Python Bokeh工具-如何从bokeh显示带有日期时间格式化程序xaxis的hovertool? - Python Bokeh tool - How to display hovertool with datetime formatter xaxis from bokeh? 如何在 pandas dataframe 与 matplotlib 在 ZA7F117F35426B56227 中绘制时保持 xaxis 中的日期时间格式不变? - How to keep the datetime format in xaxis intact while plotting pandas dataframe with matplotlib in Python? 如何将每个月放在matplotlib的xaxis中? - How to put each months in xaxis of matplotlib? 如何将文本放在matplotlib中的绘图框中 - How to put text inside a box on a plot in matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM