Creating Interactive Plots with Plotly
Welcome to PythonTimes.com, your one-stop source for everything Python! Today, we delve into the world of Python’s infamous data visualization library, Plotly. Whether you are a rookie Python programmer or a veteran data analyst, Plotly offers vast features to enhance your data storytelling capabilities.

With Plotly, not only you can create static plots, but also interactive plots that enable better engagement with your data. The library provides multiple options, from line and scatter plots to bar graphs, pie charts, heat maps, network graphs, and more. Let’s get started!
What is Plotly?
Plotly is a well-renowned Python graphing library that allows users to generate interactive, visually stimulating charts and plots. The plots are web-embeddable and can help you create dashboards, GUIs, and other graphical depicting methods. Plotly’s flexibility and wide-ranging plot types put it a step above many other plotting libraries.
Installation of Plotly
The first step towards creating vivid, interactive plots is the installation of Plotly in your python environment. This can be done easily using pip:
pip install plotly
Or if you’re using conda, use the following command:
conda install -c plotly plotly
Getting Started With Plotly
Let’s dive in by creating a simple scatter plot using Plotly.
import plotly.express as px
# Creating a DataFrame
df = pd.DataFrame({
"Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
"Amount": [10, 15, 7, 10, 5, 10],
"City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})
# Plotting the Data
fig = px.scatter(df, x="Fruit", y="Amount", color="City")
fig.show()
In the above snippet, px.scatter
creates an interactive scatter plot wherein you can zoom in/out, hover around to look at the values, and even save the plot as a PNG.
Detailed Plotting with Plotly
Scatter Plots
A scatter plot displays data as a collection of points on a graph, each representing a single observation in a dataset. The positions of these points are determined by the values of two variables in your dataset, one plotted on the x-axis and the other plotted on the y-axis.
Let’s illustrate with an example:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", size='petal_length', hover_data=['petal_width'], title="Iris Dataset Scatter Plot")
fig.show()
Bar Plots
A bar plot or bar chart is a way of presenting data in a categorical manner. Bar plots can be useful for comparing the frequency or other measures (such as mean) across different groups.
Ana example with a “tips” dataset follows:
import plotly.express as px
df = px.data.tips()
fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group", title="Total Bill Amount grouped by Gender and Smoker")
fig.show()
Pie Charts
Pie charts are effective for showing the proportional composition of categories within a single variable.
import plotly.express as px
df = px.data.tips()
fig = px.pie(df, values='tip', names='day', title="Pie Chart of Tips by Days")
fig.show()
Customizing Plotly Plots
Plotly allows you to customize plots to tailor fit your needs. This includes modifying the colors, sizes, titles, labels, and legend of your plot.
Let’s take a look at scatter plot customization:
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", title="Iris Dataset Scatter Plot")
fig.update_traces(marker=dict(size=12,
line=dict(width=2,
color='DarkSlateGrey')),
selector=dict(mode='markers'))
fig.update_layout(title='Updated Scatter Plot',
xaxis=dict(title='Sepal Width'),
yaxis=dict(title='Sepal Length'))
fig.show()
You’ll notice how we used update_traces
and update_layout
methods to modify elements on the plot.
Conclusion: Plotly – A Go-to Library for Data Visualization
This hands-on tutorial revealed the dynamic and interactive capabilities of Plotly, equipping you with the know-how to craft robust graphs and charts in Python. By merging data with design, we’re able to unlock richer understanding and insights from our dataset.
Plotly’s flexibility—expressed through the extensive variety of graphs it supports, ease of use, customization capabilities, and ability to handle different data structures—make it a fundamental tool for data visualization. Get interactive with your data and step your analytics game up a notch using Plotly.