Create Multiple Graphs as plotly Subplots in Python (3 Examples)

 

Hello everyone! I am delighted to welcome you to another interesting plotly in Python tutorial. In this tutorial, I will show you how to create multiple graphs as subplots in plotly using the Python programming language. At the end of the tutorial, you should be able to easily create your own plotly subplots in Python.

Here is a quick look at what we will do:

If you are ready, I am ready. Let’s go!

 

Install plotly Library

If you do not have plotly already installed in your Python environment, then please run the code below in your Python IDE to install it. Otherwise, you may skip to the next section.

pip install plotly

 

Import plotly Library

With plotly installed, the next thing to do is to load the library. But since we will be building subplots in this tutorial, we are going to load plotly a little differently than we usually do. In your IDE, please run the lines of code below:

from plotly.subplots import make_subplots
import plotly.graph_objects as go

We loaded plotly subplots and plotly graph objects, which are the two modules needed to build multiple plotly subplots.
 

Create DataFrames for Subplots

Now, we are going to create 4 DataFrames that we will use in this tutorial to demonstrate how subplots can easily be built in plotly using Python.

To accomplish that, we will need to install and load the Python pandas library, which is a widely-used library in Python for data manipulation and analysis. Please run the lines of code below to install and load pandas:

pip install pandas
import pandas as pd

With pandas installed and loaded, let us now create 4 simple DataFrames that we will use in this project. The first one is a DataFrame comprising years and prices data. We are going to call it dfA:

dfA = pd.DataFrame({"Price":[250,530,300,850,400,670,750],
                   "Years":[2014,2015,2016,2017,2018,2019,2020]})

You can print it out by running:

print(dfA)
#    Price  Years
# 0    250   2014
# 1    530   2015
# 2    300   2016
# 3    850   2017
# 4    400   2018
# 5    670   2019
# 6    750   2020

The next DataFrame, which we will call dfB, comprises years and sales data.

dfB = pd.DataFrame({
    "Years":[2014,2015,2016,2017,2018,2019,2020],
    "Sales":[430,350,518,405,320,650,700]
})

As we did for dfA, you can also print out dfB if you wish to do so.

Our third DataFrame, which we will store in dfC, comprises years and demand data.

dfC = pd.DataFrame({"Years":[2014,2015,2016,2017,2018,2019,2020],
                    "Demand":[600,350,450,700,550,270,410]})

And our fourth and final DataFrame comprises years and purchases data. We will save it as dfD.

dfD = pd.DataFrame({"Years":[2014,2015,2016,2017,2018,2019,2020],
                    "Purchases":[250,350,450,550,350,650,250]})

 

Example 1: Build Two-Column Subplot

Great! Let us now build subplots in two columns. We will plot the data in dfA and dfB side by side using the lines of code below:

fig = make_subplots(rows = 1,cols = 2)
fig.add_trace(go.Scatter(x=dfA["Years"],y=dfA["Price"]),row = 1,col = 1)
fig.add_trace(go.Scatter(x=dfB["Years"],y=dfB["Sales"]),row = 1,col = 2)
fig.update_layout(height = 600,width = 800,title = "Side by Side Line Plots")
fig.show()

We used the make_subplot() function to specify how many rows and columns we want. Thereafter, we used the add_trace() function to create our line plots, which uses the col and row arguments to indicate the grid where the subplot will locate.

Let us move on to something more advanced than the side-by-side line plots.
 

Example 2: Build Stacked Subplot

To create stacked subplots, we will make use of three DataFrames: dfA, dfB, and dfC. Run the lines of code to create stacked subplots:

fig = make_subplots(rows = 3,cols = 1)
fig.append_trace(go.Scatter(x=dfA["Years"],y=dfA["Price"]),row=1,col=1)
fig.append_trace(go.Scatter(x=dfB["Years"],y=dfB["Sales"]),row=2,col=1)
fig.append_trace(go.Scatter(x=dfC["Years"],y=dfC["Demand"]),row=3,col=1)
fig.update_layout(height = 600,width = 800,title = "Stacked Line Plots")
fig.show()

As you can see, the plots are stacked on top of each other. To achieve that, in the make_subplot() function, we specified that we wanted 3 rows and 1 column. Then we made use of a new function, append_trace(), to create our line plots and indicate on which row we wanted each plot to appear. Very easy!
 

Example 3: Build Multiple Subplot

Let us now build multiple subplots in plotly. We will make use of all four DataFrames we have created for this one. The principle is the same as in the previous examples:

fig = make_subplots(rows = 2,cols = 2,
                    subplot_titles = ("Price Over Time","Sales Over Time","Demand Over Time","Purchases Over Time"))
fig.add_trace(go.Scatter(x=dfA["Years"],y=dfA["Price"]),row = 1,col = 1)
fig.add_trace(go.Scatter(x=dfB["Years"],y=dfB["Sales"]),row = 1,col = 2)
fig.add_trace(go.Scatter(x=dfC["Years"],y=dfC["Demand"]),row = 2,col = 1)
fig.add_trace(go.Scatter(x=dfD["Years"],y=dfD["Purchases"]),row = 2,col = 2)
fig.update_layout(height = 600,width = 800,title = "Multiple Line Plots")
fig.show()

There you have it! We have just created multiple plotly subplots in Python. In the code above, we indicated that we wanted a 2 x 2 grid by specifying row as 2 and col as 2 in the make_subplot() function.

We also introduced a new argument in the make_subplot() function: subplot_titles =. The subplot_titles = argument is used to give each plot a title in the grid. Here, we parsed a Python tuple containing the titles of each plot to the argument.

Then in the add_trace() function, we specified on which row and in which column in the grid we wanted each plot to be.

As you have seen, it is very simple and easy to build multiple graphs as subplots in plotly using Python. You can, therefore, further explore your new-found skills by building multiple subplots of other plotly graph types as well in Python. You can be creative, and impress your users.

So, I want to believe that you have learned something of value by reading this tutorial article. If that is true, then make sure to check out other fascinating plotly in Python tutorials on Statistics Globe, and I will see you soon in the next one!
 

Video, Further Resources & Summary

Do you need more explanations on how to create multiple graphs as plotly subplots in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to create multiple graphs as plotly subplots in Python.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some other tutorials on Statistics Globe:

This post has shown how to create multiple plotly subplots in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

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.

Top