CodeGym /Courses /Python SELF EN /Introduction to Interactive Graphs with Plotly for Dynami...

Introduction to Interactive Graphs with Plotly for Dynamic Reports

Python SELF EN
Level 42 , Lesson 2
Available

1. Getting to Know Plotly

Intro to Interactive Visualization

Welcome to the dark side of the force! Today, we’ll level up our data visualization skills by adding a touch of interactivity with the Plotly library.

So, imagine you’re throwing a party, and your static charts are the guests you’ve already chatted with. They’re useful, but you’re craving lively conversations. That’s where Plotly comes in, bringing your charts to life—reacting and even entertaining you. With Plotly, you can zoom, pan, and interact with your data, making analysis deeper and more visual.

What is Plotly and Why Use It

Plotly is a multifunctional library for creating charts, offering high-level interactivity. It’s ideal when you want your charts to be more intuitive and easy to explore. Plotly is used in a variety of fields: from business to scientific research, providing powerful tools for data visualization in a web browser.

In practice, this can be useful when you need to present data to decision-makers who care about the details, or when you want to create an interactive analytics dashboard. For example, in marketing, interactive charts make it easier to analyze customer data, and in science, they help explore experimental data.

Comparing Matplotlib and Plotly

Now let’s figure out how Plotly is different from Matplotlib. Matplotlib is a solid tool for creating static, print-oriented charts when you need strict and controlled visualizations. However, in an age where interactivity is increasingly important, Plotly takes the lead. Unlike Matplotlib, Plotly makes it easy to create interactive charts that can be embedded directly into web pages.

It’s like comparing an art album to a magical book. In the album, you can enjoy the pages and images, but in the magical book, you can flip through pages that tell you stories as they come to life before your eyes. (This lecture was written before movies were invented :)

Installing and Setting Up Plotly

Let’s get down to business and set up our environment to work with Plotly.

Installing Plotly: Like most awesome things in Python, Plotly can be installed via pip. Open your command line or terminal and run the following command:

Bash

pip install plotly

Importing Libraries: After installation, to start plotting, let’s import the necessary libraries:

Python

import plotly.express as px
import plotly.graph_objects as go

Where plotly.express is a simpler API for quickly creating plots, and plotly.graph_objects offers more flexibility for complex visualizations.

2. Your First Interactive Chart

Creating Your First Interactive Chart

Now that we’ve got everything we need, let’s create our first interactive chart. We’ll start with a simple example—a line chart.

Python

import plotly.express as px
import pandas as pd

# Example data
data = pd.DataFrame({
    "Date": pd.date_range(start="2023-01-01", periods=7),
    "Sales": [150, 230, 270, 300, 190, 210, 280]
})

# Creating an interactive chart
fig = px.line(data, x="Date", y="Sales", title="Sales Over the Week")
fig.show()

This code will create a line chart that you can explore, zoom into, and pan around. Thanks to its interactivity, you can better focus on the areas of data that interest you.

Adjusting Interactivity

Plotly makes it easy to add interactive elements. For instance, you can enable data highlighting, zooming, and panning:

Python

fig.update_layout(
    xaxis=dict(rangeslider=dict(visible=True)),
    title=dict(x=0.5)  # Center the title
)
fig.show()

Here we’ve enabled a range slider and centered the title. This makes your chart more user-friendly and adaptable.

3. Creating Different Types of Interactive Charts

Plotly supports a wide array of interactive chart types. Here are some of them:

Scatter Plot

Scatter plots are great for analyzing the correlation between two variables.

Python

import plotly.express as px

# Chart data
data = {
    "Time": [1, 2, 3, 4, 5, 6],
    "Temperature": [30, 32, 34, 33, 31, 29]
}

fig = px.scatter(data, x="Time", y="Temperature", title="Temperature Over Time")
fig.show()

Histogram

Histograms are useful for analyzing data distribution and spotting anomalies.

Python

import plotly.express as px

# Chart data
data = {
    "Grades": [3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 9, 10]
}

fig = px.histogram(data, x="Grades", title="Grade Distribution")
fig.show()

Pie Chart

Pie charts help display the percentage distribution of categories.

Python

import plotly.express as px

# Chart data
data = {
    "Category": ["A", "B", "C", "D"],
    "Share": [20, 30, 25, 25]
}

fig = px.pie(data, names="Category", values="Share", title="Category Share")
fig.show()

Real-World Applications

Plotly finds its application in various domains. Let’s consider a couple of examples.

  • Business Analytics: Plotly’s interfaces are often used for creating interactive dashboards where users can explore data by changing filters and analysis parameters on the fly.
  • Scientific Research: Researchers use Plotly to visualize multi-level data, simplifying pattern and anomaly identification in data.

You can also integrate Plotly charts into Jupyter Notebooks, web applications, and even A/B tests, making your research more accessible and visual.

If you want to dive deeper, be sure to check out the official Plotly documentation, where you’ll find plenty of examples and ideas. Now go ahead and create some interactive masterpieces! 🚀

1
Task
Python SELF EN, level 42, lesson 2
Locked
Creating a basic interactive graph
Creating a basic interactive graph
2
Task
Python SELF EN, level 42, lesson 2
Locked
Adding Interactive Elements
Adding Interactive Elements
3
Task
Python SELF EN, level 42, lesson 2
Locked
Comparison of Data by Two Parameters
Comparison of Data by Two Parameters
4
Task
Python SELF EN, level 42, lesson 2
Locked
Creating a sales category analysis dashboard
Creating a sales category analysis dashboard
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION