从时代问题开始

时间:2013-05-06 11:30:12

标签: python datetime csv 3d matplotlib

我正在尝试在制作网格之后将时间绘制在y轴上以绘制3D图。然而,日期变得有趣,它应该至少是2012年。我认为从纪元开始的秒数正在搞乱事情。

csv文件内容:

Depth (m)   15.08.2012 15:39:09 15.08.2012 16:09:10 15.08.2012 16:39:10 15.08.2012 16:43:36 15.08.2012 16:46:18
0   53.218  52.804  52.865  51.202  51.59
0.128   53.107  52.709  52.414  52.141  51.627
1.143   52.205  51.88   51.664  51.766  50.598
2.159   51.026  50.846  50.842  51.258  50.046
3.174   50.061  50.055  50.457  50.19   49.909
4.189   49.092  49.31   49.586  50.068  49.611
5.205   48.611  49.08   49.313  49.81   49.912
6.22    50.14   50.784  51.066  51.321  51.748
7.236   51.22   51.899  52.472  52.459  53.621
8.251   50.66   51.324  51.879  52.18   52.209
9.263   51.017  51.651  52.342  53.097  52.854
10.274  50.486  51.156  51.829  52.226  51.73
11.285  48.656  49.256  49.774  50.328  49.903
12.297  46.009  46.333  46.575  47.406  46.924
13.308  43.793  43.773  43.752  43.845  44.047
14.32   42.27   42.135  42.163  42.527  42.559
15.331  41.622  41.512  41.735  41.815  41.735
16.342  41.189  41.233  41.414  41.203  41.457

注意它是; csv文件中的数字之间。不能让它来到这里。

from matplotlib import cm
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import time
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.dates import DateFormatter

fname = "C:\Users\zana.pepaj\Desktop\Temperature.csv"

depth_list = []
tempr_list = []
time_list = []

with open(fname) as fin:
   for ix, line in enumerate(fin):
       line = line.rstrip()
       line_list = line.split(';')
       if ix == 0:
           time_list = line_list[1:]
       else:
           depth_list.append(float(line_list[0]))
           tempr_list.append([float(t) for t in line_list[1:]])

timelist = []
timelist=[(time.mktime(time.strptime(x, "%d.%m.%Y %H:%M:%S"))) for x in time_list]

timetoday = time.mktime(time.localtime())

x = []
y = []
z = []
for j in range(len(time_list)):
    for i in range(len(depth_list)):
        x.append(depth_list[i])
        y.append(np.divide(timelist[j], 1000))
        z.append(tempr_list[i][j])

# Twice as wide as it is tall.
fig = plt.figure(figsize=plt.figaspect(0.5))

#---- First subplot
ax = fig.add_subplot(1, 1, 1, projection='3d')

xi = np.linspace(min(x), max(x), 10)
yi = np.linspace(min(y), max(y), 10)

X, Y = np.meshgrid(xi, yi)
Z = griddata(x, y, z, xi, yi)

ax.yaxis.set_major_formatter(DateFormatter('%H:%M:%S\n%m/%d/%y'))

#surf = ax.plot_surface(X, Y, Z, rstride=6, cstride=6, cmap=cm.jet, linewidth=0)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.gist_rainbow, linewidth=0, antialiased=False)

fig.colorbar(surf)
plt.show()

这是我得到的结果:

http://s1345.photobucket.com/user/zanapepa/media/Untitled_zps84ba310f.png.html

看日期完全错误,它们应该是2012年。

1 个答案:

答案 0 :(得分:1)

我认为你必须使用epoch2num。那就是:

而不是

y.append(np.divide(timelist[j], 1000))

尝试

from matplotlib.dates import epoch2num
y.append(epoch2num(timelist[j]))