Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

python - 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/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...