.Contour()
Published Mar 16, 2025
Contribute to Docs
In Plotly, the .Contour()
function creates a contour plot, which represents 3D surface data in a 2D projection using contour lines or filled color regions. This function is useful for visualizing gradual variations in a dataset over a 2D plane, such as temperature distributions, elevation maps, and probability density functions.
Syntax
plotly.graph_objects.Contour(z=None, x=None, y=None, colorscale=None, contours=None, ...)
z
: A 2D array (list or NumPy array) representing the values to be contoured.x
: A 1D or 2D array defining the x-coordinates corresponding toz
values.y
: A 1D or 2D array defining the y-coordinates corresponding toz
values.colorscale
: Defines the color scheme of the contour plot (e.g.,"Viridis"
,"Jet"
, etc.).contours
: Controls the contour levels with a dictionary containing:start
: The starting value of the contours.end
: The ending value of the contours.size
: The step size between contour levels.
Note: The ellipsis (…) in the syntax indicates that there can be additional optional parameters beyond those listed here.
Example
The following example demonstrates the usage of the .Contour()
function:
import plotly.graph_objects as go# Define x and y coordinatesx = [-2, -1, 0, 1, 2]y = [-2, -1, 0, 1, 2]# Define a 2D list for z valuesz = [[0.1, 0.2, 0.3, 0.2, 0.1],[0.2, 0.4, 0.6, 0.4, 0.2],[0.3, 0.6, 1.0, 0.6, 0.3],[0.2, 0.4, 0.6, 0.4, 0.2],[0.1, 0.2, 0.3, 0.2, 0.1]]# Create a contour plotfig = go.Figure(data=[go.Contour(z=z, # Data values for contour levelsx=x, # X-coordinatesy=y, # Y-coordinatescolorscale="Viridis", # Color schemecontours=dict(start=0, end=1, size=0.1))]) # Define contour levels# Display the plotfig.show()
The above code generates the following output:
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
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 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