Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: using ONE colorbar for multiple CONTOUR plots

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

like image 397
elizevin Avatar asked Nov 01 '25 16:11

elizevin


1 Answers

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.

like image 167
Jakub Žitný Avatar answered Nov 03 '25 06:11

Jakub Žitný



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!