Plotly绘制2D等高线图无法呈现数据细微差异的解决方案咨询
Plotly绘制2D等高线图无法呈现数据细微差异的解决方案咨询
我需要可视化一组二维数据,数据如下:
[[ 0. , 234. , 568.33333333, 881.66666667], [ 0. , 234.66888889, 569.44555556, 865.03806017], [ 0. , 235.33627407, 570.06910753, 849.9592539 ], [ 0. , 235.9989178 , 570.2632452 , 836.28943309]]
我尝试用Plotly的等高线图来展示,但发现列方向的细微数值差异完全无法在图中体现出来,我的现有代码如下:
import numpy as np from plotly.subplots import make_subplots import plotly.graph_objects as go from pathlib import Path import os import plotly.io as pio Smax = 1000 T = 1 classical_2d_without_boundary = np.array([ [ 0. , 234. , 568.33333333, 881.66666667], [ 0. , 234.66888889, 569.44555556, 865.03806017], [ 0. , 235.33627407, 570.06910753, 849.9592539 ], [ 0. , 235.9989178 , 570.2632452 , 836.28943309] ]) n_rows, n_cols = classical_2d_without_boundary.shape x = np.linspace(0, Smax, n_cols) y = np.linspace(T, 0, n_rows) x, y = np.meshgrid(x,y) fig = make_subplots(rows= 1, cols = 1) quantum_contour = go.Contour( z=classical_2d_without_boundary, x=x[0, :], # assuming x is 2D after meshgrid y=y[:, 0], # assuming y is 2D after meshgrid colorscale='Inferno' ) fig.add_trace(quantum_contour, row = 1, col = 1) fig.update_layout( title_x=0.5, title_font = dict(size=40), xaxis_title="x-axis", yaxis_title="time (Year)", height=900, width=855, # Earlier it was 750 xaxis_title_font = dict(size=36), yaxis_title_font = dict(size= 36), legend_title_font= dict(size=36), font= dict(size=30), paper_bgcolor ='white', margin=dict(l=20, r=20, t=350, b=20) ) fig.update_xaxes(title_font=dict(size=24)) fig.update_yaxes(title_font=dict(size=24)) for annotation in fig['layout']['annotations']: annotation['font'].update(size= 40) annotation.update(y=1.02) # Increase the y value to move the title up # display fig.show()
针对你遇到的“细微数值差异无法呈现”的问题,可以从以下几个方向调整:
1. 强制调整等高线的层级精度
默认的等高线层级会自动合并小的数值变化,你可以手动设置contours参数,生成更细密的层级,同时显示等高线线条来强化差异:
quantum_contour = go.Contour( z=classical_2d_without_boundary, x=x[0, :], y=y[:, 0], colorscale='Inferno', # 自定义等高线层级,聚焦数据实际范围 contours=dict( start=np.min(classical_2d_without_boundary[:,1:]), # 跳过全0的第一列 end=np.max(classical_2d_without_boundary), size=0.3, # 层级间隔,值越小越能捕捉细微差异 showlines=True, # 显示等高线线条 line=dict(width=0.5) ), # 优化颜色条刻度,方便对应数值 colorbar=dict( tickmode='linear', dtick=1, title=dict(text='数值', font=dict(size=20)) ) )
2. 改用热力图(Heatmap)展示细节
热力图对局部数值变化的敏感度远高于等高线图,更适合展示小规模数据的细微差异:
# 替换Contour为Heatmap quantum_heatmap = go.Heatmap( z=classical_2d_without_boundary, x=x[0, :], y=y[:, 0], colorscale='Inferno', colorbar=dict( tickmode='linear', dtick=1, title=dict(text='数值', font=dict(size=20)) ), # 开启 hover 提示,方便查看精确数值 hoverongaps = False ) fig.add_trace(quantum_heatmap, row=1, col=1)
3. 消除无效数据的干扰
你的数据第一列全为0,这个大跨度的低值会压缩有效数据的颜色刻度范围,可以单独对有效列做可视化,或者对数据做列归一化:
# 方案1:只可视化非0的列 valid_data = classical_2d_without_boundary[:,1:] x_valid = x[0, 1:] quantum_contour = go.Contour( z=valid_data, x=x_valid, y=y[:, 0], colorscale='Inferno', contours=dict(size=0.3) ) # 方案2:对每一列做归一化,聚焦列内相对变化 normalized_data = (classical_2d_without_boundary - classical_2d_without_boundary.min(axis=0)) / (classical_2d_without_boundary.max(axis=0) - classical_2d_without_boundary.min(axis=0))
4. 插值补充数据点
如果数据点太少,细节会被掩盖,可以用插值生成更密集的数据,放大变化趋势:
from scipy.interpolate import interp2d # 生成插值后的密集数据 f = interp2d(x[0,:], y[:,0], classical_2d_without_boundary, kind='cubic') x_new = np.linspace(0, Smax, 100) # 从4个点扩展到100个点 y_new = np.linspace(T, 0, 40) z_new = f(x_new, y_new) # 用插值后的数据绘图 quantum_contour = go.Contour( z=z_new, x=x_new, y=y_new, colorscale='Inferno', contours=dict(size=0.2) )
内容来源于stack exchange




