Custom contour levels for a contour plot

Hi all,

Is there a definitive way to plot custom contour levels yet? I’m attempting to plot some precipitation data and therefore need irregular intervals. If not, how would I go about this using traces?

Thanks

Hi @Nivhawk,

Custom contour levels aren’t supported right now. See Choose levels for the coutours in the contour plot.

If you’re ok with line contours (as opposed to solid filled contours), It wouldn’t be too complicated to achieve this using scatter traces with mode='lines', where the line coordinates are computed by a series of calls to the scikit-image measure.find_contours function, one call per contour level that you want.

Hope that helps,

-Jon

1 Like

Hi dear @jmmease

You comment is for 2019. I’m curious to know that still Custom contour level is not available, or it is? if it is available would you share a link to see, how does it work?

With all the best,

Hi @Bijan,
Please do not ask direct questions, especially by reactivating a very old thread, because it is possible that some users will not enter the forum anymore, and your question remains unanswered.
Meanwhile the go.Contour has been updated, and now you can plot custom contour levels, using the contours_type="constraint".
Print or display in your jupyter or VS Code notebook: help(go.contour.Contours to see the new attributes associated to constraints.

Here is a classic example without a “vivid” coloscale, to distinguish better the contour lines:

import plotly.graph_objects as go
import numpy as np
step=0.05
y, x = np.mgrid[-5 : 5 : step, -5 : 10 : step]
z = np.sin(x)**10 + np.cos(10 + y*x) + np.cos(x) + 0.2*y + 0.1*x

fig = go.Figure()
fig.add_contour(z=z,
        colorscale=[[0, 'rgb(255,255,255)'], [1, 'rgb(255,255,255)']],
                 showscale=False, 
        contours=dict(
            type="levels", 
            start=z.min(),
            end=z.max(),
            size=2
        ))
fig.update_layout(width=500, height=500, template="none", 
                  xaxis_showline=True, yaxis_showline=True, 
                  xaxis_mirror=True, yaxis_mirror=True )

and two examples with constraints:

fig = go.Figure()
fig.add_contour(z=z,
        fillcolor="rgba(0,0,0,0)", #change this color with a vivid one to see the effect
        contours=dict(type="constraint",
                      operation= "[]",
                      value= [-2, 0],
                      showlabels= True, coloring="none" )
                     )
fig.add_contour(z=z,
                fillcolor="rgba(0,0,0,0)",
                contours=dict(type="constraint",
                operation= ">",
                value= 2,
                showlabels= True)) 
fig.update_layout(width=500, height=500, 
                  xaxis_showline=True, yaxis_showline=True, 
                  xaxis_mirror=True, yaxis_mirror=True )

fig2-two-restrictions

fig = go.Figure()
fig.add_contour(z=z,
        fillcolor="rgba(0,0,0,0)", #change this color with a vivid one to see the effect
        contours=dict(type="constraint",
                      operation= "][",
                      value= [-1.5, 1.5],
                      showlabels= True, coloring="none" )
                     )
fig.update_layout(width=500, height=500, 
                  xaxis_showline=True, yaxis_showline=True, 
                  xaxis_mirror=True, yaxis_mirror=True )

fig3

1 Like

ِDear @empet
Really thanks for the response and sorry that I didn’t make any new post (I think you told me another time before and unfortunately, I forgot it) and I remember to ask directly.

Again, thanks for complete response and if I had a problem about the answer you sent, I will create new post for it.

With all the best, :blossom: :pray: