Introduction to the plotly Library in Python (Example & Tutorial)

 

Plotly is a free graphing library. It renders the visuals with JavaScript but has convenient wrappers to generate the images using R, Python, or several other languages. This makes it easy to use plotly in a variety of contexts, and the visualizations have some great features for interactivity!

In particular, plotly graphs provide interactive elements that enable users to modify, explore, and experience the visualized data in different ways.

This page shows an example of a plotly graph, demonstrates the main features of plotly, and outlines the situations when plotly may be the preferable solution.

You can scroll through their gallery to see a few examples.

 

 

Kirby White Researcher Statistician Programmer

Note: This article was created in collaboration with Kirby White. Kirby is a Statistics Globe author, innovation consultant, data science instructor. His Ph.D. is in Industrial-Organizational Psychology. You can read more about Kirby here!

 

Modules and Example Data

If you have not already done so, install and load these packages:

from vega_datasets import data
import plotly.express as px

We’ll use the iris and stocks dataset for this example, which are included in the vega dataset.

df = data.iris()
df

Scatter Plot

Let’s build and display a basic scatterplot that we’ll store in an object called fig:

fig = px.scatter(
    data_frame = df,
    x = 'sepalWidth',
    y = 'sepalLength',
    color = 'species'
)
 
fig.show()

 

Line Plot

It’s very simple to create a time series plot of stock prices, like this:

df2 = data.stocks()
 
fig2 = px.line(
    data_frame = df2,
    x = 'date',
    y = 'price',
    color = 'symbol'
)
 
fig2.show()

 

Box Plot

With another simple change, you can create a box plot to view the distribution of a dataset.

fig3 = px.box(
    data_frame = df2,
    x = 'symbol',
    y = 'price'
)
 
fig3.show()

As you interact with your graph, you may have noticed that detailed information pops up over each point as you hover your cursor over it.

 

Plotly Features

The plotly library provides a huge set of features. I’ve already shown you some of them. However, there are even more ways to visualize your data interactively!

Some of the most common features are:

  • The tooltip “hover” info
  • You can zoom in and out of plotly graphs
  • It’s simple to export graphs as an image

Some of the more complex features include:

  • The integration of multiple graphs together
  • Hover info for templates
  • Animated interactive graphs
  • The integration of plotly graphics with enterprise coding environments

And there are many more!

 

Further Resources

 
You can check out these other articles to get started with plotly and for more detailed examples and videos:

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top