将PANDAS DF绘制到宽度变化的3D线图中

时间:2018-06-29 15:37:58

标签: python python-3.x pandas matplotlib plot

我有一个PANDAS DataFrame,其中包含以下数据:

DF0 = pd.DataFrame(np.random.uniform(0,100,(4,2)), columns=['x', 'y'])  
pupil_rads = pd.Series(np.random.randint(1,10,(4)))  
DF0["pupil_radius"] = pupil_rads  
DF0

[out:]
    x           y           pupil_radius
0   20.516882   15.098594   8
1   92.111798   97.200075   2
2   98.648040   94.133676   3
3   8.524813    88.978467   7  

我想创建一个3D图形,显示每次测量(DF的索引)中凝视指向的位置(x / y坐标)。另外,我正在尝试使其成为线图,以使线的半径与瞳孔半径相对应。
到目前为止,我想到的是以下内容:

gph = plt.figure(figsize=(15,8)).gca(projection='3d')
gph.scatter(DF0.index, DF0['x'], DF0['y'])
gph.set_xlabel('Time Stamp')
gph.set_ylabel('X_Gaze')
gph.set_zlabel('Y_Gaze')

这将创建一个3D散点图,几乎是我所需要的:

  1. 如何使数据点具有不同的大小?
  2. 是否可以创建连续线图而不是散点图?

1 个答案:

答案 0 :(得分:2)

仅第二个问题就很容易,因为您可以使用plot而不是scatterplot的参数markersize很好,但是afaik这个参数不带序列号,这很不好。但是我们可以通过分别绘制折线图和标记来模拟其行为:

import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
#reproducibility of random results
np.random.seed(0)

DF0 = pd.DataFrame(np.random.uniform(0,100,(4,2)), columns=['x', 'y'])  
pupil_rads = pd.Series(np.random.randint(1,10,(4)))  
#pupil^2 otherwise we won't see much of a difference in markersize
DF0["pupil_radius"] = np.square(pupil_rads)  

gph = plt.figure(figsize=(15,8)).gca(projection='3d')
#plotting red dotted lines with tiny markers
gph.plot(DF0.index, DF0.x, DF0.y, "r.--")
#and on top of it goes a scatter plot with different markersizes
gph.scatter(DF0.index, DF0.x, DF0.y, color = "r", s = DF0.pupil_radius, alpha = 1)
gph.set_xlabel('Time Stamp')
gph.set_ylabel('X_Gaze')
gph.set_zlabel('Y_Gaze')

plt.show()

示例输出:

enter image description here

More information about markersize and size in plot and scatter