【问题标题】:Plotting a PANDAS DF to a 3D line-graph with changing width将 PANDAS DF 绘制为宽度变化的 3D 线图
【发布时间】:2018-06-30 17:29:16
【问题描述】:

我有一个带有以下数据的 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. 有没有办法创建连续线图而不是散点图?

【问题讨论】:

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


    【解决方案1】:

    第二个问题很简单,因为您可以使用plot 而不是scatter。 plot 有参数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()
    

    样本输出:

    More information about markersize and size in plot and scatter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      • 2019-12-21
      相关资源
      最近更新 更多