.Scatterternary()
The .Scatterternary()
function in Plotly’s graph_objects
module is used to create ternary scatter plots, which are particularly useful for visualizing compositional data. Ternary plots are commonly used in geology, chemistry, economics, and machine learning to represent datasets where three variables are interdependent and sum to a constant (often 1 or 100%).
Unlike Cartesian plots, which use (x, y)
coordinates, ternary plots use a triangular coordinate system, where each data point is represented by three values (a, b, c)
. These values correspond to proportions or fractions of a whole, and their sum remains fixed. The .Scatterternary()
function allows users to plot individual points, lines, or marker-based representations within a ternary diagram.
Key Features
- Visualizes compositional data where three variables sum to a constant.
- Supports custom styling with
marker
properties. - Works within Plotly’s interactive framework, enabling zooming and hovering.
Syntax
import plotly.graph_objects as go
go.Scatterternary(a=None, b=None, c=None, mode=None, marker=None)
a
,b
,c
(lists/arrays): Values representing the three components of each point.mode
(str, optional): Defines how points are displayed (e.g.,"markers"
,"lines"
).marker
(dict, optional): A dictionary that customizes the appearance of markers, including properties like size, color, opacity, symbol, and line properties.
Example 1
The following example demonstrates how to plot a simple ternary scatter plot with three points, each represented by three values summing to one.
import plotly.graph_objects as gofig = go.Figure(go.Scatterternary(a=[0.3, 0.1, 0.6],b=[0.3, 0.7, 0.2],c=[0.4, 0.2, 0.2],mode="markers",marker=dict(size=10, color="blue")))fig.update_layout(title="Ternary Scatter Plot")fig.show()
The output will be an interactive ternary scatter plot with three blue markers, each representing a point whose components sum to one.
Example 2
Here’s a complete example that generates a ternary scatter plot in Plotly, showcasing the proportions of three components in different data points.
import plotly.graph_objects as go# Sample data representing three-component proportionsfig = go.Figure()fig.add_trace(go.Scatterternary(a=[0.6, 0.2, 0.4, 0.8, 0.3], # Component Ab=[0.3, 0.5, 0.4, 0.1, 0.6], # Component Bc=[0.1, 0.3, 0.2, 0.1, 0.1], # Component Cmode="markers",marker=dict(size=12, color=['red', 'blue', 'green', 'purple', 'orange']),text=["Point 1", "Point 2", "Point 3", "Point 4", "Point 5"], # Hover labels))fig.update_layout(title="Ternary Scatter Plot Example",ternary=dict(sum=1, # Ensures that A + B + C = 1aaxis=dict(title="Component A"),baxis=dict(title="Component B"),caxis=dict(title="Component C")))fig.show()
The output will be:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Plotly on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours