Matplotlib 以不规则间隔绘制日期时间x轴
我想为自定义日期范围绘制数据。所以我的数据集是每天早上8点到下午5点。我需要绘制5天的时间范围。我如何才能做到这一点,使我不会得到除上述时间范围以外的其他时间范围。我尝试使用x定位器进行绘图,但它显示了整个时间范围内的数据 尝试设置x定位器无效Matplotlib 以不规则间隔绘制日期时间x轴,matplotlib,Matplotlib,我想为自定义日期范围绘制数据。所以我的数据集是每天早上8点到下午5点。我需要绘制5天的时间范围。我如何才能做到这一点,使我不会得到除上述时间范围以外的其他时间范围。我尝试使用x定位器进行绘图,但它显示了整个时间范围内的数据 尝试设置x定位器无效导入日期时间 import datetime from matplotlib import dates as mdates import pandas as pd import matplotlib.pyplot as plt import matplot
导入日期时间
import datetime
from matplotlib import dates as mdates
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates
x= pd.date_range(start=datetime.datetime( 2019, 7, 8, 8, 0,0 ), freq='10min',end=datetime.datetime ( 2019, 7, 12, 17, 0 ,0))
start = datetime.time(8, 0, 0)
end = datetime.time(17, 0, 0)
def time_in_range(start, end, x):
"""Return true if x is in the range [start, end]"""
if start <= end:
return start <= x <= end
else:
return start <= x or x <= end
t= [i.to_datetime() for i in x if time_in_range(start,end,i.to_datetime().time())]
df=pd.DataFrame({'d':d,'t':t})
df=df.set_index('t')
fig,axes = plt.subplots()
axes.xaxis_date()
axes.bar(mdates.date2num(list(df.index)),df['d'],align='center',width=0.006,color='pink')
fig.autofmt_xdate()
xformatter = mdates.DateFormatter('%d/%m/%y:%H:%M')
xlocator = mdates.HourLocator(byhour=range(8,17,2))
axes.xaxis.set_major_locator(xlocator)
plt.gcf().axes[0].xaxis.set_major_formatter(xformatter)
plt.show()
从matplotlib导入日期为mdates
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
导入matplotlib.dates
x=pd.date_范围(开始=datetime.datetime(2019,7,8,8,0,0),频率=10min',结束=datetime.datetime(2019,7,12,17,0,0))
start=datetime.time(8,0,0)
end=datetime.time(17,0,0)
def time_在_范围内(开始、结束、x):
“”“如果x在范围[start,end]内,则返回true”“”
如果开始导入日期时间
从matplotlib导入日期为mdates
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
导入matplotlib.dates
x=pd.date_范围(开始=datetime.datetime(2019,7,8,8,0,0),频率=10min',结束=datetime.datetime(2019,7,12,17,0,0))
start=datetime.time(8,0,0)
end=datetime.time(17,0,0)
def time_在_范围内(开始、结束、x):
“”“如果x在范围[start,end]内,则返回true”“”
如果启动Hi@Dhruv!欢迎来到StackOverflow。你能发布到目前为止你使用的代码来显示完整的时间范围吗?这将帮助我们解决如何帮助的问题。matplotlib文档。您可能希望(使用代码)显示这在多大程度上没有帮助。嗨@Dhruv!欢迎来到StackOverflow。你能发布到目前为止你使用的代码来显示完整的时间范围吗?这将帮助我们解决如何帮助的问题。matplotlib文档。您可能希望(使用代码,)显示这在这里没有帮助的程度。在上面的代码中,d可以是len 275的任意随机数据在上面的代码中,d可以是len 275的任意随机数据