如何每小时获得一次滴答声? [英] how to get ticks every hour?

查看:23
本文介绍了如何每小时获得一次滴答声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个简单的例子

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import matplotlib.dates as mdates

pd.__version__
Out[147]: u'0.22.0'

idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')

df = pd.Series(np.random.randn(len(idx)),  index = idx)
df.head()
Out[145]: 
2017-01-01 05:03:00   0.4361
2017-01-01 05:04:00   0.9737
2017-01-01 05:05:00   0.8430
2017-01-01 05:06:00   0.4292
2017-01-01 05:07:00   0.5739
Freq: T, dtype: float64

我想绘制此图,并且每小时进行一次滴答.我用:

I want to plot this, and have ticks every hour. I use:

fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1)  #
h_fmt = mdates.DateFormatter('%H:%M:%S')

df.plot(ax = ax, color = 'black', linewidth = 0.4)

ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)

给出

为什么这里没有每小时出现一次滴答声?感谢您的帮助!

why dont the ticks appear every hour here? Thanks for your help!

推荐答案

问题在于,虽然 Pandas 通常直接包装 matplotlib 绘图方法,但对于带有日期的绘图,情况并非如此.一旦涉及日期,pandas 就会使用完全不同的日期数字表示,因此也使用自己的刻度定位器.

The problem is that while pandas in general directly wraps the matplotlib plotting methods, this is not the case for plots with dates. As soon as dates are involved, pandas uses a totally different numerical representation of dates and hence also uses its own locators for the ticks.

如果您想在用 Pandas 创建的图上使用 matplotlib.dates 格式化程序或定位器,您可以在 Pandas 图中使用 x_compat=True 选项.

In case you want to use matplotlib.dates formatters or locators on plots created with pandas you may use the x_compat=True option in pandas plots.

df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)

这允许使用 matplotlib.dates 格式化程序或定位器,如下所示.否则,您可以将 df.plot(ax = ax, color = 'black', linewidth = 0.4) 替换为

This allows to use the matplotlib.dates formatters or locators as shown below. Else you may replace df.plot(ax = ax, color = 'black', linewidth = 0.4) by

ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)

完整示例:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)),  index = idx)

fig, ax = plt.subplots()
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')

ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
#or use
df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True)
#Then tick and format with matplotlib:
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)

fig.autofmt_xdate()
plt.show()

<小时>如果在这里使用 Pandas 的动机是(如下面的评论中所述)能够使用 secondary_y,那么 matplotlib 图的等价物将是双轴 twinx.


If the motivation to use pandas here is (as stated in the comments below) to be able to use secondary_y, the equivalent for matplotlib plots would be a twin axes twinx.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')

df = pd.DataFrame(np.cumsum(np.random.randn(len(idx), 2),0), 
                  index = idx, columns=list("AB"))

fig, ax = plt.subplots()
ax.plot(df.index, df["A"], color = 'black')
ax2 = ax.twinx()
ax2.plot(df.index, df["B"], color = 'indigo')

hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)

fig.autofmt_xdate()
plt.show()

这篇关于如何每小时获得一次滴答声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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