python - Plotly:对多个 CONTOUR 图使用一个颜色条
问题描述
我希望我的等高线图网格引用相同的颜色条,但我得到了四个堆叠的颜色条。我怎样才能只有一个颜色条,其数值引用所有图中的数据?或者,换句话说,我的绘图颜色如何引用相同的颜色条?
这是测试代码:
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中的代码示例。
推荐阅读
- node.js - 有没有办法为整个 npm 项目使用全局变量?
- c# - WPF/XAML:将元素添加到列表类型的依赖项属性
- bash - 理解 bash 代码中的输出的问题
- spring - SpringBootTest 因“IllegalStateException - 未设置 ServletContext”而失败
- jquery - 控制元素出现在 jquery .wrap 中的位置
- api - 设置 Curl 命令以在 Zapier Webhook 中运行
- javascript - 这个集合的数据类型是什么以及如何访问它的值?
- informix - 有没有办法列出informix中的所有模式(包括那些没有任何表和任何特权的模式)?
- ruby-on-rails - 将列名作为变量传递给 ActiveRecord 查询
- excel - 嵌套 For 循环替代方案或优化