2

I'm trying to sort image 1 I posted so that the densest points are more clearer, the link to an answer i'm using for the implementation indicates what i'm trying to achieve.

How can I make a scatter plot colored by density in matplotlib?

My code being:

# Calculate the point density: Saccade Orientation is an angle, Amplitude is supposed to be the Y value
xy = np.vstack([saccade_orientation_PP, saccade_amplitude_PP])
z = gaussian_kde(xy)(xy)

# Sort the points by density, so that the densest points are plotted last
idx = z.argsort()
x, y, z = np.array(saccade_orientation_PP)[idx], np.array(saccade_amplitude_PP)[idx], z[idx]

ax1 = plt.subplot(121, polar=True)
ax1.scatter(saccade_orientation_PP, saccade_amplitude_PP, c=z, edgecolor='', alpha = 0.75)

This is the result I get if I do not use the two lines of code that sort the points by density (commented out)

Resulting image **without** values sorted by z

And this is the result when the sorting is implemented

Resulting image **with** values sorted by z

I'm trying to achieve a similar goal as displayed in the answer in the link, so image 1 but cleaner but I do not understand why when I sort it I get the 2nd image below.

Thank you.

1 Answers1

2

The key issue here is the usage of polar coordinates.

The densities must be computed on xy cartesian coordinates, usage of polar coordinates results in weird distance values, try plotting your data in a rectangular plot and will see.

You can include a coordinate transformation before the z values are computed. E.g.:

xy = np.vstack([np.sin(angle)*amp, np.cos(angle)*amp]) 
z = gaussian_kde(xy)(xy) 
Javier JC
  • 309
  • 2
  • 3
  • Thank you for your reply, what does the amp variable stand for? And the second value 'saccade_amplitude_PP' are coordinates whereas orientation is a radius angle so i'm struggling to understand the transformation logically i.e. xy = np.vstack([np.sin(saccade_orientation_PP), np.cos(saccade_amplitude_PP)]) is wrong – bigPoppa350 Mar 09 '19 at 17:07
  • np.vstack([np.sin(saccade_orientation_PP)*saccade_amplitude_PP,np.sin(saccade_orientation_PP)*saccade_amplitude_PP]) is the transformation in your example – Javier JC Mar 09 '19 at 17:42