6-Three Dimensional Plot Using Plotly
6-Three Dimensional Plot Using Plotly
1. Introduction.
Plotly is a data visualization library that provides a wide range of tools for creating
interactive plots in Python, as well as in other programming languages like R and
JavaScript. It is designed to enable the creation of visually appealing and interactive charts,
graphs, and dashboards for data analysis and presentation.
This Python program uses the Plotly library to construct an interactive 3D scatter plot,
illustrating student data. The primary objective is to depict the correlation between
students' height, weight, and age within a three-dimensional space. The Plotly
graph_objects module is employed to create the plot, and the offline module is used to
generate the resulting interactive HTML file locally.
The program also generates hover text for each data point, giving detailed information
about each student. The Scatter3d trace from Plotly is utilized to craft the 3D scatter plot,
and the layout is enhanced by incorporating axis titles and a main title. The final plot is
then saved as an HTML file, offering an engaging and informative visualization of the
intricate relationships among students' height, weight, and age.
2. Program code.
# Dataset
students_data = {
'Name': ['Anil', 'Sunil', 'Kumar', 'Rajesh', 'Naveen'],
'Height': [160, 155, 170, 165, 175],
'Weight': [55, 50, 65, 60, 70],
'Age': [20, 19, 21, 20, 22]
}
# Create hover text with student names
hover_text = [f'Name: {name}<br>Height: {height}<br>Weight:
{weight}<br>Age: {age}'
for name, height, weight, age in zip(students_data['Name'],
students_data['Height'],
students_data['Weight'], students_data['Age'])]
3a. Importing the necessary libraries for creating 3D plots using Plotly.
This line imports the graph_objects module from the Plotly library and assigns it the alias
go for convenience. The graph_objects module provides a high-level interface for creating
complex visualizations, including 3D plots. By using the as go alias, it simplifies the code
when referencing functions and classes from this module.
Plotly supports both online and offline modes for generating plots. In the offline mode, the
offline module provides functions to create and save plots without needing an active
internet connection. The plot function, in particular, is employed to visualize the plot
interactively and to save it as an HTML file locally. This HTML file can then be opened
in a web browser, allowing users to interact with and explore the plot without the need for
an internet connection.
students_data = {
hover_text = [...]:
Hover text is additional information that appears when a user hovers over a data point in a
plot, providing more details about that specific point.
This line initializes a list comprehension to create a list of formatted strings, where each
string represents the hover text for a corresponding student. The hover text includes the
student's name, height, weight, and age.
This part of the code uses the zip function to iterate over the lists of names, heights,
weights, and ages simultaneously. In each iteration, it unpacks the values for a specific
student.
This part formats a string using an f-string (formatted string literal). It combines the
values of name, height, weight, and age for each student, creating a string that includes
the student's name, height, weight, and age, separated by line breaks (<br>).
fig = go.Figure(data=[...]:
This line initializes a Figure object from Plotly's graph_objects module. The Figure
object is the top-level container for all visual elements in a Plotly plot.
data=[go.Scatter3d(...)]:
The data argument is a list containing trace objects that define the data and appearance
of the plot. In this case, the list contains a single trace, which is a Scatter3d trace. The
Scatter3d trace is specifically designed for 3D scatter plots.
x=students_data['Height'], y=students_data['Weight'], z=students_data['Age']:
These parameters specify the x, y, and z coordinates for each data point. In this case,
the x-axis represents heights, the y-axis represents weights, and the z-axis represents
ages. The data is extracted from the students_data dictionary.
mode='markers':
The 'markers' mode indicates that each data point should be represented as a marker in
the plot.
This parameter configures the appearance of the markers. They are set to be blue, have
a size of 10, and an opacity of 0.8 (80% transparency).
The resulting Figure object (fig) encapsulates the 3D scatter plot configuration.
fig.update_layout(scene=dict(xaxis_title='Height',yaxis_title='Weight',zaxis_title='Age'),
title='3D Scatter Plot of Student Data')
The update_layout method is used to modify the layout settings of the Figure object
(fig). It allows customization of various aspects of the plot's appearance.
scene=dict(xaxis_title='Height', yaxis_title='Weight', zaxis_title='Age'): This part of
the code configures the scene settings for the 3D plot. It uses a dictionary (dict) to
specify the titles for the x-axis, y-axis, and z-axis. In this case, the x-axis is labeled
'Height,' the y-axis is labeled 'Weight,' and the z-axis is labeled 'Age.'
title='3D Scatter Plot of Student Data': This parameter sets the main title of the plot.
The title is a string that describes the overall content or purpose of the plot. Here, it is
set to '3D Scatter Plot of Student Data.'
The above code updates the layout of a 3D scatter plot by configuring axis titles and adding a main
title using the update_layout method of the Plotly Figure object. These modifications contribute to
the clarity and interpretation of the visualized student data.
4. Output
Output Analysis:
1. Type of Chart:
2. Variables Mapped:
Height (x-axis)
Weight (y-axis)
Age (z-axis)
3. Data Points:
The position of each marker indicates their height, weight, and age values.
4. Hover Text:
When you hover over a marker, it displays the student's name, height, weight,
and age details.
Plotly is a versatile Python library for creating interactive visualizations. Here are
few use cases for Plotly.
1. Financial Dashboards:
Plotly is great for building real-time monitoring dashboards. You can use
it to display live streaming data, such as IoT sensor readings, server
performance metrics, or social media mentions. The interactivity features
allow users to zoom in on specific time periods and get detailed information
on the fly.