【发布时间】:2020-04-20 05:59:45
【问题描述】:
我通过博客和本网站(参见 Modify date ticks for pandas plot 之类的帖子)看到了无数示例,说明能够使用 matplotlib.dates 来控制 pandas 数据框绘图的绘图。我正在努力获得类似的行为:
import pandas as pd
import sys
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib import ticker
print(f'System info: {sys.version}')
print(f'Pandas Version: {pd.__version__}')
print(f'Matplotlib Version: {matplotlib.__version__}')
index = pd.date_range(start='12/21/19 10:43:00', end='2019-12-21 10:47:00', freq= '10s')
data = {'A': [0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0.],
'B': [2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0.]}
df = pd.DataFrame(index=index, data=data)
ax = df.plot()
ax.xaxis.set_minor_locator(ticker.NullLocator())
ax.xaxis.set_major_locator(mdates.SecondLocator(interval=15))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plt.gcf().autofmt_xdate()
# ax.get_figure().canvas.draw()
plt.show()
我的终端的输出是:
System info: 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]
Pandas Version: 0.25.3
Matplotlib Version: 3.1.2
我根本没有得到任何标签。似乎pandas 或matplotlib 没有转换为datetime,即使date_range 产生datetime 对象。
【问题讨论】:
标签: python pandas matplotlib