3

I would like my grid of contour plots to refer to the same colorbar, but I get four stacked colorbars on top of each other. How can I have just one colorbar with its numerical values refering to data in all of the plots? Or, in other words, how can my plots' colors refer to the same colorbar?

Here is the test code:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

z1 =   [[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]]

z2 =   [[20, 44, 7, 120, 1, 1, 5, 16],
         [3, 10, 6, 110, 12, 135, 162, 17],
         [4, 2, 77, 77, 11, 14, 172, 18],
         [54, 34, 8, 8, 13, 1, 1, 19],
         [7, 4, 10, 96, 16, 18, 20, 19],
         [9, 10, 55, 27, 2, 21, 2, 2],
         [11, 1, 17, 26, 2, 24, 23, 22]]

z3 =   [[205, 44, 7, 120, 1, 1, 5, 16],
         [3, 10, 6, 110, 12, 135, 162, 17],
         [4, 2, 77, 77, 11, 144, 172, 18],
         [54, 34, 8, 8, 13, 1, 1, 19],
         [7, 42, 10, 96, 16, 18, 20, 19],
         [9, 10, 55, 27, 2, 25, 2, 2],
         [11, 13, 17, 26, 2, 24, 23, 22]]

z4 =   [[203, 44, 7, 120, 1, 1, 5, 16],
         [3, 10, 6, 110, 126, 135, 162, 17],
         [4, 2, 77, 7, 11, 144, 172, 18],
         [54, 34, 8, 8, 13, 1, 1, 19],
         [7, 42, 10, 96, 16, 18, 20, 19],
         [9, 10, 55, 27, 2, 253, 2, 2],
         [11, 1, 17, 26, 2, 24, 23, 22]]

figc1=make_subplots(rows=2, cols=2, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.05, horizontal_spacing=0.05)
figc1.add_trace(go.Contour(z=z1, coloraxis='coloraxis'), row=1, col=1)
figc1.add_trace(go.Contour(z=z2, coloraxis='coloraxis'), row=1, col=2)
figc1.add_trace(go.Contour(z=z3, coloraxis='coloraxis'), row=2, col=1)
figc1.add_trace(go.Contour(z=z4, coloraxis='coloraxis'), row=2, col=2)
figc1.update_layout(coloraxis=dict(colorscale='Viridis'), showlegend=False)
figc1.show()

 

If you run this code, you will see that the colorbar shows as its maximum the maximum value of z1, and it should show maximum of z1,z2,z3 and z4.

enter image description here

vestland
  • 55,229
  • 37
  • 187
  • 305
elizevin
  • 101
  • 1
  • 9
  • 1
    Please provide a complete code snippet with a [sample of your data](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254) – vestland Jan 26 '21 at 11:45
  • 2
    Thank you for improving my question! – elizevin Jan 26 '21 at 12:43
  • Here are the same questions as you and the [answers](https://stackoverflow.com/questions/44376974/plotly-contour-subplots-each-having-their-own-colorbar). `go.Contour(z=z1, colorbar=dict(len=0.5, x=0.45, y=0.75)` – r-beginners Jan 26 '21 at 13:08
  • I saw that question before I asked mine, it's not the same. I don't want more then one colorbar, I just want all subplots to refer to that one colorbar. – elizevin Jan 26 '21 at 13:13
  • So that was the intent of your question. So why don't you remove this one,`coloraxis='coloraxis'` where the four colorbar ticks overlap.. – r-beginners Jan 26 '21 at 13:39
  • If I do that colorscale reverts to default, and ticks of the colorbars of my four contour plots overlap. Also that does nothing for fixing color in my plots, or for making just one colorbar - I still get four, and they still ovelap (now visibly ). – elizevin Jan 26 '21 at 14:09

1 Answers1

4

You have to configure a separate countour setting with a single min+max range and reuse it in all Contours.

maxval = max(max(sum(z1, [])), max(sum(z2, [])), max(sum(z3, [])), max(sum(z4, [])))

contours=dict(
    start=0,
    end=maxval,
)

figc1=make_subplots(rows=2, cols=2, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.05, horizontal_spacing=0.05)
figc1.add_trace(go.Contour(z=z1, contours=contours, coloraxis='coloraxis'), row=1, col=1)
figc1.add_trace(go.Contour(z=z2, contours=contours, coloraxis='coloraxis'), row=1, col=2)
figc1.add_trace(go.Contour(z=z3, contours=contours, coloraxis='coloraxis'), row=2, col=1)
figc1.add_trace(go.Contour(z=z4, contours=contours, coloraxis='coloraxis'), row=2, col=2)
figc1.update_layout(coloraxis=dict(colorscale='Viridis'), showlegend=False)
figc1.update_coloraxes(colorscale='Viridis')

figc1.show() 

But notice, that the individual contour plots will look a bit differently, because all of them will be re-scaled to the max scale.

Here's an example of the code in Deepnote.

Jakub Žitný
  • 962
  • 1
  • 9
  • 38