plotly Area Chart in Python (5 Examples)

 

Hi there! In this tutorial, you’ll learn how to make area charts in plotly, using the Python programming language.

First, though, here is a quick overview of this tutorial:

Let’s get started!

 

Install Latest Version of Python plotly Library

If you do not have plotly already installed in your Python programming environment, then in your preferred Python IDE, run the line of code below to install the latest version of plotly; otherwise, you may skip to the next section.

pip install plotly==5.12.0

 

Import plotly Express & Graph Objects

Next, we are going to import plotly express and plotly graph objects.

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

Please note that you can use either of the above to create an area chart in Python. But for the purpose of this tutorial, we would like to demonstrate both methods.
 

Load & Preview Dataset

We will make use of the pre-loaded gapminder dataset of plotly_express. To load the dataset, run the code:

df = px.data.gapminder()

You can take a look at the first 10 rows of the DataFrame as follows.

df.head(10)
#         country  continent	year	lifeExp	  pop	 gdpPercap  iso_alpha iso_num
#0	Afghanistan	Asia	1952	28.801	8425333	 779.445314	AFG	4
#1	Afghanistan	Asia	1957	30.332	9240934	 820.853030	AFG	4
#2	Afghanistan	Asia	1962	31.997	10267083 853.100710	AFG	4
#3	Afghanistan	Asia	1967	34.020	11537966 836.197138	AFG	4
#4	Afghanistan	Asia	1972	36.088	13079460 739.981106	AFG	4
#5	Afghanistan	Asia	1977	38.438	14880372 786.113360	AFG	4
#6	Afghanistan	Asia	1982	39.854	12881816 978.011439	AFG	4
#7	Afghanistan	Asia	1987	40.822	13867957 852.395945	AFG	4
#8	Afghanistan	Asia	1992	41.674	16317921 649.341395	AFG	4
#9	Afghanistan	Asia	1997	41.763	22227415 635.341351	AFG	4

 

Example 1: Filled plotly Area Chart

In this example, we will create a basic filled area chart of GDP per capita over time for each continent, using plotly express.

# import plotly.express as px
df = px.data.gapminder()
fig = px.area(df, x="year", y="gdpPercap", color="continent", line_group="country")
fig.show()

When you hover over the plot, you see the GDP per capita over time for each country in each continent.
 

Example 2: Pattern-Filled plotly Area Chart

In this example, we will make a pattern-filled area chart for the life expectancy over time for each continent.

fig = px.area(df,
              x = "year",
              y = "lifeExp",
              color = "continent",
              pattern_shape = "continent",
              pattern_shape_sequence=["x", ".", "/"])
fig.show()

You may have noticed that in the visualization above, the patterns are not readily obvious. Nevertheless, when you run the code in your Python IDE, you will definitely see the different patterns in your plot.

Also, we can parse an array of shapes to the argument pattern_shape_sequence =, such as ["|", "\\", "-", " "].

 

Example 3: Overlaid plotly Area Chart with Boundary Lines

In this example, we will be using the graph objects method to create a basic overlaid area chart.

# import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[0, 2, 3, 5], fill='tozeroy')) 
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[3, 5, 1, 7], fill='tonexty'))
 
fig.show()

The fill property is an enumeration that may be specified as one of the following enumeration values: ['none', 'tozeroy', 'tozerox', 'tonexty', 'tonextx', 'toself', 'tonext']. You can check the meanings in the plotly.graph_objects.Scatter documentation.
 

Example 4: Overlaid plotly Area Chart without Boundary Lines

In this example, we will remove the boundary lines of the area chart in Example 3. To do it, we need to set the mode property to "none".

fig = go.Figure()
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[0, 2, 3, 5], fill='tozeroy', mode= "none"))
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[3, 5, 1, 7], fill='tonexty', mode= "none"))
 
fig.show()

 

Example 5: Interior-Filled plotly Area Chart

In this example, we will fill the interior area of an overlaid plotly area chart. In this case, it is sufficient to set None to the fill property for the top graphic and 'tonexty' for the bottom graphic.

fig = go.Figure()
fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4], 
    y=[3, 4, 8, 3],
    fill=None,
    mode='lines',
    line_color='red'))
fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4],
    y=[1, 6, 2, 6],
    fill='tonexty', 
    mode='lines', line_color='green'))
 
fig.show()

So, that’s how to make area charts in plotly using Python. I hope you found these 5 examples helpful.

 

Video, Further Resources & Summary

Do you need more explanations on how to make area charts in plotly 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 make area charts in plotly in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to take a look at other interesting plotly in Python tutorials on Statistics Globe, starting with these ones:

This post has shown how to make area charts in plotly 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.

Menu
Top