Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:40:41+00:00 2026-06-09T09:40:41+00:00

I’m using numpy, in particular the histrogram2d function. I am binning a 3D spatial

  • 0

I’m using numpy, in particular the histrogram2d function.
I am binning a 3D spatial distribution of points (arrays x,y and z) with a 2d histogram. For each point I have an associated density field d.

If I do something like that

import numpy as np
H, xedges, yedges = np.histogram2d(x,y,bins=200,weights=d)

The histogram H represent the sum of the density along the line-of-sight (in this case in the z-axis). This is pretty fast and easy considering that I’m working with very big arrays.

Now I want to go further and instead of plotting the sum of the density filed along the line-of-sight I would like to get the maximum of the density in each 2D bin.
I coded the possible solution:

from numpy import *
x=array([0.5,0.5,0.2,0.3,0.2,0.25,0.35,0.6,0.1,0.22,0.7,0.45,0.57,0.65])
y=array([0.5,0.5,0.28,0.18,0.85,0.9,0.44,0.7,0.1,0.22,0.7,0.45,0.54,0.65])
d=array([1,1,2,2,3,5,6,8,7,9,6,10,5,7])

bins=linspace(0,1,64)

idx=digitize(x,bins) 
idy=digitize(y,bins) 

img2=zeros((len(bins),len(bins)))

for i in arange(0,len(d)):
  dummy=idx[i]
  dummy2=idy[i]
  img2[dummy][dummy2]=max(d[i],img2[dummy][dummy2])

However the loop in the last lines might be really slow for a huge dataset. Any idea on how I can make it faster?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:40:43+00:00Added an answer on June 9, 2026 at 9:40 am

    Here is how I would do it, sorry I don’t have time to write up the code right now:

    1. Use numpy.ravel_multi_index to turn the 2d problem into a 1d problem.
    2. Take a look at the implementation of numpy.unique, you want to do something like that to get unique bin values, but you want to do it in such a way so that it also gives you the min/max of d at the same time. numpy.lexsort might also help here.
    3. To move back into 2d space it should be as simple as img2.flat[uniq_1d_bin_value] = bin_max

    I hope that’s enough to get you started. If you have trouble, you can post your code and let us know where you got stuck and maybe I, or someone else, can help put you on the right path again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.