Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly contour subplots each having their own colorbar

I am trying to generate contours using Plotly. There are 5 contours which I am plotting in a 5x1 grid using subplots. I am unable to see an option by which I can have a separate colorbar for each contour subplot. For line plots I can use shared_xaxes or shared_yaxes options, but unable to find documentation where I can have a separate colorbar for each subplot in this 5x1 grid.

Here are some reference links I tried to go through:

https://plot.ly/python/contour-plots/

https://plot.ly/python/heatmaps-contours-and-2dhistograms-tutorial/

https://plot.ly/python/reference/

like image 580
rtmi Avatar asked Dec 14 '22 22:12

rtmi


1 Answers

Each subplot does in fact have its own colorbar. The issue is that by default they are plotted on top of each other so it's hard to tell there are several of them. By setting the len and y parameters of the colorbar, you can space them out and see how they are associated with each contour. Here's an example, loosely based on the Plotly contour docs:

import plotly
import plotly.graph_objs as go
plotly.offline.init_notebook_mode()

fig = plotly.tools.make_subplots(rows=3, cols=1)
cbarlocs = [.85, .5, .15]
zmax = [2, 5, 10]

for n in range(3):
    trace = 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(len=0.25, y=cbarlocs[n]), 
            zmin=0, zmax=zmax[n])

    fig.append_trace(trace, n+1, 1)

plotly.offline.iplot(fig)

The crucial part is colorbar=dict(len=0.25, y=cbarlocs[n]). This code results in something that looks like this:

enter image description here

like image 88
Dan Avatar answered Dec 16 '22 12:12

Dan