我的 pandas 数据框如下所示:
data1 data2 data3
DateTime
....
2016-04-18 16:16:53 -66 1 94.8654
2016-04-18 16:17:03 -67 1 94.8601
2016-04-18 16:17:13 -68 1 94.8410
2016-04-18 16:17:23 -69 1 94.8753
2016-04-18 16:17:33 -70 1 94.8535
2016-04-18 16:17:43 -71 1 94.8529
2016-04-18 16:17:53 -72 1 94.8702
....
当我用它绘制之后
plt.style.use('ggplot')
df.plot(subplots=True, style=style, title='some title', grid=True, x_compat=True)
该情节仅显示几个小时。
1)如何让它也显示日期?
2)如何强制它显示我想要的任何格式?
您必须使用 matplotlib.dates 模块中的函数:
import pandas as pd
from datetime import datetime
import numpy as np
import matplotlib.pylab as plt
import matplotlib.dates as mdates
timeInd = pd.date_range(start = datetime(2016,4,17,23,0,0),
end = datetime(2016,4,20,1,0,0), freq = 'H')
d = {'data1': np.random.randn(len(timeInd)), 'data2': np.random.randn(len(timeInd)),
'data3': np.random.randn(len(timeInd))}
df = pd.DataFrame(data = d, index = timeInd)
plt.style.use('ggplot')
df.plot(subplots=True, grid=True, x_compat=True)
ax = plt.gca()
# set major ticks location every day
ax.xaxis.set_major_locator(mdates.DayLocator())
# set major ticks format
ax.xaxis.set_major_formatter(mdates.DateFormatter('\n\n\n%d.%m.%Y'))
# set minor ticks location every two hours
ax.xaxis.set_minor_locator(mdates.HourLocator(interval=2))
# set minor ticks format
ax.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M:%S'))
# or just set together date and time for major ticks like
# ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%Y %H:%M:%S'))
plt.show()
更多示例在这里:http://matplotlib.org/examples/api/date_demo.html http://matplotlib.org/examples/api/date_demo.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)