plotly Contour Plot in Python (4 Examples)
Hi! This tutorial will show you how to build a plotly contour plot in the Python programming language.
A contour plot is a graphical representation used to visualize the variations in a two-dimensional dataset.
This type of plot is particularly useful for displaying the patterns and relationships between two continuous variables using level curves.
Here is an overview of this tutorial:
Let’s jump into the Python code!
Install & Import plotly
We will need to first install and import the plotly Python library into our Python programming environment.
If you already have plotly installed and imported, you may skip to the next section in this tutorial.
But if you do not, then in your preferred Python coding IDE, run the lines of code below to install and import plotly:
# install plotly pip install plotly # import plotly import plotly.graph_objects as go
Here, we imported plotly’s graph object because that is what we will make use of to build the contour plot.
Create Demo Dataset
Here, we will create the demo dataset that we will use in the examples in this tutorial. Therefore, run the code below to create the demo dataset:
z = [[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5.2, 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]]
The demo dataset is a nested list or list of lists.
It is important to point out that in order to use this dataset in our examples, we will need to pass it directly inside the plotly contour method, otherwise, it may throw an error.
Example 1: Build A Basic Plot
In this first example, we will build a basic plotly contour plot. Run the code below to do so:
fig = go.Figure(data = go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5.2, 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]])) fig.show()
In the above example, we passed our data directly to the go.Contour() method, which created the graph object.
The go.Contour()
method is itself also passed as an argument to the go.Figure() method, which then generates the contour plot visualization that is displayed with fig.show()
Example 2: Adjust Spacing Between X & Y Axis Ticks of Plot
In this next example, we will adjust the spacing between the X and Y axis ticks of the contour plot:
fig = go.Figure(data = go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5.2, 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], dx=10, x0=5, dy=10, y0=10, )) fig.show()
Here, we defined new arguments dx
, dy
, x0
, and y0
in the go.Contour()
method.
The dx
and dy
parameters set the spacing between consecutive grid points along the X and Y axes, respectively while the x0
and y0
parameters determine the starting values of the X and Y axes.
Increasing the values of dx
and dy
would increase the distance between neighboring data points along the X and Y axes, leading to larger gaps between ticks on the respective axes.
Conversely, decreasing these values would result in smaller gaps between ticks and more closely spaced contour lines.
Example 3: Add Color Scale to Plot
In this third example, we will add a color scale to the contour plot:
fig = go.Figure(data = go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5.2, 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], colorscale="Spectral" )) fig.show()
In this example, we added a color scale to our original contour plot by simply defining the colorscale =
argument in the go.Contour()
method.
There are other color scales you can play with, and you can find examples here
Example 4: Change Size & Range of Plot Contours
In this final example, we will change the size and range of the contours in the contour plot:
fig = go.Figure(data = go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5.2, 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], colorscale='Spectral', contours=dict( start=0, end=6, size=2 ) )) fig.show()
As you can see, we have changed the size and range of the contour plot. All we needed to do was to format the contours by passing a dictionary containing the range of the contours, denoted by the start
and finish
arguments, and the size
argument.
You can play around with these parameters to modify your own plot.
Video, Further Resources & Summary
Do you need more explanations on how to build a plotly contour plot in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain how to build a plotly contour plot in Python.
The YouTube video will be added soon.
You can check out these other articles for more detailed examples and videos of these popular charts in plotly using the Python programming language:
- plotly Pie & Donut Chart in Python (4 Examples)
- plotly Map in Python (3 Examples)
- plotly Sunburst Chart in Python (4 Examples)
- plotly Area Chart in Python (5 Examples)
- plotly Heatmap in Python (3 Examples)
- Introduction to Python Programming
This post has shown how to build a plotly contour plot in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter