1

I have two different plots, one heatmap and a contour plot, and I would like to put the contour on top of the heatmap. The z-value for each of the plots correspond to different quantities.

import plotly.graph_objects as go

fig1=go.Figure(go.Heatmap(
    x=data.X,
    y=data.Y,
    z=data0.Z1, zsmooth='best',
    colorscale='Viridis'
), layout=layout)

fig2=go.Figure(go.Contour(
    x=data.X,
    y=data.Y,
    z=round(data.Z2,3),
    zmin=0,
    zmax=0.3,
    contours_coloring='lines',
    line_width=2,
    colorscale=[[0,'blue'],[0.5,'black'],[1,'red']],
    contours=dict(
        start=0, end=0.01, size=1)
), layout=layout)

In Altair I could just use fig1 + fig2 but that does not work with Plotly. Unfortunately Altair does not have any contour capabilities. If this is not possible with Plotly, could this be achieved with matplotlib?

Edit: here is a data example

ls = [[-0.1,-3.0,-0.5,0.0],[-0.1,-2.0,-0.25,0.2],[-0.1,-1.0,-0.15,0.25],[-0.1,0.0,0,0.3],[-0.1,1.0,0.1,0.25],[-0.1,2.0,0.25,0.2],[-0.1,3.0,0.5,0.0],[0.0,-3.0,-0.5,0.0],[0.0,-2.0,-0.25,0.1],[0.0,-1.0,-0.15,0.11],[0.0,0.0,0.0,0.1],[0.0,1.0,0.1,0.0],[0.0,2.0,0.25,0.0],[0.0,3.0,0.5,0.0],[0.1,-3.0,-0.5,0.0],[0.1,-2.0,-0.5,0.12],[0.1,-1.0,-0.15,0.12],[0.1,0.0,0.0,0.13],[0.1,1.0,0.1,0.12],[0.1,2.0,0.5,0.12],[0.1,3.0,0.5,0.0]]

data = pd.DataFrame(ls, columns=['X', 'Y', 'Z1', 'Z2'])
2
  • what do you mean by contours_coloring='lines', also could you provide a sample of the dataset in order to check if my proposition would work? If you could provide details of the environment you're working on I may also be able provide some plotly advice Commented Feb 29, 2020 at 20:37
  • @vlizana contours_coloring='lines' allows to show colored lines on an empty background, i.e. without the underlying heatmap. You make look at the documentation to see an example: contour lines . I will edit my post later today to provide you with some of the data. I didn't put any because I didn't think it was relevant in this case. Commented Mar 2, 2020 at 13:31

2 Answers 2

2

Given your example, to put the contour on top the heat map you need to do:

fig1.add_trace(fig2.data[0])
Sign up to request clarification or add additional context in comments.

Comments

1

I believe that the desired result could be achieved using a single go.Countour plot. Simply set the key word argument contours_coloring="heatmap" for smooth colours behind the contour lines.

See https://plotly.com/python/contour-plots/#smooth-contour-coloring.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.