How to Draw a plotly Line Plot in Python (Examples)

 

This article explains how to draw line plots (also called line charts; curve charts) using the plotly library in the Python programming language.

The tutorial is structured as follows:

 

 

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 modules:

from vega_datasets import data
import pandas as pd
import plotly.express as px

We’ll use the stocks dataset for this example, which is included in the vega datasets module. We’ll store the data in df_long and df_wide to provide examples using two different structures of the same data.

df_long = pd.DataFrame(data.stocks())
df_long
 
# index	symbol	date	price
# 0	MSFT	2000-01-01 00:00:00	39.81
# 1	MSFT	2000-02-01 00:00:00	36.35
# 2	MSFT	2000-03-01 00:00:00	43.22
# 3	MSFT	2000-04-01 00:00:00	28.37
 
 
df_wide = df_long.pivot(index = 'date', columns = 'symbol', values = 'price').reset_index()
df_wide
 
# symbol	date	AAPL	AMZN	GOOG	IBM	MSFT
# 0	2000-01-01	25.94	64.56	NaN	100.52	39.81
# 1	2000-02-01	28.66	68.87	NaN	92.11	36.35
# 2	2000-03-01	33.95	67.00	NaN	106.11	43.22
# 3	2000-04-01	31.01	55.19	NaN	99.95	28.37

These DataFrames contain identical information. However, they are structured differently to show examples that work in a large variety of settings.

 

Basic Line Plots

Let’s create a simple line plot to show Amazon’s stock price over time, which is a single column in df_wide:

fig1 = px.line(
    data_frame = df_wide
    ,x = 'date'
    ,y = 'AMZN'
)
 
fig1.show()

To add the other variables from the wide data set, we just need to provide a list of the columns to plot on the y axis:

fig2 = px.line(
    data_frame = df_wide
    ,x = 'date'
    ,y = ['AMZN', 'MSFT']
)
 
fig2.show()

This method is quick and easy, but wide data structures are not always as convenient when you have many variables to plot, or when they may change throughout the course of a script. Let’s show how easy it is to plot all the companies in our long dataset, and how we can add a marker to each measurement:

fig3 = px.line(
    data_frame = df_long
    ,x = 'date'
    ,y = 'price'
    ,color = 'symbol'
    ,markers = True
)
 
fig3.show()

 

Customizing Line Plots

These examples demonstrate how to alter the colors, dashes, and curves of your line plots.

 

Custom Colors

This example shows how you can specify the exact colors to use for each line. You can use the generic CSS color text, hex codes, or rgb codes. When you have already mapped the color of each line to one of your variables, you can pass a dictionary to the color_discrete_map argument specifying each group and the desired color.

fig4 = px.line(
    data_frame = df_long
    ,x = 'date'
    ,y = 'price'
    ,color = 'symbol'
    ,color_discrete_map={"MSFT":"gray", "AMZN":"gray", "IBM":"gray", "GOOG":"#faad07", "AAPL":"rgb(187,23,209)"}
)
 
fig4.show()

Tip: Using gray for most colors except those you wish to highlight can be an effective technique to draw your viewers attention to the most important elements of a graph.

 

Custom Dashes

Similar to our color mapping, we can also specify the line style for each group:

fig5 = px.line(
    data_frame = df_long
    ,x = 'date'
    ,y = 'price'
    ,color = 'symbol'
    ,line_dash = 'symbol'
    ,line_dash_map={"MSFT":"solid", "AMZN":"dot", "IBM":"dash", "GOOG":"longdash", "AAPL":"dashdot"}
)
 
fig5.show()

 

Line Shape

Finally, you can change specify whether lines are shaped as linear or spline. This is most noticeable on graphs with fewer points, so this example shows a limited range of data to highlight the differences:

fig6 = px.line(
    data_frame = df_wide[df_wide['date']> '2009-10-01'],
    x = 'date'
    ,y = 'AMZN'
    ,markers = True
    ,line_shape='linear'
)
 
fig6.show()
fig6.update_traces(patch = {"line_shape":"spline"})
fig6.show()


 

Further Resources

 
You can check out these other articles for more detailed examples and videos of these popular charts in plotly using the Python programming language:

 

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.


5 Comments. Leave new

  • Can the color = ‘symbol’ be modified to get only top two values in Legen? In this case Google and Amazon need to be shown not others. The requirement is show the top two from legends.

    Reply
  • Hi Sushanta,

    Thanks for your question. One solution is to create an array of the symbols you want displayed in the legend, and then in a for loop, check if the symbols in the data exists in the created array. If not, then “showlegend” is set to False for those symbols that do not exist within the array. Please, take a look at the code below:

    fig = px.line(
    data_frame = df_long
    ,x = ‘date’
    ,y = ‘price’
    ,color = ‘symbol’
    ,markers = True
    )

    legend_symbols_to_show = [“GOOG”, “AAPL”]

    for trace in fig[“data”]:
    if(not trace[“name”] in legend_symbols_to_show):
    trace[“showlegend”] = False

    fig.show()

    I hope this helps.

    Reply
  • Hi Ifeanyi,

    thank you for taking time to reply. What you said it is going to work if I know the values like GOOG and AAPL. It is kind of as if I know the values earlier. But consider one data where you dont know the top 5 values. In that case how would you do this? Any help is appreciated.

    Reply
    • Hi Sushanta,

      I am not sure there is an automatic way to do that in plotly. However, with a bit of data wrangling, you can determine the top 5 or top 2 highest values in your dataset, and then build a custom function that will automatically select and rank these highest values and then visualize them with plotly. I hope this gives you an idea.

      Reply

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