Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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
Python 需要matplotlib中按日期时间序列进行三维打印的帮助吗_Python_Numpy_Matplotlib - Fatal编程技术网

Python 需要matplotlib中按日期时间序列进行三维打印的帮助吗

Python 需要matplotlib中按日期时间序列进行三维打印的帮助吗,python,numpy,matplotlib,Python,Numpy,Matplotlib,我想在下面绘制3D图像: 但出现以下错误消息: TypeError:float()参数必须是字符串或数字,而不是“datetime.timedelta” 我该怎么做呢?虽然有一个函数可以在datetime.timedelta上操作,但在您的情况下,您不能在3D中使用该函数。相反,您可以通过使用将日期转换为数字来创建一个int/float数组来表示该数据。然后,您可以按所需格式设置记号标签。我使用了下面的默认timedelta import numpy as np import matplotl

我想在下面绘制3D图像:

但出现以下错误消息: TypeError:float()参数必须是字符串或数字,而不是“datetime.timedelta”

我该怎么做呢?

虽然有一个函数可以在
datetime.timedelta
上操作,但在您的情况下,您不能在3D中使用该函数。相反,您可以通过使用将日期转换为数字来创建一个int/float数组来表示该数据。然后,您可以按所需格式设置记号标签。我使用了下面的默认
timedelta

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.gca(projection = '3d')

x = np.zeros((2, 24), dtype = 'datetime64[h]')
x[0,  : ] = np.arange('2020-02-27', '2020-02-28', dtype = 'datetime64[h]')
x[1,  : ] = np.arange('2020-02-28', '2020-02-29', dtype = 'datetime64[h]')

y = np.zeros((2, 24), dtype = 'datetime64[D]')
y[0, : ] = np.array(['2020-02-27' for i in range(24)])
y[1, : ] = np.array(['2020-02-28' for i in range(24)])

z = np.zeros((2, 24))
z[0,  : ] = np.arange(24)
z[1,  : ] = np.arange(24)

surf = ax.plot_surface(x, y, z)

plt.show()
结果如下。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.dates as dates

fig = plt.figure()
ax = fig.gca(projection = '3d')

x = np.zeros((2, 24), dtype = 'datetime64[h]')
x[0,  : ] = np.arange('2020-02-27', '2020-02-28', dtype = 'datetime64[h]')
x[1,  : ] = np.arange('2020-02-28', '2020-02-29', dtype = 'datetime64[h]')
# convert the datetime to num
xt = [[dates.date2num(d) for d in xi] for xi in x]

y = np.zeros((2, 24), dtype = 'datetime64[D]')
y[0, : ] = np.array(['2020-02-27' for i in range(24)])
y[1, : ] = np.array(['2020-02-28' for i in range(24)])
# convert the datetime to num
yt = [[dates.date2num(d) for d in yi] for yi in y]

z = np.zeros((2, 24))
z[0,  : ] = np.arange(24)
z[1,  : ] = np.arange(24)

surf = ax.plot_surface(xt, yt, z)
# set x labels as diff in hours
ax.set_xticklabels(x[0]-min(x[0]))
# set y labels as the two dates 
ax.set_yticklabels([y[0][0],'','','','',y[1][0]])
plt.show()