每小时如何获得滴答声? [英] how to get ticks every hour?
问题描述
考虑这个简单的例子
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!
推荐答案
问题在于,尽管大熊猫通常直接包装matplotlib绘图方法,但带日期的绘图却不是这种情况.一旦涉及到日期,大熊猫将使用完全不同的日期数字表示形式,因此也将其自身的定位器用于刻度.
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.
如果要在使用熊猫创建的图上使用matplotlib.dates
格式化程序或定位符,可以在熊猫图上使用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()
如果此处使用熊猫的动机是(如下面的评论所述)能够使用
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屋!