Python中固定Y轴坐标绘制图像强度剖面并指定线段起止点的实现方案及剖面图红线消除方法
Solution to Your Moon Image Profile Plotting Problems
Got it, let's work through both of your issues to get precise, reproducible profile plots and get rid of that unwanted red line.
1. Precise Fixed-Y Intensity Profile
The mouse-click method is great for quick checks, but for side-by-side comparisons across images, hardcoding exact coordinates is the way to go. Here's how to lock your profile line to a fixed Y-axis position perfectly:
- First, define a single
fixed_yparameter (this is your target row/Y-coordinate). Use the same value across all your images to ensure direct comparisons. - Calculate the start and end points automatically to span the full width of the image:
startuses(fixed_y, 0)to anchor to the left edge at your target Y.enduses(fixed_y, image.shape[1] - 1)to reach the right edge (no need to manually count pixels—this adapts to any image width).
2. Eliminating the Red Line from the Profile Plot
Looking at your code, the red line is only drawn on the image subplot (to mark where the profile is taken). If you're seeing a red line in the intensity profile plot itself, it's likely a default color quirk or accidental setting. We'll explicitly set the profile plot's color to avoid this.
Full Modified Code
from matplotlib import pyplot as plt from skimage.measure import profile_line from skimage.io import imread # Load your moon image image = imread(r".....path to image....\moon2.png") # Set your fixed Y-coordinate (row) here - use this same value for all comparison images fixed_y = 210 # Auto-calculate start/end points for full horizontal cross-section start = (fixed_y, 0) # Left edge at fixed Y end = (fixed_y, image.shape[1] - 1) # Right edge at fixed Y # Compute the intensity profile profile = profile_line(image, start, end) # Create subplots with a reasonable figure size (30,70 was way too tall!) fig, ax = plt.subplots(1, 2, figsize=(30, 10)) # Image subplot (optional: remove the ax[0].plot line if you don't want the red marker on the image) ax[0].set_title('Moon Image with Profile Line') ax[0].imshow(image) ax[0].plot([start[1], end[1]], [start[0], end[0]], 'r') # Red line marks the profile's position on the image # Intensity profile subplot (explicit color to avoid red) ax[1].set_title('Horizontal Intensity Profile') ax[1].plot(profile, color='blue') # Set to any color you prefer ax[1].set_xlabel('Pixel Position (X-axis)') ax[1].set_ylabel('Pixel Intensity') # Clean up spacing between subplots plt.tight_layout() plt.show()
Key Notes:
- Reproducibility: By using
fixed_yas a single parameter, you can easily copy this code to other images and just update the file path—no more guesswork with mouse clicks. - Red Line Control: If you don't want the red line marking the profile on the image, just delete the
ax[0].plot(...)line. For the profile plot itself, explicitly settingcolorensures it never defaults to red. - Figure Size: I adjusted the figure size to
(30,10)since(30,70)creates an unwieldy tall plot—feel free to tweak this to fit your needs.
内容的提问来源于stack exchange,提问作者A.Sa
相关产品推荐
相关产品推荐

