html tool

2016年3月7日星期一

matplotlib的一个顺序问题

测试matplotlib的横纵坐标时间日期格式定义

遇到好奇怪的问题,一定要这个顺序
import matplotlib
matplotlib.use('Agg')

from matplotlib.pyplot import plot,savefig
import pylab as pl
否则运行时是提示如下
 File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable


关于时间x轴的定义demo如下:

参考http://www.zeuux.com/group/scipython/bbs/content/51470/
---------------------------------------------------------------------------
用loadtxt载入数据时,用object作为元素类型。
然后将日期和时间相加,并转换成列表,然后调用pl.datestr2num将日期字符串转换成一个数值。
绘图时调用plot_date()。对于日期和时间的格式化,请参考matplotlib网站的演示程序。
# -*- coding: utf-8 -*-

import numpy as np
import pylab as pl

t = np.loadtxt("data.txt", np.object)
date = pl.datestr2num( list(t[:,0] + " " + t[:,1]) )
value = t[:,2].astype(np.float)

pl.plot_date(date, value)
pl.show()
------------------------------------------------------------------------------------
[popexizhi]
关于np.loadtxt加载速度问题,参考http://stackoverflow.com/questions/14985233/load-text-file-as-strings-using-numpy-loadtxt
Is it essential that you need a numpy array? Otherwise you could speed things up by loading the data as a nested list.
def load(fname):
    ''' load the file using std open'''
    f = open(fname,'r')

    data = []
    for line in f.readlines():
        data.append(line.replace('\n','').split(' '))

    f.close()

    return data
For a textfile with 4000x4000 words this is about 10 times faster than loadtxt.
[popexizhi ]没有自己测试但是感觉大数据这个会有问题,[next]测试一下,有结果的blog一下

没有评论:

发表评论