我有15分钟的间隔记录数据。我想绘制这些数据并让x轴每天显示一个小刻度,每个月都有一个主要刻度。这是我在1月和2月试图做的事情的片段。
#!/usr/bin/env python3
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
#make some data
t = np.arange(0, 5664, 1) # Jan and Feb worth of 15 minute steps
s = np.sin(2*np.pi*t) # data measured
#plot the data
fig, ax = plt.subplots()
ax.plot(t, s)
#select formatting
days = mdates.DayLocator()
daysfmt = mdates.DateFormatter('%d')
months = mdates.MonthLocator()
monthsfmt = mdates.DateFormatter('\n%b')
从文档和其他Q& A中我已经阅读并尝试拼凑在一起,我明白我需要告诉matplotlib我想用于x轴的格式。这是我感到困惑的地方。我无法弄清楚如何指示绘图数据是每15分钟(每个小刻度1440个样本),所以当我显示绘图时,图表上没有显示任何内容。或者至少我认为这是原因......
#apply formatting
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsfmt)
ax.xaxis.set_minor_locator(days)
ax.xaxis.set_minor_formatter(daysfmt)
#select dates
datemin = dt.datetime.strptime('01/01/17', '%d/%m/%y')
datemax = dt.datetime.strptime('28/02/17', '%d/%m/%y')
ax.set_xlim(datemin, datemax)
plt.show()
绘制结果
答案 0 :(得分:0)
在那里,工作;)
您的代码存在两个问题:
您正在绘制t和s,但是将x轴设置为错误的刻度
你只是在绘制sin(x)的数值噪声,因为你总是使用2pi的倍数作为sin的参数,而sin(n * 2pi)= 0,对于任何整数n
< / LI> 醇>完整代码:
#!/usr/bin/env python3
import num
py as np
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
#make some data
t = np.arange(0, 5664, 0.1) # Jan and Feb worth of 15 minute steps
s = np.sin(2*np.pi*t) # data measured
#plot the data
fig, ax = plt.subplots()
#ax.plot(t, s)
#select formatting
days = mdates.DayLocator()
daysfmt = mdates.DateFormatter('%d')
months = mdates.MonthLocator()
monthsfmt = mdates.DateFormatter('\n%b')
#apply formatting
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsfmt)
ax.xaxis.set_minor_locator(days)
ax.xaxis.set_minor_formatter(daysfmt)
#select dates
datemin = dt.datetime.strptime('01/01/17', '%d/%m/%y')
datemax = dt.datetime.strptime('28/02/17', '%d/%m/%y')
t0 = dt.datetime(2017,1,1)
t_datetime = [ t0 + dt.timedelta(minutes=15*t_) for t_ in t ]
ax.plot(t_datetime,s)
ax.set_xlim(t0, t_datetime[-1])
plt.show()