将AXHLINE添加到我的图中,并用DateTime对象约束Xmax和Xmin
我有一个给定位置的时间序列。我已经使用其他数据和遥感进行了一些分析,以确定相同位置的森林生长季节的开始和结束。现在,我想在次级X轴上强加小水平线,该线轴显示了生长季节的长度。理想情况下,它看起来像 this 。如果主要X轴为温度,则次级X轴是单个生长季节的静态值,而Y轴是绘制为DateTime对象的13年时间段。因此,基本上,我想要相同的蓝线,但是我希望在两个日期时值确定长度。
我知道Axhline()需要AY位置论点(这将是生长季节的静态值),而Xmin和一个必须在0到1之间的Xmin和Xmax
。为了给出适当的值xmin和xmax值,为13年(每日)数据框架。
这是我用来绘制上面图的代码
,在这里是带日期的dataframe对于13个Axhline()线的每一个的开始和结尾
fig, temp = plt.subplots()
temp.plot(df_w.index, df_w['TA_F'], color = 'red', label = 'TEMP')
# set x-label
temp.set_xlabel('Date')
temp.tick_params('x', labelsize =24)
# set primary y label
temp.set_ylabel('Tempurature (C)')
temp.tick_params('y', colors = 'red', labelsize =24)
# set x-axis limits as the min and max of the series
temp.set_xlim(date2num([df_w.index.min(), df_w.index.max()]))
temp.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
temp.xaxis.set_major_locator(mdates.YearLocator(1, month=1, day=1))
temp.set_ylim(2,28)
season = temp.twinx()
season.set_ylabel('GPP 20th percent yearly max')
season.tick_params('y', colors = 'blue', labelsize =24)
season.scatter(df_d.index, SRO_20['GPP_MINCORRECTED_20th'], marker = '^',color = 'blue')
plt.show()
I have a time series of temperatures for a given location. I have done some analyses using other data and remote sensing to determine the Start and End of the forest growing season for the same location. Now I would like to impose small horizontal lines on a secondary x-axis, which display the length of the growing season. Ideally, it would look something like this. Where the primary x axis is temp, the secondary x axis is a static value for a single growing season and the y axis is the 13 year time period ploted as a datetime object. So basically, i want the same blue lines, but i want there length to be determined by two datetime values.
I am aware that axhline() takes a y positional argument (which will be the static value for the growing season), and a xmin and an xmax which must be normalized floats between 0 and 1.
My question is then, how do I normalize a 13 year-long (daily) dataframe in order to give the plt.axhline() the appropriate values xmin and xmax values.
here is the code i am using to plot the figure above
and here is the dataframe with the dates for the start and end of each of the 13 axhline() lines
fig, temp = plt.subplots()
temp.plot(df_w.index, df_w['TA_F'], color = 'red', label = 'TEMP')
# set x-label
temp.set_xlabel('Date')
temp.tick_params('x', labelsize =24)
# set primary y label
temp.set_ylabel('Tempurature (C)')
temp.tick_params('y', colors = 'red', labelsize =24)
# set x-axis limits as the min and max of the series
temp.set_xlim(date2num([df_w.index.min(), df_w.index.max()]))
temp.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
temp.xaxis.set_major_locator(mdates.YearLocator(1, month=1, day=1))
temp.set_ylim(2,28)
season = temp.twinx()
season.set_ylabel('GPP 20th percent yearly max')
season.tick_params('y', colors = 'blue', labelsize =24)
season.scatter(df_d.index, SRO_20['GPP_MINCORRECTED_20th'], marker = '^',color = 'blue')
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑使用如果要使用数据坐标而不是轴坐标,而不是
axhline
。您设法进行的任何标准化都会崩溃,当用户放大时。Consider using
hlines
instead ofaxhline
if you want to use data coordinates instead of axes coordinates. Any normalization you manage to do will collapse the moment the user zooms in.