Line chart in plotly

The line function from plotly

You can create line plots in plotly and Python with the line function from plotly express. The function recognizes the data in two ways: passing individual arrays to x and y or passing a pandas data frame as input and specifying the name of the columns to be used.

import plotly.express as px

# Line chart
fig = px.line(x = [1, 2, 3, 4, 5, 6, 7],
              y = [10, 15, 25, 18, 43, 30, 65])


# Alternative: data from pandas data frame
# import pandas as pd
# df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7],
#                    'y': [10, 15, 25, 18, 43, 30, 65]})

# Line plot from data frame
# fig = px.line(df, x = "x", y = "y")

fig.show()

Line plot titles

You can add a title to your line plot with the title argument. You can also customize the axis labels with labels.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7],
                  'y': [10, 15, 25, 18, 43, 30, 65]})

fig = px.line(df, x = 'x', y = 'y',
              title = "Line chart title",
              labels = {'x': 'X-axis', 'y':'Y-axis'})

fig.show()

Markers

By default, plotly line graphs doesn’t show the markers for each pair of data points, but if you want you can set the markers argument to True in order to add them.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7],
                  'y': [10, 15, 25, 18, 43, 30, 65]})

fig = px.line(df, x = 'x', y = 'y', markers = True)

fig.show()

Adding text

You can label each data point with text with the text argument. You just need to input the desired variable. Note that you can customize the position of the text with the textposition argument from the update_traces function.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7],
                  'y': [10, 15, 25, 18, 43, 30, 65]})

fig = px.line(df, x = 'x', y = 'y', text = 'y')
fig.update_traces(textposition = "top center")

fig.show()

Line and markers advanced styling

It is important to note that you can customize the line and markers style with the update_traces method. You will need to pass dictionaries with the desired properties for each trace to that method, as in the example below.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7],
                  'y': [10, 15, 25, 18, 43, 30, 65]})

fig = px.line(df, x = 'x', y = 'y', markers = True)
fig.update_traces(line = dict(dash = "dot", width = 4, color = "red"),
                  marker = dict(color = "darkblue", size = 20, opacity = 0.8))

fig.show()

Line plot by group

The line function provides the color, symbol and line_dash arguments to create line charts based on the groups of a categorical variable.

Color by group

If you pass a categorical variable to color you will create a line plot with as many lines as different groups and each line will have a different color.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013],
                   'y': [10, 15, 25, 18, 43, 30, 6, 12],
                   'group': ['G1', 'G1', 'G1', 'G1', 'G2', 'G2', 'G2', 'G2']})

fig = px.line(df, x = 'x', y = 'y', color = 'group')

fig.show()

You can customize the color for each group with color_discrete_map as in the following example or passing an array of colors to color_discrete_sequence.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013],
                   'y': [10, 15, 25, 18, 43, 30, 6, 12],
                   'group': ['G1', 'G1', 'G1', 'G1', 'G2', 'G2', 'G2', 'G2']})

fig = px.line(df, x = 'x', y = 'y', color = 'group',
              color_discrete_map = {'G1':'green', 'G2':'orange'})

fig.show()

Markers by group

If you pass your categorical variable to symbol each line will have different markers. Note that you can combine this with color, so each line will have different colors and different markers.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013],
                   'y': [10, 15, 25, 18, 43, 30, 6, 12],
                   'group' : ['G1', 'G1', 'G1', 'G1', 'G2', 'G2', 'G2', 'G2']})

fig = px.line(df, x = 'x', y = 'y', color = 'group', symbol = 'group')

fig.show()

The shape of the symbols can be set through the symbol_map argument in order to specify the symbol for each group or passing an array of symbols to symbol_sequence.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013],
                   'y': [10, 15, 25, 18, 43, 30, 6, 12],
                   'group' : ['G1', 'G1', 'G1', 'G1', 'G2', 'G2', 'G2', 'G2']})

fig = px.line(df, x = 'x', y = 'y', color = 'group', symbol = 'group',
              symbol_map = {'G1': 'square', 'G2': 'cross'})

fig.show()

Line style by group

The last option is to use a different line style for each group. For this purpose you will need to pass your categorical variable to line_dash.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013],
                   'y': [10, 15, 25, 18, 43, 30, 6, 12],
                   'group': ['G1', 'G1', 'G1', 'G1', 'G2', 'G2', 'G2', 'G2']})

fig = px.line(df, x = 'x', y = 'y', color = 'group', line_dash = 'group')

fig.show()

You can specify the desired line type for each group with line_dash_map or with line_dash_sequence.

import plotly.express as px
import pandas as pd

# pandas data frame
df = pd.DataFrame({'x': [2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013],
                   'y': [10, 15, 25, 18, 43, 30, 6, 12],
                   'group': ['G1', 'G1', 'G1', 'G1', 'G2', 'G2', 'G2', 'G2']})

fig = px.line(df, x = 'x', y = 'y', color = 'group', line_dash = 'group',
              line_dash_map = {'G1': 'dashdot', 'G2': 'dash'})

fig.show()
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon

See also