0

I have a PANDAS DataFrame with the following data:

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  

I want to create a 3D graph, showing where the gaze was pointed at (x/y coordinates) in every measurement (index of the DF). Also, I'm trying to make it a line-graph so that the radius of the line would correspond with the pupil-radius.
So far what I've come up with is the following:

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')

This creates a 3D scatter-plot, which is almost what I need:

  1. How do I make the data-points be in varying size?
  2. Is there a way to create a continuous-line-graph and not a scatter-plot?
Jon Nir
  • 507
  • 3
  • 15

1 Answers1

2

The second question alone would be easy because you can use plot instead of scatter. plot has the parameter markersize, which is good, but afaik this parameter doesn't take a series, which is bad. But we can emulate its behavior by plotting a line graph and markers separately:

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()

Sample output:

enter image description here

More information about markersize and size in plot and scatter

Mr. T
  • 11,960
  • 10
  • 32
  • 54