4

I believe the fix to this will be relatively simple, but I can't seem to figure out how to convolve a scatter plot that I've plotted in python.

I have 2 data arrays, one of galactic latitudes and one of galactic longitudes, and I've plotted them with a hammer projection to represent a distribution of stars in galactic coordinates.

Now, I want to use boxcar smoothing to smooth the plot with 15 degree boxes. I have tried using astropy.convolution with convolve and Box2DKernel, but I can't seem to make it work. I've also looked at examples from http://docs.astropy.org/en/stable/convolution/kernels.html but I don't understand how to translate their examples to what I need to do. They seem to be plotting a 2D function and smoothing that. Can I not convolve a plot and bin up the points by where they are on the graph? The only thing that I've gotten to display anything produces a straight line and I don't understand why. I'm very new to python so this has been giving me a lot of trouble.

This is the code that I have so far:

This plots the two arrays into a hammer projection:

from astropy import units as u
import astropy.coordinates as coord
glat = coord.Angle(pos_data['GLAT']*u.degree)
glon = coord.Angle(pos_data['GLON']*u.degree)
glon= glon.wrap_at(180*u.degree)

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,12))
ax = fig.add_subplot(211, projection="hammer")
ax.scatter(glon.radian, glat.radian)
ax.grid(True)

This is my attempt at convolving the data:

from astropy.convolution import convolve, Box2DKernel
data = [glon, glat]
kernel = Box2DKernel(10)
smoothed = convolve(data, kernel)

ax = fig.add_subplot(212, projection="hammer")
ax.scatter(smoothed[0]*u.radian, smoothed[1]*u.radian)
ax.grid(True)

Like I said, it's just one of many attempts that ended up giving something instead of an error, but I'm not sure that I'm using the function correctly at all. I'm not sure (or I don't think) that I can create "data" the way that I did, but any other combination of arrays or convolving each as a 1D array didn't work either.

Any ideas would be really helpful, thanks.

P Abb
  • 41
  • 3
  • Hi Abb, Welcome to StackOverflow. Could you post the code and ask specific place where you might need help rather than pointing to tutorial? – WoodChopper Dec 03 '15 at 19:35
  • I added the current code that I have above. Thanks! – P Abb Dec 03 '15 at 21:03
  • 1
    Convolution can only be applied to regularly gridded data, whereas what you have is a collection of points. I think you are looking for histograms, not convolution. Try binning the data using matplotlib.pyplot.hist or numpy.histogram and plotting that. – keflavich Dec 04 '15 at 07:04

1 Answers1

1

It seems like you're looking for Kernel Density Estimation, which is a way of turning individual measurements of spatial point patterns into a continuous distribution. I happen to prefer the scikit-learn implementation. You can then use the basemap package to do your plotting. The following code should be adaptable to your situation, where ra and dec are arrays of your stars' Right Ascension and Declination (you'll have to be careful about radians vs degrees here):

from sklearn.neighbors import KernelDensity
from sklearn.grid_search import GridSearchCV

data = np.column_stack((ra, dec))

# use a tophat/boxcar kernel and a haversine (spherical) metric
p = {'bandwidth': np.logspace(-1, 1, 20), 'kernel'='tophat', 
     'metric'='haversine'}
grid = GridSearchCV(KernelDensity(), params)
grid.fit(data)

Then you should be able to define a meshgrid over which to evaluate your KDE, and then plot it using imshow/pcolormesh/something else over a Hammer projection (see here or here)

DathosPachy
  • 742
  • 1
  • 6
  • 17