I have some data as x,y arrays and an array of v values corresponding to them, i.e for every x and y there is a v with matching index.
What I have done: I am creating a grid on the x-y plane and then the v-values fall in cells of that grid. I am then taking mean of the v-values in each cell of the grid.
Where I am stuck: Now, I want to identify the cells where the mean of v is greater than 2 and plot the histograms of those cells (histogram of original v values in that cell). Any ideas on how to do that? Thanks!
EDIT: I am getting some histogram plots for mean>2 but it also includes histograms of empty cells. I want to get rid of the empty ones and just keep mean>2 cells. I tried print(mean_pix[(mean_pix!=[])])
but it returns errors.
My full code is:
import numpy as np
import matplotlib.pyplot as plt
x=np.array([11,12,12,13,21,14])
y=np.array([28,5,15,16,12,4])
v=np.array([10,5,2,10,6,7])
x = x // 4
y = y // 4
k=10
cells = [[[] for y in range(k)] for x in range(k)] #creating cells or pixels on x-y plane
#letting v values to fall into the grid cells
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(y == ycell) & (x == xcell)]
for ycell in range(k):
for xcell in range(k):
this = cells[ycell][xcell]
#print(this)
#fig, ax = plt.subplots()
#plt.hist(this)
#getting mean from velocity values in each cell
mean_v = [[[] for y in range(k)] for x in range(k)]
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(y == ycell) & (x == xcell)]
this = cells[ycell][xcell]
mean_v[ycell][xcell] = np.mean(cells[ycell][xcell])
mean_pix= mean_v[ycell][xcell]
fig, ax = plt.subplots()
plt.hist(this[(mean_pix>2)]) # this gives me histograms of cells that have mean>2 but it also gives histograms of empty cells. I want to avoid getting the empty histograms.
Maybe there is a better way, but you can create an empty list and append the lists that you want to plot:
import numpy as np
import matplotlib.pyplot as plt
x=np.array([11,12,12,13,21,14])
y=np.array([28,5,15,16,12,4])
v=np.array([10,5,2,10,6,7])
x = x // 4
y = y // 4
k=10
cells = [[[] for y in range(k)] for x in range(k)] #creating cells or pixels on x-y plane
#letting v values to fall into the grid cells
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(y == ycell) & (x == xcell)]
for ycell in range(k):
for xcell in range(k):
this = cells[ycell][xcell]
#getting mean from velocity values in each cell
mean_v = [[[] for y in range(k)] for x in range(k)]
to_plot = []
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(y == ycell) & (x == xcell)]
mean_v[ycell][xcell] = np.mean(cells[ycell][xcell])
if mean_v[ycell][xcell]>2:
to_plot.append(cells[ycell][xcell])
for x in to_plot:
fig, ax = plt.subplots()
plt.hist(x)
I also removed some unnecessary code. It should output something like this: