Create 3D Scatter Plot -- Python Plotly
Вставка
- Опубліковано 21 лип 2024
- Learn to create the 3D scatter plot in under 25 lines of code. We will also animate the plot, and save as html to share with others. Lastly, we will review when it is best to use or avoid the 3D plot. Open Links below...
View my book - The Book of Dash:
www.amazon.com/Python-Dash-An...
github.com/DashBookProject/Pl...
Code:
github.com/Coding-with-Adam/D...
Data:
drive.google.com/file/d/1MeFm...
Data source:
ourworldindata.org/
3d Scatter Plot Documentation:
plotly.com/python/3d-scatter-...
plotly.com/python-api-referen...
Plotly Marker symbol list:
plotly.com/python/reference/#...
Video Layout:
00:00 What you will learn
00:41 Goal of tutorial
03:31 the Data
05:41 the Code
07:16 Fundamental 1- Creating the 3D plot
11:38 Fundamental 2- Styling the 3D plot
22:46 Fundamental 3- Animation of 3D plot
30:03 Fundamental 4- Sharing your plot with others via HTML
31:55 Fundamental 5- Do's and Dont's of 3d plotting
Best tutorials on plotly !Thanks again
Thanks. Clearly explained and easy to follow.
Adam, thank you very much for your tutorials. Greetings from Argentina!
Saludos Geronimo. Vivé en Ecuador unos 3 años y siempre quería viajar a Argentina. Tal vez algún día.
@@CharmingData Adam, si algún día venís, avisame y hacemos una presentación de dash y plotly . Acá hay una gran comunidad de Python. Yo tambien espero algún día ir a visitar NY!😁💪
Thanks, it helped me a lot.
wow... after I watched this video... I totally did not want to learn about plotly anymore... I just wanted to know how to get those brilliant sounds that appeared when the bullet list continues in the beginning of the video :D
I want to ask is there any way to show the centroids plot (used in k-means) in the 3d scatter plot?
Hi, Your contents were so helpful!!
Is there a simple way to make a 3D-plot with same xyz-axis size? like a cube.
Thanks Gangmin, here's an example: chart-studio.plotly.com/~empet/14165.embed
Hi, thanks for good
explanation. Do you know how to disable axies rotation duriong rotation of the scene >?
Not sure. Sorry
very comprehensive
the scatter 3d doesn't show because of the log_x=True. What should i do? when i change the log into log_y and log_z it works
how to remove axis?
HEYY.... CAN YOU EXPLAIN OR HELP ME TO SAVE THIS AS ANIMATION VIDEO FILE !?
Keep teaching us bro, have you thought about tutorial on dash mobile app?
Thanks Johnny. I don't believe I'm aware of that. Is that a library? Or are you just thinking how to do Dash on mobile?
@@CharmingData idk, I just thinking of .
is there a way to highlight a selected point ?
Yes. You can listen to click data like this:
@app.callback(
[Output('click-data', 'children'),
Output('basic-interactions', 'figure')],
[Input('basic-interactions', 'clickData')],
[State('basic-interactions', 'relayoutData')])
def display_click_data(clickData, relayoutData):
if not clickData:
return dash.no_update, dash.no_update
point = clickData["points"][0]
# Do something only for a specific trace
if point["curveNumber"] > 0:
return dash.no_update, dash.no_update
else:
fig = go.Figure(go.Scatter3d(
x=np.arange(10),
y=np.arange(10),
z=np.arange(10),
mode='markers'))
sizes = 8 * np.ones(10)
sizes[point["pointNumber"]] = 15
colors = ['blue',]*10
colors[point["pointNumber"]] = 'red'
fig.update_traces(marker_size=sizes, marker_color=colors)
# Make sure the view/angle stays the same when updating the figure
if relayoutData and "scene.camera" in relayoutData:
fig.update_layout(scene_camera=relayoutData["scene.camera"])
return json.dumps(clickData, indent=2), fig
This is from community.plotly.com/t/highlighting-scatter3d-selection-via-callback-mesh3d-blocking-click-event/32006/2?u=adamschroeder
This is awesome and i have learnt a lot so far.... I have somewhat of big data and i was looking to displaying my high dimensionality data points through through t_SNE on plotly.... any tip on how i could go about that ?
Sorry, not sure about that, @Precious O.
Hi can I do this with RGB values?
X = [1,2,3,4]
Y = [1,2,3,4]
Z = [1,2,3,4]
C = np.array([[0, 0, 0], [0, 0, 128], [255, 255, 102],[0, 7, 65]])
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.scatter(X, Y, Z, c = C/255.0)
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
plt.show()
this but place my whole RGB colour list in C parameter? And changew X, Y, Z to n=(how many are in the list)
I'm trying to plot two sets of xyz data on the same 3D graph, Ik how to do this on pyplot but how can I do this on plotly? (I'm learning plotly bc I wanna share my graphs without sending people my whole code...)
So you're trying to plot two separate traces?
Charming Data Yes. Two traces on the same graph. They’re two sets of the same parameters but operating at different conditions, so it’s better to show them on one graph.
@@wadewen1008 you'll have to use the fig.add_trace() function with graph objects. For example, this is how I added a trace to my code in this tutorial.
fig.add_trace(go.Scatter3d(
x=df['% Econ. active'],
y=df['GDP per capita'],
z=df['Years in school (avg)'],
mode='markers',
marker=dict(
size=12,
color="black", # set color to an array/list of desired values
colorscale='Viridis', # choose a colorscale
opacity=0.8
)
))
Charming Data for the df when reading data from the excel file, can I do df1 df2, etc? Bc the datasets aren’t from the same excel sheet
@@wadewen1008 yes, that should be possible.
Ha, in every video we can hear some police sirens XD
Please, go straight to the point....
(5:50 You're welcome!)