0

Happy new year everyone!
I'm trying to superpose the plots of scalar and vector fields using Matplotlib's quiver and pcolormesh. The data I would like to represent is organized in a file of five columns, the first two are the nodes coordinates x and y which are equally spaced. The following two are the vector field components vx and vy, and finally the last column gives the values of the scalar field v used with color map.

I would like to precise that the nodes with zero vx and vz should not be included in the scalar map.

Here is a sample of my data file:

0.1  0.1 -0.005  -0.0016  0.2482
0.1  0.3  0.0051 -0.0015  0.2428
0.1  0.5 -0.0002 -0.0023  0.2434
0.1  0.7 -0.004  -0.0023  0.2406
0.1  0.9 -0.0012 -0.002   0.2409
0.1  1.1 -0.0041 -0.0021  0.2393
0.1  1.3  0.0002 -0.0021  0.2404
0.1  1.5 -0.0067 -0.0025  0.237
0.1  1.7  0.0004 -0.0027  0.2387
0.1  1.9  0.0013 -0.0025  0.2387
0.3  0.1  0.0018 -0.002   0.2444
0.3  0.3  0.0105 -0.0019  0.2401
0.3  0.5  0.0031 -0.0028  0.2409
0.3  0.7 -0.0014 -0.0028  0.239
0.3  0.9  0.0009 -0.0024  0.2382
0.3  1.1  0.0013 -0.0026  0.2358
0.3  1.3  0.0021 -0.0024  0.2397
0.3  1.5 -0.0008 -0.0027  0.2386
0.3  1.7  0.0026 -0.0029  0.2394
0.3  1.9  0.0025 -0.0028  0.2395
0.5  0.1  0.0041 -0.0015  0.2485
0.5  0.3  0.0123 -0.0015  0.2439
0.5  0.5  0.0018 -0.0022  0.2454
0.5  0.7 -0.0052 -0.0022  0.2445
0.5  0.9 -0.0021 -0.002   0.2426
0.5  1.1 -0.0034 -0.0023  0.2396
0.5  1.3 -0.0027 -0.0022  0.242
0.5  1.5 -0.0077 -0.0024  0.242
0.5  1.7 -0.0006 -0.0026  0.2417
0.5  1.9 -0.0002 -0.0025  0.2409
Tom de Geus
  • 5,625
  • 2
  • 33
  • 77

1 Answers1

1

Hoping it's useful to anyone else looking for it:

import matplotlib.pyplot as plt
import numpy as np


def read_data(data_file):
    # https://stackoverflow.com/questions/3277503/
    with open(data_file, 'r') as data:
        lines = data.readlines()

    x = []
    y = []
    vx = []
    vy = []
    v = []
    for line in lines:
        # https://stackoverflow.com/questions/2492415/
        numbers = [float(n) for n in line.split()]
        x.append(numbers[0])
        y.append(numbers[1])
        vx.append(numbers[2])
        vy.append(numbers[3])
        v.append(numbers[4])

    x = np.array(x)
    y = np.array(y)
    vx = np.array(vx)
    vy = np.array(vy)
    v = np.array(v)
    n_lines = 0
    i = 0
    while x[0] == x[i]:
        n_lines += 1
        i += 1
    n_cols = len(x) / n_lines
    x = x.reshape((n_cols, n_lines))
    y = y.reshape((n_cols, n_lines))
    vx = vx.reshape((n_cols, n_lines))
    vy = vy.reshape((n_cols, n_lines))
    v = v.reshape((n_cols, n_lines))
    # Change the conditions on the line bellow to what you want,
    # it should make the 'contourf' not draw on those points.
    v[(abs(vx) <= 0.002) & (abs(vy) <= 0.002)] = np.NaN
    #https://stackoverflow.com/questions/16343752/
    return x, y, vx, vy, v


def plot_all(x, y, vx, vy, v):
    # https://stackoverflow.com/questions/12079842/
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.contourf(x, y, v)
    ax.quiver(x, y, vx, vy)
    fig.show()


if __name__ == "__main__":
    data = read_data('data.txt')
    plot_all(*data)

resulting graph with white holes where v is NaN

berna1111
  • 1,811
  • 1
  • 18
  • 23