You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用pyqtgraph显示三维距离矩阵?

Hey there! Let's tackle this 3D distance plot challenge with pyqtgraph step by step. Since you're new to the library, I'll break this down clearly so you can get the Matlab-like Euclidean Distance Plot 3D effect you're looking for.

Core Idea

Matlab's 3D Euclidean Distance Plot typically maps each (x,y) coordinate pair to its corresponding distance as the z-value, creating a smooth surface with a color gradient tied to distance values. Pyqtgraph's SurfacePlotItem is exactly what you need here—your earlier attempts might have had mismatched data formats or missing parameter tweaks to match Matlab's style.

Step-by-Step Implementation

First, let's make sure your data is in the right shape, then build the plot with settings that mimic Matlab's default look:

1. Validate Data Format

  • Your x and y should be 1D arrays (e.g., sorted coordinate lists).
  • Your distance matrix should be a 2D array where z[i][j] corresponds to the distance at (x[j], y[i])—pyqtgraph expects the z-matrix to have rows aligned with y values and columns aligned with x values. If your matrix is transposed, just use dist_matrix.T to fix it.

2. Example Code (Replace with Your Data)

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

# ----------------------
# Replace these with YOUR actual data
x = np.linspace(0, 10, 50)  # Your x coordinate list
y = np.linspace(0, 10, 50)  # Your y coordinate list
# Simulated distance matrix—swap this with your precomputed matrix
dist_matrix = np.sqrt((x[:, np.newaxis] - y) ** 2)
# ----------------------

# Create the main application window
app = QtGui.QApplication([])
view = pg.GraphicsLayoutWidget()
view.show()
view.setWindowTitle('3D Euclidean Distance Plot')

# Add a 3D plot and adjust camera to match Matlab's default perspective
plot3d = view.addPlot(title='Distance vs. Coordinates')
# Azimuth = horizontal rotation, elevation = vertical tilt, distance = zoom level
plot3d.setCameraPosition(azimuth=-45, elevation=30, distance=50)

# Create the surface plot with shaded lighting (matches Matlab's 3D look)
surface = pg.SurfacePlotItem(
    x=x, 
    y=y, 
    z=dist_matrix,
    shader='shaded',  # Critical for realistic 3D lighting
    color=(255, 255, 255, 200)  # Base color with transparency
)
plot3d.addItem(surface)

# Add a color bar (like Matlab's colorbar) to map colors to distance values
color_bar = pg.ColorBarItem(
    values=(np.min(dist_matrix), np.max(dist_matrix)),
    colorMap=pg.colormap.get('viridis')  # Use 'jet' if you prefer Matlab's classic gradient
)
color_bar.setImageItem(surface)
view.addItem(color_bar)

# Start the application
QtGui.QApplication.instance().exec_()

Key Fixes for Your Earlier Issues

  • Why MeshPlot didn't work: MeshPlot is designed for irregular, unstructured grids. Your distance data is likely based on regular (x,y) coordinates, so SurfacePlot is the right tool.
  • Adjusting the view: The setCameraPosition parameters let you replicate Matlab's default 3D angle—tweak azimuth and elevation until it looks familiar.
  • Color matching: Use pg.colormap.get('jet') instead of 'viridis' if you want the exact color gradient Matlab uses for distance plots.

Debugging Tips

  1. Print the shapes of your data first: print(x.shape, y.shape, dist_matrix.shape)—they should be (N,), (M,), (M,N) respectively.
  2. Test with the simulated data above first to confirm the plot works, then swap in your actual distance matrix.
  3. If the surface looks distorted, double-check that your distance matrix is aligned correctly with x and y (transpose it if needed).

内容的提问来源于stack exchange,提问作者Atieh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:47:08