[Python] How to Eliminate Visible Edges in Matplotlib pcolormesh Plot?

Discussão em 'Python' iniciado por Stack, Junho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with Matplotlib to create a heightmap visualization using pcolormesh, but I'm encountering an issue where the edges of the mesh grid are visible in the output image, despite setting edgecolors='none'. This results in an unwanted wireframe-like appearance on the plot near the edge of the colorplot. I'm trying to achieve a smooth, uninterrupted surface visualization. image

    Despite setting edgecolors='none' and testing with rasterized=True, the wireframe still appear in the saved figure. I've also tried increasing the resolution and adjusting the colormap. When I set color map at 50% opacity i see that the meshgrid comes through my colormap. Like this pic shows (with viridis and alpha=0.5) image

    How can I remove or hide these mesh edges to achieve a completely smooth surface visualization?

    Additional Information: Python version: 3.x Matplotlib version: latest

    Any suggestions or insights would be greatly appreciated!

    Here's a simplified version of my code that shows the same artifact if you zoom in to the edge:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.colors import LinearSegmentedColormap
    from scipy.interpolate import griddata
    from os.path import join

    # Sample data for demonstration
    x = np.random.rand(1000)
    y = np.random.rand(1000) * 100 # Y values scaled to simulate height data
    z = np.random.rand(1000)

    # Convert data to NumPy arrays
    x_np = np.array(x)
    y_np = np.array(y)
    z_np = np.array(z)

    # Define grid resolution
    heightmap_resolution = (512, 512)

    # Create a grid
    grid_x, grid_z = np.meshgrid(np.linspace(np.min(x_np), np.max(x_np), heightmap_resolution[0]),
    np.linspace(np.min(z_np), np.max(z_np), heightmap_resolution[1]))

    # Interpolate Y values on the grid
    points = np.column_stack((x_np, z_np))
    grid_y = griddata(points, y_np, (grid_x, grid_z), method='cubic')

    # Plot setup
    fig_width, fig_height = 210 / 25.4, 297 / 25.4 # Dimensions in inches
    dpi = 1440 # High DPI for detailed output

    fig = plt.figure(figsize=(fig_width, fig_height), dpi=dpi)
    ax = fig.add_axes([0, 0, 1, 1]) # Use full area of the figure
    cmap = LinearSegmentedColormap.from_list('mycmap', ['white', 'black'])
    c = ax.pcolormesh(grid_x, grid_z, grid_y, cmap=cmap, shading='gouraud', edgecolors='none', rasterized=True)
    ax.axis('off') # Turn off the axis

    # Save the figure
    output_dir = r"C:\test" #CHANGE THIS PATH
    heightmap_name = 'out_plot_height_map.png'
    plt.savefig(join(output_dir, heightmap_name), format='png', dpi=dpi, bbox_inches='tight', pad_inches=0)
    plt.close(fig)


    I tried this code, but this 'pixelates' the output. I need the gradient that comes with shading='gouraud', as this heightmap will be used in a manufacturing step and the steps form the pixels are a no-go

    fig, ax = plt.subplots(figsize=(fig_width, fig_height), dpi=dpi)
    im = ax.imshow(grid_y, extent=[np.min(x_np), np.max(x_np), np.min(z_np), np.max(z_np)], interpolation='bicubic', cmap='viridis', aspect='auto')
    ax.axis('off')
    plt.savefig(join(output_dir, heightmap_name), format='png', dpi=dpi, bbox_inches='tight', pad_inches=0)
    plt.close(fig)

    Continue reading...

Compartilhe esta Página