Home > Software design >  How to plot histogram for chosen cells using mean as condition in python?
How to plot histogram for chosen cells using mean as condition in python?

Time:06-21

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.

CodePudding user response:

If I understand your question, I think something like this should help:

for i in mean_v:
    for j in i:
        if j > 2:
            #draw the hisrtogram

CodePudding user response:

can you not check if mean_pix > 2 and plot only if it is? Update the last 2 lines with this. I was able to see only plots with data in it, while the others (blank ones) were removed. Let me know...

        if mean_pix > 2: #New line - below two will be inside the IF statement
            fig, ax = plt.subplots()
            plt.hist(this[(mean_pix>2)])
  • Related