<bdo id='u5Kj2'></bdo><ul id='u5Kj2'></ul>
<tfoot id='u5Kj2'></tfoot>

<small id='u5Kj2'></small><noframes id='u5Kj2'>

  1. <i id='u5Kj2'><tr id='u5Kj2'><dt id='u5Kj2'><q id='u5Kj2'><span id='u5Kj2'><b id='u5Kj2'><form id='u5Kj2'><ins id='u5Kj2'></ins><ul id='u5Kj2'></ul><sub id='u5Kj2'></sub></form><legend id='u5Kj2'></legend><bdo id='u5Kj2'><pre id='u5Kj2'><center id='u5Kj2'></center></pre></bdo></b><th id='u5Kj2'></th></span></q></dt></tr></i><div id='u5Kj2'><tfoot id='u5Kj2'></tfoot><dl id='u5Kj2'><fieldset id='u5Kj2'></fieldset></dl></div>
    1. <legend id='u5Kj2'><style id='u5Kj2'><dir id='u5Kj2'><q id='u5Kj2'></q></dir></style></legend>

      在 matplotlib 中绘制一天中的时间与日期

      时间:2023-09-14
      1. <tfoot id='aSGR5'></tfoot>

      2. <legend id='aSGR5'><style id='aSGR5'><dir id='aSGR5'><q id='aSGR5'></q></dir></style></legend>
          <tbody id='aSGR5'></tbody>

        <small id='aSGR5'></small><noframes id='aSGR5'>

              <i id='aSGR5'><tr id='aSGR5'><dt id='aSGR5'><q id='aSGR5'><span id='aSGR5'><b id='aSGR5'><form id='aSGR5'><ins id='aSGR5'></ins><ul id='aSGR5'></ul><sub id='aSGR5'></sub></form><legend id='aSGR5'></legend><bdo id='aSGR5'><pre id='aSGR5'><center id='aSGR5'></center></pre></bdo></b><th id='aSGR5'></th></span></q></dt></tr></i><div id='aSGR5'><tfoot id='aSGR5'></tfoot><dl id='aSGR5'><fieldset id='aSGR5'></fieldset></dl></div>

              • <bdo id='aSGR5'></bdo><ul id='aSGR5'></ul>
                本文介绍了在 matplotlib 中绘制一天中的时间与日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想以特定的几种方式在 x 轴上绘制日期,在 y 轴上绘制一天中的时间,然后绘制线图或间隔(浮动条)图.

                I want to plot, in a specific few ways, dates on the x axis and time of day on the y axis, and then have either line plots or interval (floating bar) plots.

                这个答案有帮助

                但我与那个情节有一些不同,我无法让它发挥作用.实际上,我正在获取 y 轴来绘制日期和时间,因此当我只需要大约一天的时间时,它正在处理 y 轴上几个月的时间戳.该示例在评论中声称,yaxis 的基准日期可以是任何东西,因为信息是及时的",但我不明白他是如何丢弃"基准日期信息的.

                But I have a few differences from that plot and I can't get it to work. I'm actually getting the y axis to plot both the DATES as well as the time, so it is scrunching months' worth of timestamps on the y axis, when I just need about one day's worth. That example claims in the comments, "base date for yaxis can be anything, since information is in the time", but I don't understand how he is "throwing away" the base date information.

                无论如何,我的需求是:

                In any case, my needs are:

                1. 我希望 y 轴选择 24 小时制(00:00 到 24:00)或上午/下午样式时间,轴刻度看起来像下午 3:00,上午 11:00等.我猜这将使用 FuncFormatter 完成.

                1. I'd like the option for either 24 hour time (00:00 to 24:00) or am/pm style time for the y axis, with the axis ticks looking like 3:00pm, 11:00am, etc. This will be done with a FuncFormatter, I guess.

                对于间隔(时间范围),我不想使用误差线——线条太细了.我想使用(浮动)条形图/柱形图.

                For the intervals (time ranges) I don't want to use the error bars--the lines are way too thin. I'd like to use a (floating) bar/column plot.

                我的数据是格式为2010-12-20 05:00:00"的日期时间字符串

                My data are datetime strings of the format '2010-12-20 05:00:00'

                感谢您的帮助.

                推荐答案

                我认为您对 matplotlib 如何在幕后处理时间和日期感到有些困惑.

                I think you're slightly confused as to exactly how matplotlib handles times and dates behind the scenes.

                matplotlib 中的所有日期时间都表示为简单的浮点数.1 天对应于 1.0 的差异,日期是自 1900 年以来的天数(如果我没记错的话,无论如何).

                All datetimes in matplotlib are represented as simple floats. 1 day corresponds to a difference of 1.0, and the dates are in days since 1900 (if I remember correctly, anyway).

                因此,为了仅绘制给定日期的时间,您需要使用 %1.

                So, in order to plot just the time of a given date, you need to use a % 1.

                我将使用积分,但您可以轻松使用条形.如果您确实使用 plt.bar,请考虑使用 bottom 关键字参数,这样条形的底部将从间隔的开始时间开始(并记住第二个参数是条形的 高度,而不是它的顶部 y 值).

                I'm going to use points, but you can easily use bars. Look into using the bottom keyword argument if you do use plt.bar, so that the bottom of the bars will start at the start time of your intervals (and remember that the second argument is the height of the bar, not its top y-value).

                例如:

                import matplotlib.pyplot as plt
                import matplotlib as mpl
                import numpy as np
                import datetime as dt
                
                # Make a series of events 1 day apart
                x = mpl.dates.drange(dt.datetime(2009,10,1), 
                                     dt.datetime(2010,1,15), 
                                     dt.timedelta(days=1))
                # Vary the datetimes so that they occur at random times
                # Remember, 1.0 is equivalent to 1 day in this case...
                x += np.random.random(x.size)
                
                # We can extract the time by using a modulo 1, and adding an arbitrary base date
                times = x % 1 + int(x[0]) # (The int is so the y-axis starts at midnight...)
                
                # I'm just plotting points here, but you could just as easily use a bar.
                fig = plt.figure()
                ax = fig.add_subplot(111)
                ax.plot_date(x, times, 'ro')
                ax.yaxis_date()
                fig.autofmt_xdate()
                
                plt.show()
                

                希望能有所帮助!

                这篇关于在 matplotlib 中绘制一天中的时间与日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:pandas 时间从UTC到本地 下一篇:如何将明天(在特定时间)日期转换为时间戳

                相关文章

                最新文章

              • <legend id='811B0'><style id='811B0'><dir id='811B0'><q id='811B0'></q></dir></style></legend>
              • <tfoot id='811B0'></tfoot>

                  <small id='811B0'></small><noframes id='811B0'>

                    <bdo id='811B0'></bdo><ul id='811B0'></ul>
                1. <i id='811B0'><tr id='811B0'><dt id='811B0'><q id='811B0'><span id='811B0'><b id='811B0'><form id='811B0'><ins id='811B0'></ins><ul id='811B0'></ul><sub id='811B0'></sub></form><legend id='811B0'></legend><bdo id='811B0'><pre id='811B0'><center id='811B0'></center></pre></bdo></b><th id='811B0'></th></span></q></dt></tr></i><div id='811B0'><tfoot id='811B0'></tfoot><dl id='811B0'><fieldset id='811B0'></fieldset></dl></div>