Plotly:对多个 CONTOUR 图使用一个颜色条
Plotly: using ONE colorbar for multiple CONTOUR plots
我希望我的等高线图网格指的是同一个颜色条,但我得到了四个堆叠在一起的颜色条。我怎样才能只有一个颜色条,其数值引用所有图中的数据?或者,换句话说,我的绘图的颜色如何引用相同的颜色条?
这里是测试代码:
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()
如果你 运行 这段代码,你会看到颜色条显示为 z1 的最大值,它应该显示 z1、z2、z3 和 z4 的最大值。
您必须使用单个最小+最大范围配置单独的轮廓设置,并在所有轮廓中重复使用它。
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()
但请注意,各个等高线图看起来会有些不同,因为所有等高线图都将重新缩放到最大比例。
这是 Deepnote 中的 example of the 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()
如果你 运行 这段代码,你会看到颜色条显示为 z1 的最大值,它应该显示 z1、z2、z3 和 z4 的最大值。
您必须使用单个最小+最大范围配置单独的轮廓设置,并在所有轮廓中重复使用它。
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()
但请注意,各个等高线图看起来会有些不同,因为所有等高线图都将重新缩放到最大比例。
这是 Deepnote 中的 example of the code。