Lesson

Introduction to Plotly

Learn Plotly fundamentals in SQLPad's Data Science in Action course with practical examples and guided lessons.

In this lesson, we will learn about Plotly, an interactive visualization library in Python. We will explore its features and capabilities and create some basic charts using built-in datasets.

What is Plotly?

Plotly is a Python graphing library that allows you to create interactive, publication-quality graphs online. It is built on top of D3.js, WebGL, and Three.js and is capable of producing over 40 different types of charts, including scatter plots, line charts, bar charts, and more.

Plotly's interactive features make it easy to explore data, zoom in on specific areas, toggle data series on and off, and much more. It is widely used in various fields, including data science, finance, engineering, and academia.

Getting Started with Plotly

In this course, we will be using Plotly Express, which is a high-level interface for creating Plotly charts. It offers a simple syntax and a wide range of built-in functions to create different types of charts quickly.

To get started with Plotly Express, we need to import the library:

import plotly.express as px

Creating a Basic Scatter Plot

Let's create a basic scatter plot using the iris dataset that comes built-in with Plotly Express. This dataset contains information about the length and width of the sepals and petals of 150 iris flowers, as well as their species.

Here's how to create a scatter plot of the sepal length and width:

import pandas as pd
import plotly.express as px

# Load the iris dataset
data = px.data.iris()

# Create a scatter plot
fig = px.scatter(data, x="sepal_width", y="sepal_length", color="species", title="Iris Sepal Dimensions")

# Show the plot
fig.show()

In the code above, we first load the iris dataset using px.data.iris(). Then, we create a scatter plot using the px.scatter() function, specifying the x and y axes, the color scale, and the title of the plot. Finally, we display the plot using fig.show().

Creating a Basic Line Chart

Now let's create a basic line chart using the gapminder dataset, which contains information about the GDP per capita, life expectancy, and population of various countries over the years.

Here's how to create a line chart of the GDP per capita and life expectancy for the United States:

import plotly.express as px

# Load the gapminder dataset
data = px.data.gapminder()

# Filter data for the United States
usa_data = data[data['country'] == 'United States']

# Create a line chart
fig = px.line(usa_data, x="year", y="gdpPercap", title="GDP per Capita in the United States")

# Show the plot
fig.show()

In the code above, we first load the gapminder dataset using px.data.gapminder(). Then, we filter the data for the United States using the pandas syntax. Next, we create a line chart using the px.line() function, specifying the x and y axes and the title of the plot. Finally, we display the plot using fig.show().

Creating a Basic Bar Chart

Lastly, let's create a basic bar chart using the tips dataset, which contains information about the total bill and tip amounts for various meals at a restaurant.

Here's how to create a bar chart of the total bill amounts by day of the week:

import plotly.express as px

# Load the tips dataset
data = px.data.tips()

# Create a bar chart
fig = px.bar(data, x="day", y="total_bill", title="Total Bill Amounts by Day of the Week")

# Show the plot
fig.show()

In the code above, we first load the tips dataset using px.data.tips(). Then, we create a bar chart using the px.bar() function, specifying the x and y axes and the title of the plot. Finally, we display the plot using fig.show().

Conclusion

In this lesson, we have introduced Plotly, a powerful interactive visualization library for Python, and learned how to create basic scatter plots, line charts, and bar charts using Plotly Express. In the following lessons, we will dive deeper into the various capabilities and features of Plotly and learn how to create more advanced and customized charts.

Exercises

1. Creating a Scatter Plot with Plotly Express

Instruction

In this exercise, you will create a scatter plot using the iris dataset that comes built-in with Plotly Express. This dataset contains information about the length and width of the sepals and petals of 150 iris flowers, as well as their species.

Follow these steps to create a scatter plot of the sepal length and width:

  1. Import the Plotly Express library as px.
  2. Load the iris dataset using px.data.iris() and store it in a variable called data.
  3. Create a scatter plot using the px.scatter() function, specifying the x and y axes as sepal_width and sepal_length, the color scale as species, and the title of the plot as Iris Sepal Dimensions. Store the result in a variable called fig.
  4. Display the plot using fig.show().

My Solution

# Your solution goes here

Hint

Remember to import the Plotly Express library as px. Use the px.scatter() function to create the scatter plot, and don't forget to specify the x and y axes, the color scale, and the title of the plot.

Solution

import plotly.express as px

data = px.data.iris()

fig = px.scatter(data, x="sepal_width", y="sepal_length", color="species", title="Iris Sepal Dimensions")

fig.show()