1

I have some data as numpy arrays x, y, v as shown in the code below.

This is actually dummy data for velocity (v) of dust particles in a x-y plane.

I have binned my data into 4 bins and for each bin I have calculated mean of entries in each bin and made a heat map.

Now what I want to do is make a histogram/distribution of v in each bin with 0 as the centre of the histogram.

I do not want to plot the mean anymore, just want to divide my data into the same bins as this code and for each bin I want to generate a histogram of the values in the bins.

How should I do it?

I think this is a way to model the spectrum of an emission line from the gas particles. Any help is appreciated! Thanks!

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt

x = np.array([-10,-2,4,12,3,6,8,14,3])
y = np.array([5,5,-6,8,-20,10,2,2,8])
v = np.array([4,-6,-10,40,22,-14,20,8,-10])

x_bins = np.linspace(-20, 20, 3)
y_bins = np.linspace(-20, 20, 3)

H, xedges, yedges = np.histogram2d(x, y, bins = [x_bins, y_bins], weights = v)

pstat = stats.binned_statistic_2d(x, y, v, statistic='mean', bins = [x_bins, y_bins])

plt.xlabel("x")
plt.ylabel("y")
plt.imshow(pstat.statistic.T, origin='lower',  cmap='RdBu',
            extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])

plt.colorbar().set_label('mean', rotation=270)

EDIT: Please note that my original data is huge. My arrays for x,y, v are very large and I am using 30x30 grid, that is, not just 4quadrants but 900 bins. I might also need to increase the bin number. So, we want to find a way to automatically divide the 'v' data into the regularly spaced bins and then be able to plot the histograms of the 'v' data in each bin.

heatmap

Jerome
  • 49
  • 8

1 Answers1

1

I would iterate over the zipped x and y, then flag if v is inside the quadrant and append them to a quadrant list. after, you can plot whatever you'd like:

x = np.array([-10,-2,4,12,3,6,8,14,3])
y = np.array([5,5,-6,8,-20,10,2,2,8])
v = np.array([4,-6,-10,40,22,-14,20,8,-10])

q1 = []
q2 = []
q3 = []
q4 = []

for i, (x1,y1) in enumerate(zip(x,y)):
    if x1<0 and y1>=0:
        q1.append(v[i])
    elif x1>=0 and y1>=0:
        q2.append(v[i])
    elif x1>=0 and y1<0:
        q3.append(v[i])
    elif x1<0 and y1<0:
        q4.append(v[i])
print(q1)
print(q2)
print(q3)
print(q4)  
#[4, -6]
#[40, -14, 20, 8, -10]
#[-10, 22]
#[]  
plt.hist(q1, density=True)
plt.hist(q2, density=True)
plt.hist(q3, density=True)
#q4 is empty

enter image description here

barker
  • 1,005
  • 18
  • 36
  • Hi @barker, thanks for the answer but actually my original data is huge. My arrays for x,y, v are very large and I am using 30x30 grid, that is, not just 4quadrants but 900 bins. I might also need to increase the bin number. So, can we find a way to generalise the code so that we can automatically divide the data into the regularly spaced bins and then be able to plot their histogram? – Jerome Oct 04 '21 at 03:20
  • hm, sounds like a separate question. I'd ask a new question how to quickly block your plane into squares, then organize your data points inside each square. – barker Oct 05 '21 at 13:38