Graph of scattered points in 2d smoothly

The main question:

I wrote a little tracing code. He called direct ray tracing, so rays are actually created at the source, move into one and only a mirror, and are reflected. Subsequently, I calculated the intersection of each ray with a plane of my choice, which I call a detector. And what I get on the detector, printing every hit as a pixel, is a scatter plot (x, y). Like this:

import matplotlib.pyplot as plt import numpy as np import random x = np.zeros(1000) y = np.zeros(1000) for i in range(len(x)): x[i] = random.random() y[i] = random.random() plt.plot(x,y,'k,') plt.show() 

Now I am looking for a way to represent the distribution of hit density (intensity) as a smooth image, for example this one.

Thus, the gray scale of each pixel should correspond to the density in the surrounding patch. But all that looks like what I need is for 3d arrays like z = f (x, y).

I also tried hexbin (), but it is not smooth enough, and for very small bins it becomes too slow and only resembles what I have.

So what can i use?

Secondary Question:

Somehow I need to add another dimension, because I'm interested in the parallelism of the incident rays. One of them is to define it as follows:

  • calculating a + a * b, where:

a = angle between the incident beam and the detector normal

b = angle between the incident ray and the yz plane (the rays move approximately parallel to this plane)

  • average value of this quantity

  • deviation from the average value for each hit

I was thinking of incorporating both of these data into one plot, adding color to the gray scale. Is it possible?

I am new to programming, any hints, explanations or alternative ideas would be greatly appreciated.

+4
source share
2 answers

I donโ€™t think you can get away with creating a 2d image, as you mentioned ... you need a 3rd dimension to describe the signal intensity in (x, y). Here is just a quick and dirty example:

 import matplotlib.pyplot as plt import matplotlib.cm as cm import numpy as np # just creating random data with a bunch of 2d gaussians def gauss2d(x, y, a, x0, y0, sx, sy): return a * (np.exp(-((x - x0) / sx)**2 / 2.) * np.exp(-((y - y0) / sy)**2 / 2.)) imsize = 1000 im = np.zeros((imsize, imsize), dtype=float) ng = 50 x0s = imsize * np.random.random(ng) y0s = imsize * np.random.random(ng) sxs = 100. * np.random.random(ng) sys = sxs #100. * np.random.random(ng) amps = 100 + 100 * np.random.random(ng) for x0, y0, sx, sy, amp in zip(x0s, y0s, sxs, sys, amps): nsig = 5. xlo, xhi = int(x0 - nsig * sx), int(x0 + nsig * sx) ylo, yhi = int(y0 - nsig * sy), int(y0 + nsig * sy) xlo = xlo if xlo >= 0 else 0 xhi = xhi if xhi <= imsize else imsize ylo = ylo if ylo >= 0 else 0 yhi = yhi if yhi <= imsize else imsize nx = xhi - xlo ny = yhi - ylo imx = np.tile(np.arange(xlo, xhi, 1), ny).reshape((ny, nx)) imy = np.tile(np.arange(ylo, yhi, 1), nx).reshape((nx, ny)).transpose() im[ylo:yhi, xlo:xhi] += gauss2d(imx, imy, amp, x0, y0, sx, sy) plt.imshow(im, cmap=cm.gray) plt.show() 

Basically you process data such as a 2d image with a CCD, each pixel containing the signal level.

(I would add that depending on what you are trying to highlight in the data, you can use a scatter plot, but change the size / opacity of the points to show your information ... it really depends on what you are trying to achieve .)

Actually, I donโ€™t understand what exactly you want to build from the intensity of the beam, but if you accept rays falling under the image at an angle, you need to calculate the projected intensity of the beam on the plane. And this is another question about how you plan to work with Matplotlib.

+1
source

I assume that your main question includes two main steps: firstly, the calculation of the density function for the scatter points, and secondly, its construction itself. So, if you have a function z = f (x, y), where z is the estimated density at the point (x, y), you can use the matplotlib methods that you already examined.

As for the first step, I would suggest looking at the kernel density estimation routines in scipy.stats.kde . Basically you do

 density = scipy.stats.gaussian_kde(scatterpoints) 

and then you can estimate the density for each point from

 z = density([x,y]) 
+1
source

Source: https://habr.com/ru/post/1432365/


All Articles