Show Sidebar Hide Sidebar

Contour Plots in Python

How to make Contour plots in Python with Plotly.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Basic Contour Plot

In [1]:
import plotly.plotly as py
from plotly.graph_objs import *

data = Data([
    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., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]]
    )
])
py.iplot(data)
Out[1]:

Setting X and Y Coordinates in a Contour Plot

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

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., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]],
        x=[-9, -6, -5 , -3, -1],
        y=[0, 1, 4, 5, 7]
    )]

py.iplot(data)
Out[2]:

Colorscale for Contour Plot

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

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., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]],
        colorscale='Jet',
    )]
py.iplot(data)
Out[3]:

Customizing Size and Range of a Contour Plot's Contours

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go

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., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]],
        colorscale='Jet',
        autocontour=False,
        contours=dict(
            start=0,
            end=8,
            size=2,
        ),
    )
]
py.iplot(data)
Out[4]:

Customizing Spacing Between X and Y Axis Ticks

In [5]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [
    {
        'z': [[10, 10.625, 12.5, 15.625, 20],
              [5.625, 6.25, 8.125, 11.25, 15.625],
              [2.5, 3.125, 5., 8.125, 12.5],
              [0.625, 1.25, 3.125, 6.25, 10.625],
              [0, 0.625, 2.5, 5.625, 10]],
        'colorscale':'Jet',
        'type': u'contour',
        'dx': 10,
        'x0': 5,
        'dy': 10,
        'y0':10,
    }
]
py.iplot(data)
Out[5]:

Connect the Gaps Between None Values in the Z Matrix

In [6]:
import plotly.plotly as py
import plotly.tools as tls

trace0 = {
    'z': [[None, None, None, 12, 13, 14, 15, 16],
          [None, 1, None, 11, None, None, None, 17],
          [None, 2, 6, 7, None, None, None, 18],
          [None, 3, None, 8, None, None, None, 19],
          [5, 4, 10, 9, None, None, None, 20],
          [None, None, None, 27, None, None, None, 21],
          [None, None, None, 26, 25, 24, 23, 22]],
    'type': 'contour',
    'showscale': False
}
trace1 = {
    'z': [[None, None, None, 12, 13, 14, 15, 16],
          [None, 1, None, 11, None, None, None, 17],
          [None, 2, 6, 7, None, None, None, 18],
          [None, 3, None, 8, None, None, None, 19],
          [5, 4, 10, 9, None, None, None, 20],
          [None, None, None, 27, None, None, None, 21],
          [None, None, None, 26, 25, 24, 23, 22]],
    'connectgaps': True,
    'type': 'contour',
    'showscale': False
}
trace2 = {
    'z': [[None, None, None, 12, 13, 14, 15, 16],
          [None, 1, None, 11, None, None, None, 17],
          [None, 2, 6, 7, None, None, None, 18],
          [None, 3, None, 8, None, None, None, 19],
          [5, 4, 10, 9, None, None, None, 20],
          [None, None, None, 27, None, None, None, 21],
          [None, None, None, 26, 25, 24, 23, 22]],
    'type': 'heatmap',
    'zsmooth': 'best',
    'showscale': False
}
trace3 = {
    'z': [[None, None, None, 12, 13, 14, 15, 16],
          [None, 1, None, 11, None, None, None, 17],
          [None, 2, 6, 7, None, None, None, 18],
          [None, 3, None, 8, None, None, None, 19],
          [5, 4, 10, 9, None, None, None, 20],
          [None, None, None, 27, None, None, None, 21],
          [None, None, None, 26, 25, 24, 23, 22]],
    'connectgaps': True,
    'type': 'heatmap',
    'zsmooth': 'best',
    'showscale': False
}

fig = tls.make_subplots(rows=2, cols=2, subplot_titles=('connectgaps = False',
                                                        'connectgaps = True'))

fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 2)
fig.append_trace(trace2, 2, 1)
fig.append_trace(trace3, 2, 2)

fig['layout']['yaxis1'].update(title='Contour map')
fig['layout']['yaxis3'].update(title='Heatmap')

py.iplot(fig)
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]

Out[6]:

Smoothing the Contour lines

In [7]:
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go

trace0 = go.Contour(
    z=[[2, 4, 7, 12, 13, 14, 15, 16],
       [3, 1, 6, 11, 12, 13, 16, 17],
       [4, 2, 7, 7, 11, 14, 17, 18],
       [5, 3, 8, 8, 13, 15, 18, 19],
       [7, 4, 10, 9, 16, 18, 20, 19],
       [9, 10, 5, 27, 23, 21, 21, 21],
       [11, 14, 17, 26, 25, 24, 23, 22]],
    line=dict(smoothing=0),
)
trace1 = go.Contour(
    z=[[2, 4, 7, 12, 13, 14, 15, 16],
       [3, 1, 6, 11, 12, 13, 16, 17],
       [4, 2, 7, 7, 11, 14, 17, 18],
       [5, 3, 8, 8, 13, 15, 18, 19],
       [7, 4, 10, 9, 16, 18, 20, 19],
       [9, 10, 5, 27, 23, 21, 21, 21],
       [11, 14, 17, 26, 25, 24, 23, 22]],
    line=dict(smoothing=0.85),
)

fig = tools.make_subplots(rows=1, cols=2,
                          subplot_titles=('Without Smoothing', 'With Smoothing'))

fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 2)

py.iplot(fig)
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]

Out[7]:

Smooth Contour Coloring

In [8]:
import plotly.plotly as py
import plotly.graph_objs as go

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., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]],
        contours=dict(
            coloring='heatmap'
        )
    )
]
py.iplot(data)
Out[8]:

Contour Lines

In [9]:
import plotly.plotly as py
import plotly.graph_objs as go

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., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]],
        colorscale='Jet',
        contours=dict(
            coloring='lines',
        ),
    )
]
py.iplot(data)
Out[9]:

Custom Contour Plot Colorscale

In [10]:
import plotly.plotly as py
from plotly.graph_objs import *

data = Data([
    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., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]],
        colorscale=[[0, 'rgb(166,206,227)'], [0.25, 'rgb(31,120,180)'], [0.45, 'rgb(178,223,138)'], [0.65, 'rgb(51,160,44)'], [0.85, 'rgb(251,154,153)'], [1, 'rgb(227,26,28)']],
    )
])
py.iplot(data, filename='contour-custom-colorscale')
Out[10]:

Color Bar Title

In [11]:
import plotly.plotly as py
import plotly.graph_objs as go

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., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]],
        colorbar=dict(
            title='Color bar title',
            titleside='right',
            titlefont=dict(
                size=14,
                family='Arial, sans-serif',
            ),
        )
    )
]
py.iplot(data)
Out[11]:

Color Bar Size for Contour Plots

In [12]:
import plotly.plotly as py
import plotly.graph_objs as go

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., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]],
        colorbar=dict(
            thickness=25,
            thicknessmode='pixels',
            len=0.9,
            lenmode='fraction',
            outlinewidth=0
        )
    )
]
py.iplot(data, filename='contour-custom-colorbar-size')
Out[12]:

Styling Color Bar Ticks for Contour Plots

In [13]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [{
        'type': u'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., 8.125, 12.5],
              [0.625, 1.25, 3.125, 6.25, 10.625],
              [0, 0.625, 2.5, 5.625, 10]],
        'colorbar':{
            'nticks': 10,
            'ticks': 'outside',
            'ticklen': 5,
            'tickwidth': 1,
            'showticklabels': True,
            'tickangle': 0,
            'tickfont': {
                'size': 12
            },
        }
        }]
py.iplot(data)
Out[13]:

Reference

See https://plot.ly/python/reference/#contour for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.