[Python] Matplotlib quiver colormap

Discussão em 'Python' iniciado por Stack, Abril 30, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to plot interpolated velocity data from Fluent in Python. Interpolated velocity vector components are normalized by size and plotted with quiver function. I'm trying to represent the velocity magnitude with a colormap, but whatever I do, the arrows are black. Am I missing something?

    Sample code is below; the plot in question is at the end (axs[3]).

    import pandas as pd
    import numpy as np
    import scipy
    import matplotlib.pyplot as plt
    import matplotlib

    input_filename = 'period_rot_shadow_vel-mag-vel_comps.txt'

    df_results = pd.read_csv(input_filename,
    sep =',', skiprows = 1,
    names = ['nodenumber', 'X', 'Y', 'Z', 'vel_mag', 'v_x', 'v_y', 'v_z',
    'v_ax', 'v_rad', 'v_tang'])

    x_data_min = df_results['X'].min()
    x_data_max = df_results['X'].max()
    y_data_min = df_results['Y'].min()
    y_data_max = df_results['Y'].max()

    x_size = np.abs(x_data_max) + np.abs(x_data_min)
    y_size = np.abs(y_data_max) + np.abs(y_data_min)

    rect_mesh_spacing = 0.001

    N_x_steps = int(x_size/rect_mesh_spacing) + 1
    N_y_steps = int(y_size/rect_mesh_spacing) + 1

    rect_mesh_x_axis = np.array([])
    rect_mesh_y_axis = np.array([])

    for i in range(0, N_x_steps):
    x = x_data_min + i * rect_mesh_spacing
    rect_mesh_x_axis = np.append(rect_mesh_x_axis, x)

    for i in range(0, N_y_steps):
    y = y_data_min + i * rect_mesh_spacing
    rect_mesh_y_axis = np.append(rect_mesh_y_axis, y)

    #use meshgrid function to create a rectangular grid, meshgrid returns a tuple of ndarrays
    xx, yy = np.meshgrid(rect_mesh_x_axis, rect_mesh_y_axis)

    #create an array from meshgrid points
    xy_mesh = np.array((xx,yy)).T

    #define input coordinates (imporeted data point coordinates) as numpy arrays
    input_coords = np.array(list(zip(df_results['X'].to_numpy(), df_results['Y'].to_numpy())))

    # scipy.spatial.KDTree function is used to lookup nearest-neighbour for defined points
    tree = scipy.spatial.KDTree(input_coords)

    dist, idx = tree.query(xy_mesh)
    #define the radius of lookup of vicinity of points
    radius = (rect_mesh_spacing + rect_mesh_spacing) / 2
    distance_filter = dist <= radius

    #filter out the points in rectangular grid that are close to the exported data points
    xx_filtered, yy_filtered = xy_mesh[distance_filter].T

    v_x_interpol= scipy.interpolate.griddata((df_results['X'], df_results['Y']), df_results['v_x'],
    (xx_filtered, yy_filtered), method = 'nearest')
    v_y_interpol= scipy.interpolate.griddata((df_results['X'], df_results['Y']), df_results['v_y'],
    (xx_filtered, yy_filtered), method = 'nearest')
    v_mag_interpol = scipy.interpolate.griddata((df_results['X'], df_results['Y']), df_results['vel_mag'],
    (xx_filtered, yy_filtered), method = 'nearest')

    fig, axs = plt.subplots(1,4)
    fig.set_size_inches(13, 4)

    # Plot non-interpolated data
    axs[0].quiver(df_results['X'], df_results['Y'], df_results['v_x'], df_results['v_y'])

    # Plot interpolated data
    axs[1].quiver(xx_filtered, yy_filtered, v_x_interpol, v_y_interpol)

    #Plot interpolated data with normalized vector sizes (all arrows of the same size)
    #formula bellow is ordinary way to normalize vectors to the size of 1, it just divides every vector by it's length
    r = (v_x_interpol**2 + v_y_interpol**2)**0.5
    axs[2].quiver(xx_filtered, yy_filtered, v_x_interpol/r, v_y_interpol/r)

    # Plot interpolated data with normalized vectors and normalized colormap
    #colormap is normalized on the vector length in 2D
    r = (v_x_interpol**2 + v_y_interpol**2)**0.5
    axs[3].quiver(xx_filtered, yy_filtered, v_x_interpol/r, v_y_interpol/r,r,
    norm = matplotlib.colors.Normalize(vmin = np.min(r), vmax = np.max(r)),
    cmap = 'bwr')


    plt.show()

    Continue reading...

Compartilhe esta Página