用变化宽度绘制PANDAS数据框的3D折线图

2024-09-30 00:22:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含以下数据的PANDAS数据框:

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  

我想创建一个三维图形,显示在每次测量(测向的索引)时凝视的位置(x/y坐标)。此外,我还试图将其制作成一个线图,以便线的半径与瞳孔半径相对应。
到目前为止,我得出的结论是:

^{pr2}$

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

  1. 如何使数据点大小不一?在
  2. 有没有一种方法可以创建连续的线图而不是散点图?在

Tags: columns数据dataframepandasnp半径randomuniform
1条回答
网友
1楼 · 发布于 2024-09-30 00:22:28

第二个问题很简单,因为您可以使用plot,而不是{}。plot有一个参数markersize,这很好,但是如果这个参数不取一个序列,那就不好了。但我们可以通过分别绘制线图和标记来模拟其行为:

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

相关问题 更多 >

    热门问题