Python

Plotly.Express.Line_ternary

“The line_ternary() function is used to create a ternary line plot. A ternary line plot refers to a line plot where each row of the DataFrame is represented as a vertex of a polyline mark on a ternary coordinate.”

Let’s explore.

Function Syntax and Parameter

The function’s syntax is as shown:

plotly.express.line_ternary(data_frame=None, a=None, b=None, c=None, color=None, line_dash=None, line_group=None, hover_name=None, hover_data=None, title=None, template=None, width=None, height=None)

 
The following are some important function parameters you will need to know when creating ternary line plots.

    1. data_frame – this specifies the data frame containing the columns used in the plot.
    2. a – sets the values used to position the marks along the a axis in the ternary coordinate.
    3. b – defines the values used to position the marks along the b axis in the ternary coordinate.
    4. c – similar to a and b but used to position the marks along the c axis in the ternary coordinate.
    5. color – specifies the values used to assign a unique color to their marks.
    6. line_dash – specifies the values used to assign dash-pattern to the lines of the plot.
    7. line_group – sets the values used to group the rows of the data frame into lines.
    8. hover_name – sets the values that appear in bold on the hover tooltip.
    9. title – sets the title for the plot.
    10. template – specifies the figure template.
    11. width/height – sets the width and height of the figure in pixels.

Example

The following creates a simple line ternary plot.

import plotly.express as px
df = px.data.iris()
fig = px.line_ternary(df, a='sepal_width', b='sepal_length', c='petal_width')
fig.show()

 
The resulting plot is as shown:


You can also assign a unique color based on the flower specified as shown:

import plotly.express as px
df = px.data.iris()
fig = px.line_ternary(df, a='sepal_width', b='sepal_length', c='petal_width', color='species')
fig.show()

 
Output:


To set a different color scheme and dark background, we can use the discrete_color_sequence parameter and the template parameter, as shown in the example below:

import plotly.express as px
df = px.data.iris()
fig = px.line_ternary(df, a='sepal_width', b='sepal_length', c='petal_width', color='species', color_discrete_sequence=px.colors.sequential.Plasma_r, template='plotly_dark')
fig.show()

 
The resulting figure:

Termination

That’s it for this one. In this post, you learned how to use the Plotly express module to create a ternary line plot using the line_ternary() function.

Happy coding!!

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