[Python] Python Matplotlib: Creating grid-based wind velocity visualization with discrete...

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a wind velocity visualization in Python using Matplotlib that shows both wind speed (as colored grid cells) and wind direction (as arrows).

    Original image such as:

    [​IMG]

    Expected Output:

    [​IMG]

    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import numpy as np

    # Path to your uploaded image
    image_path = r"C:\Users\aaa\Downloads\2W.jpg" # Use raw string to avoid escape issues

    # Load the image
    img = mpimg.imread(image_path)

    # Generate synthetic arrow data (replace with real data)
    x = np.linspace(0, img.shape[1], 20) # X-axis arrow positions
    y = np.linspace(0, img.shape[0], 20) # Y-axis arrow positions
    X, Y = np.meshgrid(x, y)
    U = np.sin(Y / 100) # X-component of arrow (example data)
    V = np.cos(X / 100) # Y-component of arrow (example data)

    # Plot the image as background
    plt.figure(figsize=(10, 8))
    plt.imshow(img, extent=[0, img.shape[1], img.shape[0], 0])

    # Overlay arrows (quiver plot)
    plt.quiver(X, Y, U, V, color="white", scale=30, pivot="middle", alpha=0.7)

    # Optional: Add contour lines for additional data visualization
    Z = np.sqrt(U**2 + V**2) # Example scalar field (magnitude of flow)
    contour = plt.contour(X, Y, Z, levels=10, cmap="jet", alpha=0.6)
    plt.colorbar(contour, label="Flow Magnitude")

    # Final touches
    plt.axis("off") # Turn off axis labels
    plt.title("Carbon Flow Visualization")
    plt.show()


    output image from above code: [​IMG]

    Continue reading...

Compartilhe esta Página