假设我有3个数据框,其中包含对象的纬度,经度和时间标签信息,这样每列代表一个不同的对象,每一行代表一个时间点。
latitudes =
object 1 2 3
0 -8.064267 -8.047483 -8.056339
1 -8.064267 -8.047483 -8.056339
2 -8.064267 -8.047483 -8.056339
3 -8.064267 -8.047483 -8.056339
4 -8.064267 -8.047483 -8.056339
5 -8.064267 -8.047483 -8.056339
longitudes =
object 1 2 3
0 -34.878386 -34.904086 -34.889661
1 -34.878386 -34.904086 -34.889661
2 -34.878386 -34.904086 -34.889661
3 -34.878386 -34.904086 -34.889661
4 -34.878386 -34.904086 -34.889661
5 -34.878386 -34.904086 -34.889661
times =
object 1 2 3
0 2016-03-05 07:52:00 2016-03-05 16:26:00 2016-03-05 16:58:00
1 2016-03-05 08:19:00 2016-03-05 16:42:00 2016-03-05 17:45:00
2 2016-03-05 08:52:00 2016-03-05 17:06:00 2016-03-05 17:58:00
3 2016-03-05 09:36:00 2016-03-05 18:21:00 2016-03-05 18:23:00
4 NaT 2016-03-05 23:06:00 2016-03-05 22:38:00
5 NaT 2016-03-05 23:09:00 NaT
我想绘制一个3D轨迹,z轴上有时间,x轴上有经度,y轴上有纬度,所有对象都在这里。轨迹在同一个地块上。我怎么能这样做?
这是我的尝试,但不起作用:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot(longitudes.values,latitudes.values,times.values)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('Time')
plt.show()
错误:
ValueError: third arg must be a format string
谢谢
答案 0 :(得分:3)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('Time')
for t in times.columns:
ax.plot(longitudes[t].values, latitudes[t].values, times[t].values, label='t')
ax.legend()
答案 1 :(得分:1)
您需要过滤掉NaT值,因为它们无法绘制。
latitudes = u"""
-8.064267 -8.047483 -8.056339
-8.064267 -8.047483 -8.056339
-8.064267 -8.047483 -8.056339
-8.064267 -8.047483 -8.056339
-8.064267 -8.047483 -8.056339
-8.064267 -8.047483 -8.056339"""
longitudes = u"""
-34.878386 -34.904086 -34.889661
-34.878386 -34.904086 -34.889661
-34.878386 -34.904086 -34.889661
-34.878386 -34.904086 -34.889661
-34.878386 -34.904086 -34.889661
-34.878386 -34.904086 -34.889661"""
times = u"""
2016-03-05 07:52:00, 2016-03-05 16:26:00, 2016-03-05 16:58:00
2016-03-05 08:19:00, 2016-03-05 16:42:00, 2016-03-05 17:45:00
2016-03-05 08:52:00, 2016-03-05 17:06:00, 2016-03-05 17:58:00
2016-03-05 09:36:00, 2016-03-05 18:21:00, 2016-03-05 18:23:00
2016-03-05 09:36:00, 2016-03-05 23:06:00, 2016-03-05 22:38:00
2016-03-05 09:36:00, 2016-03-05 23:09:00, 2016-03-05 09:36:00
"""
import io
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
kw = dict(delim_whitespace=True, header=None)
latitudes = pd.read_csv(io.StringIO(latitudes),**kw)
longitudes = pd.read_csv(io.StringIO(longitudes),**kw)
times = pd.read_csv(io.StringIO(times), delimiter=",",
infer_datetime_format=True,header=None)
times.iloc[4:,0] = None
times.iloc[5,2] = None
for i in times.columns:
times[i] = pd.to_datetime(times[i], format="%Y-%m-%d %H:%M:%S")
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
for t in times.columns:
inx = times[t].values != None
x = longitudes[t].values[inx]
y = latitudes[t].values[inx]
z = times[t].values[inx]
ax.plot(x,y,z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('Time')
plt.show()