Plotting a PANDAS DF to a 3D line-graph with changing width

Multi tool use
Plotting a PANDAS DF to a 3D line-graph with changing width
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 Answer
1
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:
plot
scatter
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()
Sample output:
More information about markersize and size in plot and scatter
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Amazing! thanks :)
– Jon Nir
2 days ago