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

如何用Matplotlib在等高线图(热力图)上绘制指定半径的圆形

Got it, let's tackle this visualization problem step by step. You want to turn your ultrasonic sensor's radius measurements into a heatmap where regions matching those radii are brighter, right? This will make it super easy to spot all possible object positions in the sensor's field of view.

Here's a complete, customizable solution using Matplotlib and Numpy:

import numpy as np
import matplotlib.pyplot as plt

# Replace this with your actual measured radii array
measured_radii = [0, 3.4, 5.1, 7.2, 9.0]

# 1. Create a coordinate grid covering the sensor's full field of view
max_radius = max(measured_radii) + 1  # Add a small buffer to avoid cutting off outer circles
x = np.linspace(-max_radius, max_radius, 500)  # 500 points for smooth resolution
y = np.linspace(-max_radius, max_radius, 500)
X, Y = np.meshgrid(x, y)

# 2. Calculate distance from the sensor (origin) for every grid point
distance_from_sensor = np.sqrt(X**2 + Y**2)

# 3. Build the intensity heatmap
intensity = np.zeros_like(distance_from_sensor)
sigma = 0.1  # Controls ring sharpness: smaller = sharper, larger = softer (adjust for your sensor's precision)

for radius in measured_radii:
    # Use a Gaussian kernel to create a bright, smooth ring around each measured radius
    intensity += np.exp(-((distance_from_sensor - radius)**2) / (2 * sigma**2))

# 4. Plot the heatmap
plt.figure(figsize=(8, 8))
heatmap = plt.imshow(
    intensity,
    extent=[-max_radius, max_radius, -max_radius, max_radius],
    cmap='viridis',  # Swap with 'plasma'/'inferno' for different color schemes
    origin='lower'
)
plt.colorbar(heatmap, label='Detection Intensity')

# 5. Overlay dashed circles for each measured radius
for radius in measured_radii:
    circle = plt.Circle((0, 0), radius, color='white', linestyle='--', linewidth=1.5, fill=False)
    plt.gca().add_artist(circle)
    # Optional: Add text labels for each radius
    plt.text(radius, 0, f'r={radius}', color='white', fontsize=10, ha='left', va='center')

# 6. Polish the plot
plt.xlabel('X Position (match your radius units: cm/m/etc.)')
plt.ylabel('Y Position (match your radius units: cm/m/etc.)')
plt.title('Ultrasonic Sensor Object Positions Heatmap')
plt.axis('equal')  # Critical to keep circles round (not elliptical)
plt.show()

Key Details & Customization Tips:

  • Grid Resolution: The 500 in linspace controls how smooth your heatmap is. Higher numbers = smoother, but slower to render.
  • Sigma Adjustment: If your sensor has noisy measurements, increase sigma (e.g., to 0.2 or 0.3) to widen the bright rings. For precise readings, keep it small.
  • Colormaps: Swap viridis with other Matplotlib colormaps like plasma, inferno, or magma to match your preference.
  • Duplicate Radii: If you have repeated radius values, the intensity will stack automatically—making those regions even brighter, which is great for highlighting frequent detections.
  • Units: Don't forget to update the axis labels to match your radius units (centimeters, meters, etc.).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:35:05