plotly

Plotly.expess.line

One of the most common plots in statistical, scientific, or financial plotting is a line plot. A line plot is easy to create and interpret as it shows data points as a series. It then connects the points using a straight segment allowing you to view the change of values over a given parameter. Therefore, it’s no surprise its widely adopted in many plotting needs.

In this tutorial, we will dive into our plotting knowledge and discuss how we can create a line plot using Plotly Express module.

Plotly.express.line()

To create a line plot using the Plotly express module, we will use the line function. The function takes a syntax as shown below:

plotly.express.line(data_frame=None, x=None, y=None, line_group=None, color=None, line_dash=None, symbol=None, hover_name=None, hover_data=None, custom_data=None, text=None, facet_row=None, facet_col=None, facet_col_wrap=0, facet_row_spacing=None, facet_col_spacing=None, error_x=None, error_x_minus=None, error_y=None, error_y_minus=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, orientation=None, color_discrete_sequence=None, color_discrete_map=None, line_dash_sequence=None, line_dash_map=None, symbol_sequence=None, symbol_map=None, markers=False, log_x=False, log_y=False, range_x=None, range_y=None, line_shape=None, render_mode='auto', title=None, template=None, width=None, height=None)

Despite the large parameter list, the function is relatively simple and rarely will you need to use all the parameters, if any.

Let us explore the most useful and common parameter list you will need to know.

  1. Data_frame –specifies the column names used in the plot. You can pass these values as a Pandas DataFrame, an array_like object, or a Python dictionary.
  2. x – specifies the values used to position the marks along the x axis. You can specify this parameter as a column name within the specified data frame, a Pandas series, or an array_like object.
  3. Y – similar to x but the values are used for the y axis.
  4. Color – specifies the values used to assign the color to marks.
  5. Line_group – allows you to group rows of data_frames into lines.
  6. Line_shape – specifies the shape of the lines. Accepted values include ‘linear’ or ‘spline’.
  7. Title – specifies the title for the plot.
  8. Mode – specifies the function will return the Line plot as graph_objects.Figure type.

Line Plot with Plotly.Express Module

Let us now learn how we can create a line plot with plotly express. Take the code shown below:

import plotly.express as px

df = px.data.stocks()

fig = px.line(df, x='date', y='AMZN')

fig.show()

In the example above, we start by importing the plotly express module as px. We then create a DataFrame from the pandas stocks data.

Finally, we create the line plot for the ‘AMZN’ column from the data Frame. The code above should return a time-series chart of the stocks in the data frame.

An example figure is as shown:

Simple Line Plot

We can also create simple line plots without using custom data. For example, we can use a simple NumPy range as shown in the code below.

import plotly.express as px

import numpy as np

x = np.arange(50)

y = np.arange(25, 75)

fig = px.line(x=x, y=y)

fig.show()

The code above should return a simple line plot as shown:

Specifying Color

If you have multiple line plots, you can distinguish them by giving a color using the color parameter.

Take the example code below:

import plotly.express as px

df = px.data.gapminder().query("continent=='Europe'")

fig = px.line(df, x='year', y='lifeExp', color='country')

fig.show()

In this example, we are using the gapminder data. We then create a line plot for each country in the Europe continent. Using the color parameter, we specify the color as country column. This will assign a unique color for each color in the plot.

The resulting figure is as shown:

Congratulations, you have successfully learned how to create and use line plots using Plotly Express.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list