x轴上带有时间戳的 pandas 图-刻度单位? [英] pandas plot with timestamps on x axis - tick units?

查看:88
本文介绍了x轴上带有时间戳的 pandas 图-刻度单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有日期时间值的熊猫数据框:

I have a pandas data frame with datetime values:

    Date/Time           Event Value
0   2018-11-15 17:17:49 62
1   2018-11-15 17:27:50 63
2   2018-11-15 17:37:50 64
3   2018-11-15 17:42:49 65
4   2018-11-15 18:17:49 64
5   2018-11-15 19:27:48 65
6   2018-11-15 19:37:48 66
7   2018-11-15 19:47:49 67
8   2018-11-15 19:57:49 68
9   2018-11-15 20:12:49 69

print (tmp.dtypes)

Date/Time      datetime64[ns]
Event Value             int64

按如下所示进行绘制:

fig, ax1 = plt.subplots(1,1,figsize=(8,4))
tmp.plot(x='Date/Time',y='Event Value', legend=None, ax=ax1)

x轴已使用适当的日期和时间进行了格式化(并且可以使用set_major_formatter进行进一步格式化).

The x-axis has been formatted with appropriate dates and times (and can be further formatted using set_major_formatter).

现在,我想在图表上绘制一个矩形,以突出显示特定时间.为此,我需要知道x位置.

Now I want to draw a rectangle on the chart, to highlight a particular time. To do that I need to know the x-positions.

print([x for x in ax1.get_xticks()])

[737013.7291666666, 737013.75, 737013.7708333334, 737013.7916666666, 737013.8125, 737013.8333333334]

这些确实是x值,即,如果我将它们用作矩形的原点,则会按预期绘制.

These are indeed the x-values, i.e. if I use them as origins for the rectangle it plots as expected.

rect = patches.Rectangle((737013.75,55),.01,100)
ax1.add_patch(rect)

我不知道这些数字是从哪里来的.它们不是时代值,尽管看起来有点像它们("737013.75"对应于"1970-01-09 07:43:33",对我而言没有任何意义).

I don't understand where these numbers come from. They're not epoch values, though they look a little bit like them ('737013.75' corresponds to '1970-01-09 07:43:33', which doesn't mean anything to me).

如何从数据框中的日期/时间值获取图表上的x位置?

How can I get the x-position on the chart from a Date/Time value in my dataframe?

推荐答案

如果使用x_compat=True选项绘制数据框,则可以确定单位为matplotlib.dates单位.否则,大熊猫可能会为您决定一些单位.

If you plot a dataframe with the x_compat=True option you can be certain that the units are matplotlib.dates units. Else, pandas may decide some units for you.

此处,熊猫使用matplotlib日期单位.它们被定义为:

Here, pandas uses matplotlib dates units. They are defined as:

Matplotlib使用浮点数表示日期,该浮点数指定自0001-01-01 UTC以来的天数加1.例如,0001-01-01、06:00为1.25,而不是0.25.值< 1,即不支持UTC 0001-01-01之前的日期.

Matplotlib represents dates using floating point numbers specifying the number of days since 0001-01-01 UTC, plus 1. For example, 0001-01-01, 06:00 is 1.25, not 0.25. Values < 1, i.e. dates before 0001-01-01 UTC are not supported.

但是,您不希望自己计算这些单位. Matplotlib提供辅助功能

You are however not expected to calculate those units yourself. Matplotlib provides helper functions

  • matplotlib.dates.date2num
  • matplotlib.dates.num2date
  • matplotlib.dates.datestr2num
  • matplotlib.dates.date2num
  • matplotlib.dates.num2date
  • matplotlib.dates.datestr2num

,可用于在单位之间来回计算.

which you can use to calculate back and forth between the units.

例如,matplotlib.dates.datestr2num("2018-11-15 19:27:48")给您737013.8109722222.

这篇关于x轴上带有时间戳的 pandas 图-刻度单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆