×

Line Plot with Plotly

A line chart or line plot or line graph or curve chart is a type of chart that displays information as a series of data points called ‘markers’ connected by straight line segments.

Line graphs are common and effective charts because they are simple, easy to understand, and efficient. Line charts are great for:

  • Comparing lots of data all at once
  • Showing changes and trends over time
  • Including important context and annotation
  • Displaying forecast data and uncertainty
  • Highlighting anomalies within and across data series

Markers are the circles in the plots.

Line Chart

Submodules in Plotly to create Plots

Plotly supports two submodules for creating various plots such as line plot, scatter plot etc., that are following:

  • Plotly Graph objects
import plotly.graph_objects as go
  • Plotly Express
import plotly.express as px

We can choose to use any of the modules but plotly.express is widely used, because plotly.express needs less number of lines of code in order to create any plot.

Since we now know What is line plot and What are the different submodules for creating plots, let’s work on how we can get our data set, to create a plot.

Creating Data Set

Either we can read data from any CSV file, or we can choose to create our own data set for visualisation purposes. For any of the ways mentioned above, we can use Pandas, NumPy – a Python Modules.

Let’s create our own demo dataset:

Install Pandas and NumPy

pip install pandas
pip install numpy
  • Using pandas.read_csv() we can read data from any CSV file.
import pandas as pd
import numpy as np

df = pd.DataFrame(dict(
    x_axis = [i for i in range(100)],
    y_axis = np.random.randint(10, 50, 100)
))

Line Plot with Plotly Express

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures. Plotly have express.line() – function to create a line plot.

The line() – function takes two list’s as parameters to plot in X, Y axes OR You can name the data set and put the names of the columns on the X & Y axes. There’s an optional parameter called title, a title for the plot.

import plotly.express as px

import pandas as pd
import numpy as np

df = pd.DataFrame(dict(
    x_axis = [i for i in range(50)],
    y_axis = np.random.randint(15, 20, 50)
))

fig = px.line(        
        df, #Data Frame
        x = "x_axis", #Columns from the data frame
        y = "y_axis",
        title = "Line Plot"
)
  • Plotly has fig.show() function to display the plot.
fig.show()

Output

Line Plot Plotly 1

Line Plots with Colour encoded Columns

We can colour our points with the “color argument” in line() – method and px take care of the details, assign default colours and set up the legend etc.

Here’s an example of a Colour encoded column Plot with the Gapminder dataset – which comes built-in! – showing year vs life expectancy by the continent for Oceania:

Built-In dataset from Plotly

gapminder = px.data.gapminder()
  • We can get the insides of a dataset by the head() – function.
print(gapminder.head())
Output
       country continent  year  ...   gdpPercap  iso_alpha  iso_num
0  Afghanistan      Asia  1952  ...  779.445314        AFG        4
1  Afghanistan      Asia  1957  ...  820.853030        AFG        4
2  Afghanistan      Asia  1962  ...  853.100710        AFG        4
3  Afghanistan      Asia  1967  ...  836.197138        AFG        4
4  Afghanistan      Asia  1972  ...  739.981106        AFG        4

[5 rows x 8 columns]

Plot

#Gapminder DataSet
gapminder = px.data.gapminder()
gapminder_df = gapminder.query("continent == 'Oceania'")

fig_color_en = px.line(
    gapminder_df,
    x = "year",
    y = "lifeExp",
    #Color Property
    color = "country"
)

fig_color_en.show()
Output
Plotly Column Encoding Color

Line Plots with Marker

Markers are the circles in the plot. Markers are essential to distinguish the data point in the plot. They represent the specific values in the plot.

If you have noticed the plots we have created so far don’t have markers in them. Let’s create a plot with markers.

The markers – argument in line() – method, can be set to True – to show markers on lines. Marker is set to False – by default in px.line().

fig_with_marker = px.line(
    gapminder_df,
    x = "year",
    y = "lifeExp",
    color = "country",
    #Markers Property
    markers = True
)
fig_with_marker.show()
Output
Line Plots With Markers Plotly

Line Plot with Text

We can also add a text value to the markers in the line plot by setting up the text argument in px.line() – method.

fig_with_text = px.line(
    gapminder_df,
    x = "lifeExp",
    y = "gdpPercap",
    color = "country",
    #Text Property
    text = "year"
)

We can also set the position of the text in the plot.

fig_with_text.update_traces(textposition = "right")

The ‘text position‘ property is an enumeration that may be specified as – One of the following enumeration values:

['top left', 'top center', 'top right', 'middle left',
'middle center', 'middle right', 'bottom left',
'bottom center', 'bottom right']
Full implementation
import plotly.express as px
import pandas as pd

#Gapminder DataSet
gapminder = px.data.gapminder()
gapminder_df = gapminder.query("continent == 'Oceania'")

fig_with_text = px.line(
    gapminder_df,
    x = "lifeExp",
    y = "gdpPercap",
    color = "country",
    markers= True,
    #Text Property
    text = "year"
)
fig_with_text.update_traces(textposition = "bottom right")
fig_with_text.show()
Output
Line Plot With Text In Plotly

Line Plot with Plotly Graph Objects

plotly.graph_objects module (typically imported as go) has scatter function. go.Scatter can be used both for plotting points (markers) or lines, depending on the value of the mode provided. Values of mode are as follows:

  • lines: For line plot
  • lines+markers: For line plot with markers in it.
  • markers: Plot with markers only, no lines.

When using Plotly graph objects first we have to create a figure(i.e a canvas) in which we actually add the plot. For creating a figure we have to just call the Figure() – method of the graph_objects – submodule.

import plotly.graph_objects as go

fig = go.Figure()

For adding the plot into the figure(or canvas) created, we have to add_trace() – method of the same submodule.

fig.add_trace(<plot object>)

Similar to plotly.express, plotly.graph_objects also uses fig.show() – method to display the plot.

fig.show()

Lines mode

Implementation
x_axis = np.arange(10)

go_fig = go.Figure()
obj = go.Scatter(
    x = x_axis,
    y = x_axis**2,
    mode = 'lines',
)

go_fig.add_trace(obj)
go_fig.show()

Output

Line Plot Graph Objects

Lines+markers mode

Numpy module can be very handy while creating the demo data sets. Numpy has a variety of functions to create an array of specified dimensions or the values inside it. Linspace and random.randn are the two we are going to use here:

  • np.linspace(): Linspace takes three parameters – start, stop and no. of values. It returns a 1D array containing float values.
  • np.random.randn(): This method can take any number of parameters signifying the dimension of the array to be created – One value for 1D array, Two values for 2D array and so on.
# Data Set
x_axis = np.linspace(0, 1, 100)
y0 = np.random.randn(100) + 5
y1 = np.random.randn(100)
y2 = np.random.randn(100) - 5
Implementation
obj_lm = go.Scatter(
    x = x_axis,
    y = y1,
    mode = 'lines+markers',
    name = 'lines+markers'
)
go_fig.add_trace(obj_lm)

Markers mode

obj_markers = go.Scatter(
    x = x_axis,
    y = y2,
    mode = 'markers',
    name = 'markers'
)
go_fig.add_trace(obj_markers)
Complete implementation
import plotly.graph_objects as go
import numpy as np

x_axis = np.linspace(0, 1, 100)
y0 = np.random.randn(100) + 5
y1 = np.random.randn(100)
y2 = np.random.randn(100) - 5

#Lines mode
go_fig = go.Figure()
obj_lines = go.Scatter(
    x = x_axis,
    y = y0,
    mode = 'lines',
    name = 'lines'
)

#Lines + markers mode
obj_lm = go.Scatter(
    x = x_axis,
    y = y1,
    mode = 'lines+markers',
    name = 'lines+markers'
)

#Markers mode
obj_markers = go.Scatter(
    x = x_axis,
    y = y2,
    mode = 'markers',
    name = 'markers'
)

#Adding traces
go_fig.add_trace(obj_lines)
go_fig.add_trace(obj_lm)
go_fig.add_trace(obj_markers)

#Create the plot
go_fig.show()
Output
Modes Graph Objects Plotly

Style the Line Plot

Let’s see how we can change the colour, change the line style (i.e dash or dot) in the plot, modify the line width and add titles to the plot as well as axes.

Modify the Colour, width and line Style

The Scatter function of the graph_objects submodule takes an optional parameter line – a dictionary data type. Dictionary has several optional key-value pairs to set the Colour, width and style of line in the plot.

line = dict(color = 'firebrick', width = 4, dash = 'dot'

dash options include ‘dash’, ‘dot’, and ‘dashdot’

st_fig = go.Figure()

#Dash
dash_obj = go.Scatter(
    x = x_axis,
    y = y0,
    line = dict(color='firebrick', width=4, dash='dash')
)

#Dot
dot_obj = go.Scatter(
    x = x_axis,
    y = y1,
    line = dict(color='royalblue', width=4, dash='dot')
)

#Dash Dot
dd_obj = go.Scatter(
    x = x_axis,
    y = y2,
    line = dict(color='firebrick', width=4, dash='dashdot')
)

#Add traces
st_fig.add_trace(dash_obj)
st_fig.add_trace(dot_obj)
st_fig.add_trace(dd_obj)

#Create the plot
st_fig.show()
Output
Style Line Plot Go

Title to the Plot and the axes

For adding the title’s we have to use the update_layout() – method to set the respective titles of the plot and the axes.

fig.update_layout(title = "TITLE", xaxis_title = "X axis", yaxis_titile = "Y axis"
Implementation
import plotly.graph_objects as go
import numpy as np

x_axis = np.linspace(0, 1, 100)
y_axis = np.random.randn(100) - 5

obj = go.Scatter(
    x = x_axis,
    y = y_axis,
    line = dict(color='firebrick', width=4)
)

st_fig.add_trace(obj)

st_fig.update_layout(
    title='Line Plot',
    xaxis_title='X axis',
    yaxis_title='Y axis')
    
st_fig.show()
Output
Update Layout Plotly Go