Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matplotlib和timestamp正在引发问题_Matplotlib_Plot_Timestamp_Unix Timestamp - Fatal编程技术网

Matplotlib和timestamp正在引发问题

Matplotlib和timestamp正在引发问题,matplotlib,plot,timestamp,unix-timestamp,Matplotlib,Plot,Timestamp,Unix Timestamp,为什么这不起作用?如何修复它?我试图绘制一个图形,其中x轴是时间轴,y值是csv文件中的第二列。我希望x打印标签的格式更易于阅读 testi.csv 睾丸2.py 这是我给出“python>>>导入numpy>>>>numpy。_u版本_u'1.6.1'我认为该错误在1.6.1版本后已修复。您应该更新numpy安装以避免此错误,或者您可以如上所述定义一个函数。 import numpy as np import matplotlib matplotlib.use('Agg') import ma

为什么这不起作用?如何修复它?我试图绘制一个图形,其中x轴是时间轴,y值是csv文件中的第二列。我希望x打印标签的格式更易于阅读

testi.csv 睾丸2.py 这是我给出“python
回溯(最近一次呼叫最后一次):
文件“”,第13行,在
savefig中的文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/pyplot.py”,第564行
返回图savefig(*args,**kwargs)
savefig中的文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/figure.py”,第1421行
self.canvas.print_图(*args,**kwargs)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/backend_base.py”,第2220行,如图所示
**kwargs)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/backends/backend_agg.py”,第505行,打印格式为png
图CAVASAGG.draw(自绘制)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/backends/backend_agg.py”,绘图中第451行
self.figure.draw(self.renderer)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/artist.py”,第54行,在draw_包装中
绘制(艺术家、渲染器、*args、**kwargs)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/figure.py”,绘图中的第1034行
func(*args)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/artist.py”,第54行,在draw_包装中
绘制(艺术家、渲染器、*args、**kwargs)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/axes/_base.py”,绘图中的第2056行
a、 绘制(渲染器)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/artist.py”,第54行,在draw_包装中
绘制(艺术家、渲染器、*args、**kwargs)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/axis.py”,绘图中的第1087行
勾号\u到\u绘制=自。\u更新\u勾号(渲染器)
文件“/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/axis.py”,第945行,在更新中
tick_tups=[t代表self.iter_ticks()中的t]
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/axis.py”,第893行,在iter_中
对于i,枚举中的val(主要)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/dates.py”,第395行,在调用中__
dt=num2date(x,self.tz)
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/dates.py”,第329行,单位为num2date
从顺序lf(x,tz)返回
文件“/usr/local/lib/python2.7/dist packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/dates.py”,第218行,从
dt=datetime.datetime.fromordinal(ix)
ValueError:年份超出范围

您必须将时间戳转换为
datetime
对象

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime as dt

# create vectorized converter (can take list-like objects as arguments)
dateconv = np.vectorize(dt.datetime.fromtimestamp)

data = np.genfromtxt('testi.csv', delimiter=',', names=['t', 'btc', 'eur'])
plt.xkcd()

dates = dateconv(data['t']) # convert timestamps to datetime objects

plt.plot(dates, data['btc'], label='the data')
plt.xticks(dates, rotation=75, ha='right') # set ticks at plotted datetimes
ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)

plt.tight_layout() # to make sure everything fits inside the figures boundaries

plt.savefig('testi2.png')

为了使为绘图设置的标签出现,必须在
plt.savefig('testi2.png')
之前调用
plt.legend()
。使用
plt.legend(loc=2)
(将图例放在左上角)时,输出变为:


谢谢!太好了!:)实际上,它仍然给出了一些错误:Traceback(最后一次调用):File“”,第9行,在文件“/usr/lib/python2.7/dist-packages/numpy/lib/function\u-base.py”中,第1818行,在init-nin中,ndefault=\u-get-nargs(pyfunc)文件“/usr/lib/python2.7/dist-packages/numpy/lib/function\u-base.py”,第1750行,在\u-get-nargs中无法确定%s“%的参数数(obj))ValueError:无法确定任何想法的参数数量?您使用的是什么版本的numpy?您可以尝试更新您的numpy安装。我相信这是一个已修复的错误。另外,请参阅。或者,您可以定义一个函数,将numpy数组作为参数,转换数组中的元素,并返回t他转换了数组。>>>>导入numpy>>>>numpy。_u版本_u'1.6.1'我认为该错误在1.6.1版本后已修复。您应该更新numpy安装以避免此错误,或者您可以如上所述定义一个函数。
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as md
data = np.genfromtxt('testi.csv', delimiter=',', names=['t', 'btc', 'eur'])
plt.xkcd()
plt.plot(data['t'], data['btc'], label='the data')
plt.xticks(rotation=75)
ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.savefig('testi2.png')
Traceback (most recent call last):
  File "<stdin>", line 13, in <module>
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/pyplot.py", line 564, in savefig
    return fig.savefig(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/backend_bases.py", line 2220, in print_figure
    **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/backends/backend_agg.py", line 505, in print_png
    FigureCanvasAgg.draw(self)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/figure.py", line 1034, in draw
    func(*args)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/axes/_base.py", line 2056, in draw
    a.draw(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/axis.py", line 1087, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/axis.py", line 945, in _update_ticks
    tick_tups = [t for t in self.iter_ticks()]
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/axis.py", line 893, in iter_ticks
    for i, val in enumerate(majorLocs)]
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/dates.py", line 395, in __call__
    dt = num2date(x, self.tz)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/dates.py", line 329, in num2date
    return _from_ordinalf(x, tz)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.4.x-py2.7-linux-i686.egg/matplotlib/dates.py", line 218, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix)
ValueError: year is out of range
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime as dt

# create vectorized converter (can take list-like objects as arguments)
dateconv = np.vectorize(dt.datetime.fromtimestamp)

data = np.genfromtxt('testi.csv', delimiter=',', names=['t', 'btc', 'eur'])
plt.xkcd()

dates = dateconv(data['t']) # convert timestamps to datetime objects

plt.plot(dates, data['btc'], label='the data')
plt.xticks(dates, rotation=75, ha='right') # set ticks at plotted datetimes
ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)

plt.tight_layout() # to make sure everything fits inside the figures boundaries

plt.savefig('testi2.png')