如何以自然可读的方式用python绘制与时间相关的信息?
通常,您有一组与时间值相关的事件(访问网站、价格信息等),比如时间戳(尽管datetime对象也可以)。如何绘制它们,使时间轴获得人类可读的、有意义的值,而不仅仅是秒数 我一直在寻找gnuplot和matplot,但我还没有找到这样做的方法。问题是,虽然matplot可以每小时设置一次滴答声,但最好每N小时查看一次文本时间信息,而不必进行计数如何以自然可读的方式用python绘制与时间相关的信息?,python,time,plot,Python,Time,Plot,通常,您有一组与时间值相关的事件(访问网站、价格信息等),比如时间戳(尽管datetime对象也可以)。如何绘制它们,使时间轴获得人类可读的、有意义的值,而不仅仅是秒数 我一直在寻找gnuplot和matplot,但我还没有找到这样做的方法。问题是,虽然matplot可以每小时设置一次滴答声,但最好每N小时查看一次文本时间信息,而不必进行计数 我怀疑gnuplot有点过头了/并不是专门为此而设计的。有什么建议吗?FWIW下面是一个简单的例子,使用优秀且有良好文档记录的matplotlib绘制一些
我怀疑gnuplot有点过头了/并不是专门为此而设计的。有什么建议吗?FWIW下面是一个简单的例子,使用优秀且有良好文档记录的matplotlib绘制一些时间值: data.csv: VISIT_TIME TOTAL_VISITS 06:00:00 290 06:30:00 306 07:00:00 364 07:30:00 363 08:00:00 469 08:30:00 436 09:00:00 449 09:30:00 451 10:00:00 524 10:30:00 506 11:00:00 613 11:30:00 585 12:00:00 620 12:30:00 529 13:00:00 588 13:30:00 545 输出如下图所示:
问题问的是相同或类似的问题。可能只是一个附加问题的重复。是否可以根据某些标准给点上色?@DavidPÁ答案是肯定的,您应该使用
color='g'
参数,而不是绘图函数中的color=matplotlib.cm.jet[value]
,其中jet
是许多可用的颜色贴图之一。或者沿着这条路线的东西;o)
import matplotlib.dates as mdates
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import datetime as dt
import sys
def main( datafile ):
np_rec_array = mlab.csv2rec( datafile, delimiter='\t' )
np_rec_array.sort() # in-place sort
# a `figure` is a starting point for MPL visualizations
fig = plt.figure( figsize=(8,6) )
# add a set of `axes` to above `figure`
ax = fig.add_subplot(111)
x = np_rec_array.visit_time
y = np_rec_array.total_visits
# `plot_date` is like `plot` but allows for easier x-axis formatting
ax.plot_date(x, y, 'o-', color='g')
# show time every 30 minutes
ax.xaxis.set_major_locator( mdates.MinuteLocator(interval=30) )
# specify time format
ax.xaxis.set_major_formatter( mdates.DateFormatter("%H:%M") )
# set x-axis label rotation (otherwise they can overlap)
for l in ax.get_xticklabels():
l.set_rotation(60)
plt.title( 'Website Visits' )
plt.show()
if __name__ == '__main__':
if len( sys.argv ) == 1:
sys.stderr.write( 'need a filename, exiting...' )
sys.exit(-1)
main( sys.argv[1] )