Matplotlib3D отображение временной оси
Не получается сделать временную ось вылазит ошибка:
Traceback (most recent call last): File "c:/Users/Professional/Desktop/History_spectrs/History_spectrs/bin/Debug/kascad.py", line 60, in ax.plot(xlist, ylist, zlist,"-k",linewidth = 0.5) File "C:\Users\Professional\AppData\Local\Programs\Python\Python37\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1428, in plot self.auto_scale_xyz(xs, ys, zs, had_data) File "C:\Users\Professional\AppData\Local\Programs\Python\Python37\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 496, in auto_scale_xyz np.column_stack([X, Y]), not had_data) File "C:\Users\Professional\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\transforms.py", line 875, in update_from_data_xy path = Path(xy) File "C:\Users\Professional\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\path.py", line 127, in init vertices = _to_unmasked_float_array(vertices) File "C:\Users\Professional\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\cbook__init__.py", line 1317, in _to_unmasked_float_array return np.asarray(x, float) File "C:\Users\Professional\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core_asarray.py", line 85, in asarray return array(a, dtype, copy=False, order=order) TypeError: float() argument must be a string or a number, not 'datetime.datetime'
В принципе ошибка понятна, но как исправить не знаю. Гуглил, но ничего не помогло.
#выборка из бд
records = cursor.fetchall()
for row in records:
zlist.append(row[0])
ylist.append(row[1])
#d =row[3]
#date = DT.datetime.strptime(str(d),'%Y-%m-%d %H:%M:%S')
#xlist.append(pd.to_datetime(row[3]))
xlist.append(row[3])
if (row[1] == 2046): #конец одного графика
ax.plot(xlist, ylist, zlist,"-k",linewidth = 0.5)
temp.append(i)
i += 2
xlist.clear()
ylist.clear()
zlist.clear()
label.append(str(row[3]))
print(row[3])
# make labels
cursor.close()
conn.close()
fig.autofmt_xdate()
#plt.xticks(temp,label,rotation=90)
#locator = matplotlib.ticker.MaxNLocator ()
#ax.xaxis.set_major_locator (locator)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
Пытаюсь построить график как на картинке.
Ответы (1 шт):
import datetime
from time import sleep
import numpy
import pandas
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plot
import matplotlib
now = datetime.datetime.now()
date_time_now = now.strftime("%d.%m.%Y %H:%M:%S")
date_time = []
for _ in range(10):
date_time.append(date_time_now)
sleep(1)
dates_formatted = [pandas.to_datetime(_) for _ in date_time ]
print(date_time_now)
figure = plot.figure()
axes = figure.add_subplot(111, projection='3d')
#axes.plot(x, y, z,"-k",linewidth = 0.5)
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
xs = numpy.arange(200)
ys = numpy.random.rand(200)
cs = [c] * len(xs)
cs[0] = 'c'
axes.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)
axes.xaxis.set_ticks(xs)
axes.xaxis.set_ticklabels(dates_formatted)
locator = matplotlib.ticker.MaxNLocator ()
axes.xaxis.set_major_locator(locator)
axes.set_xlabel('X')
axes.set_ylabel('Y')
axes.set_zlabel('Z')
plot.show()
