0

I'm using a 3 dimensional dataset from a simulation. It contains the coordinates of some gaseous particles and their associated magnetic field vector. I'm trying to plot a 2D histogram of some region of this simulation, weighted in terms of the magnetic field intensity.

I have two arrays: one coordinates array (x,y,z) of the N particles I want to represent (thus a (N,3) array) and one array that contains the magnetic field vector for each of these particle (also (N,3)). My original idea was to compute the norm of each of these vectors and then use it as my weighting array in hist2d function :

weights = np.linalg.norm(magnetic_field_array,axis=1)
hist = ax.hist2d(x,y,weights=weights)

The problem is that by doing it this way, I will only sum the norm (positive) contribution of each vector, no matter in which direction they are going. For instance, if I have only two particles falling in a specific bin, whose magnetic field are (1,0,0) and (-1,0,0), the total contribution will be positive even though the total magnetic field is obviously zero.

Is there any way to use hist2d or any other function to get the total magnetic field vector in each pixel before computing its norm?

Michael S.
  • 3,050
  • 4
  • 19
  • 34

1 Answers1

0

You have to apply hist2d on each components, and then compute the norm after that. Here is a possible implementation:

import numpy as np
import matplotlib.pylab as plt

N = 60
nbins = 10
magnetic_field_array = np.random.rand(N, 3)
x, y, z = np.random.rand(3, N)
hist3d = np.zeros((nbins, nbins, 3))
for i in range(3):
    hist3d[:, :, i], *__ = plt.hist2d(
        x, y, bins=nbins, weights=magnetic_field_array[:, i]
    )
hist = np.linalg.norm(hist3d, axis=2)
Thomasi Muc
  • 164
  • 7