作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么这不起作用?如何解决?我试图绘制一个图形,其中 x 轴是时间轴,y 值是 csv 文件中的第二列。我希望 x 绘图标签采用更易于阅读的格式...
测试文件
1315655275.0,0.1,1.4
1325655275.0,0.11,2.2
1335655275.0,0.23,11.96
1345655275.0,0.81,63.18
1355655275.0,2.76,560.28
1365655275.0,5.54,609.4
1375655275.0,7.21,576.8
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
最佳答案
您必须将时间戳转换为 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.legend()
之前
plt.savefig('testi2.png')
.与
plt.legend(loc=2)
(将图例放在左上角)输出变为:
关于Matplotlib 和时间戳造成麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18062457/
给定一个带有多个 date_time 戳的字符串,我想 提取第一个戳及其前面的文本 候选字符串可以有一个或多个时间戳 后续的 date_time 戳记将被 sep="-" 隔开 后续date_time
是否可以合并从相机拍摄的文本和照片?我想在照片上标记日期和时间,但我在 Google 上找不到任何内容。 最佳答案 使用下面的代码来实现你所需要的。 Bitmap src = Bitm
有没有办法通过 Graph API 戳另一个用户?基于this post ,并使用 Graph Explorer ,我发布到“/USERID/pokes”,我已经授予它(Graph API 应用程序和
我有两个向左浮动的元素。一个是 body 的第一个 child ,另一个是容器的第一个 child ,容器是 body 的第二个 child 。 ...
我是一名优秀的程序员,十分优秀!