Plotly is a powerful and versatile data visualization library used by Data Science professionals to create interactive and visually appealing plots, charts, and graphs. It enables users to represent complex datasets in a graphical form, making it easier to comprehend patterns, trends, and relationships within the data. If you want to learn Plotly for Data Science, this article is for you. In this article, I’ll take you through a step-by-step practical guide to Plotly for Data Science.
What is Plotly?
Plotly is a powerful and versatile data visualization library used by Data Science professionals to create interactive and visually appealing plots, charts, and graphs.
It offers a range of visualization types, including scatter plots, bar charts, line graphs, heatmaps, 3D surface plots, and various other plots. Plotly visualizations can be customized extensively to suit specific requirements, such as changing colours, sizes, and labels. Moreover, Plotly allows users to create interactive visualizations, enabling them to zoom in, pan, and get additional information by hovering over data points.
To install Plotly on your Python virtual environment, you can execute the command mentioned below in your terminal or command prompt:
- pip install plotly
A Practical Guide to Plotly for Data Science
In this section, I will take you through a practical guide to Plotly for Data Science. Let’s start by creating a simple scatter plot:
# creating a basic scatter plot import plotly.graph_objects as go # Sample data x_data = [1, 2, 3, 4, 5] y_data = [5, 2, 4, 1, 3] # Create a scatter plot fig = go.Figure() fig.add_trace(go.Scatter(x=x_data, y=y_data, mode='markers', name='Data Points')) fig.update_layout(title='Simple Scatter Plot', xaxis_title='X-axis', yaxis_title='Y-axis') fig.show()

The plot represents data points with coordinates given in two lists: x_data and y_data. Each data point is shown as a marker on the graph. The x-axis is labelled “X-axis”, the y-axis is labelled “Y-axis”, and the plot is titled “Simple Scatter Plot”.
The plotly.graph_objects module is imported as go (gives us access to functions to create various types of visualizations). A new figure (fig) is created to hold the scatter plot, and the Scatter function from graph_objects is used to add the data points as a trace to the figure. The mode parameter is set to “markers” to indicate that we want to represent the data points with markers.
Now let’s see how to customize this scatter plot:
fig.update_layout(title='Customized Scatter Plot', xaxis_title='X-axis Label', yaxis_title='Y-axis Label') fig.show()

Here the plot has been given the title of “Customized Scatter Plot”, and the x-axis is labelled as “X-axis Label”, while the y-axis is labelled as “Y-axis Label”.
And here’s how to change the colours and size of the markers in the plot:
fig.update_traces(marker=dict(color='blue', size=10)) fig.show()

You can also adjust the height and width of the plot:
fig.update_layout(width=800, height=400) fig.show()

Now let’s see how to add traces to the plot:
# Sample data for two traces x_trace1 = [1, 2, 3, 4, 5] y_trace1 = [5, 2, 4, 1, 3] x_trace2 = [1, 2, 3, 4, 5] y_trace2 = [3, 4, 2, 5, 1] # Adding traces to the plot fig.add_trace(go.Scatter(x=x_trace1, y=y_trace1, mode='markers', name='Trace 1')) fig.add_trace(go.Scatter(x=x_trace2, y=y_trace2, mode='lines', name='Trace 2')) fig.show()

By adding multiple traces to the plot, we can compare the two datasets visually and understand any differences or similarities between them. Here we have two sets of data points represented by x_trace1, y_trace1, x_trace2, and y_trace2. Each set of data points will be shown as a different trace on the plot.
We can also create subplots, as shown below:
from plotly.subplots import make_subplots # Create subplots with 1 row and 2 columns fig = make_subplots(rows=1, cols=2) # Add a trace to the main plot fig.add_trace(go.Scatter(x=x_data, y=y_data, mode='markers', name='Data Points')) # Add traces to the subplots fig.add_trace(go.Scatter(x=x_trace1, y=y_trace1, mode='markers', name='Trace 1'), row=1, col=1) fig.add_trace(go.Scatter(x=x_trace2, y=y_trace2, mode='lines', name='Trace 2'), row=1, col=2) # Update layout for the entire figure fig.update_layout(title='Subplots Example', xaxis_title='X-axis', yaxis_title='Y-axis') # Show the figure fig.show()

Here we are creating a figure with two subplots arranged side by side. Each subplot will display different data using scatter plots. Additionally, the main plot will also contain a scatter plot for another set of data points. The first step is to create a figure with two subplots, arranged in a single row with two columns. This arrangement ensures that the subplots will be displayed side by side. Next, a scatter plot is added to the main plot. The data points represented by x_data and y_data will be shown as markers. This main scatter plot will help us understand the overall distribution of these data points.
In addition to the main plot, we add two more scatter plots as traces to the subplots. The first subplot, located in the first column of the main plot, will display the data points represented by x_trace1 and y_trace1. These data points will be shown as markers. The second subplot, located in the second column of the main plot, will display the data points represented by x_trace2 and y_trace2. These data points will be connected by lines, forming a line graph.
After adding all the traces to the subplots and main plot, we update the layout for the entire figure. The figure is given the title “Subplots Example”, and the x-axis and y-axis are labelled accordingly.
Now here’s how to create a simple bar chart using Plotly:
# Sample data for bar chart categories = ['A', 'B', 'C', 'D', 'E'] values = [10, 8, 5, 12, 6] # Create a bar chart fig = go.Figure(data=[go.Bar(x=categories, y=values)]) fig.update_layout(title='Bar Chart Example', xaxis_title='Categories', yaxis_title='Values') fig.show()

And here’s how to create a heatmap:
import plotly.figure_factory as ff import pandas as pd # Sample data for heatmap data = pd.DataFrame([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Create a heatmap fig = ff.create_annotated_heatmap(data.values, x=list(data.columns), y=list(data.index)) fig.update_layout(title='Heatmap Example') fig.show()

Plotly is popularly used for 3D visualizations as well. Let’s see how to create a 3D Surface plot using Plotly:
import numpy as np # Sample data for 3D surface plot x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(x, y) Z = np.sin(np.sqrt(X**2 + Y**2)) # Create a 3D surface plot fig = go.Figure(data=[go.Surface(z=Z, x=x, y=y)]) fig.update_layout(title='3D Surface Plot') fig.show()

To create the surface plot (surface plot represents a mathematical function in three dimensions), we first generated sample data points for the x and y coordinates using NumPy. The linspace function from NumPy generated 100 equally spaced points between -5 and 5 for both the x and y axes. These points are used to create a grid using meshgrid, which creates a two-dimensional grid of points in the x-y plane.
Next, we compute the corresponding z-values for each point on the grid using a mathematical function. In this case, we calculate Z as the sine of the square root of the sum of the squares of the x and y coordinates. This mathematical function gives us the z-values for each point on the surface. With the data points ready, we create the 3D surface plot using the go.Figure function from Plotly. The Surface function from Plotly’s graph_objects module is used to create the surface plot, with x, y, and z parameters specifying the data for the x, y, and z coordinates, respectively.
So these were some of the Plotly operations you should know while getting started with Plotly for Data Science. You can explore more Plotly operations and how to plot different types of graphs from the official documentation of Plotly here.
Summary
Plotly is a powerful and versatile data visualization library used by Data Science professionals to create interactive and visually appealing plots, charts, and graphs. It offers a range of visualization types, including scatter plots, bar charts, line graphs, heatmaps, 3D surface plots, and various other plots. Plotly visualizations can be customized extensively to suit specific requirements, such as changing colours, sizes, and labels. Moreover, Plotly allows users to create interactive visualizations, enabling them to zoom in, pan, and get additional information by hovering over data points.
I hope you liked this article on a practical guide to Plotly for Data Science. Feel free to ask valuable questions in the comments section below.