python绘制时间_在matplotlib中绘制一天中的时间与日期

我想你对matplotlib在幕后如何处理时间和日期有点困惑。

matplotlib中的所有日期时间都表示为简单的浮点数。1天相当于1.0的差,日期是1900年以来的天数(如果我没记错的话)。

因此,为了只绘制给定日期的时间,需要使用% 1。

我要用点,但你可以很容易地用条。如果您确实使用了plt.bar,请考虑使用bottom关键字参数,以便条形图的底部将从间隔的开始时间开始(请记住,第二个参数是条形图的高度而不是其顶部的y值)。

例如:import matplotlib.pyplot as plt

import matplotlib as mpl

import numpy as np

import datetime as dt

# Make a series of events 1 day apart

x = mpl.dates.drange(dt.datetime(2009,10,1),

dt.datetime(2010,1,15),

dt.timedelta(days=1))

# Vary the datetimes so that they occur at random times

# Remember, 1.0 is equivalent to 1 day in this case...

x += np.random.random(x.size)

# We can extract the time by using a modulo 1, and adding an arbitrary base date

times = x % 1 + int(x[0]) # (The int is so the y-axis starts at midnight...)

# I'm just plotting points here, but you could just as easily use a bar.

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot_date(x, times, 'ro')

ax.yaxis_date()

fig.autofmt_xdate()

plt.show()

希望这有点帮助!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值