1

So I have a dataset from a molecular dynamics simulation in which I measured both tilt and rotation of a protein. I am using SciKitLearn's kernel Density estimate function to calculate the gaussian kernel density values for tilt and rotation. These values are correct, as I checked them using seaborn's kdeplot function. The reason I am manually calculating KDE with scikitlearn is that I want to plot these on a polar projection plot, and when I attempt to do so with seaborn, the distribution does not look right at all. I am trying to use the contourf method in matplot lib to do this, however I'm having a hard time converting the KDE values to the correct format for plotting. Specifically the "Z" argument in the matplotlib.pyplot.contour method. I basically would like to make a density plot that is circular from these datasets, because it would be easier to understand the rotation angle specifically. Here is the code for generating the KDE values for each angle array, and then the code I am trying to use to make a contour plot. I can post my entire code for calculating the angles if necessary.

def get_gaussian_kde(data_array):
    data_array = np.reshape(data_array, (len(data_array),1))
    x = np.linspace(np.min(data_array), np.max(data_array), len(data_array))[:, np.newaxis]
    kde = KernelDensity(kernel='gaussian', bandwidth=10).fit(data_array)
    log_dens = kde.score_samples(x)
    return x, np.exp(log_dens)

rotation_kde = get_gaussian_kde(rotation_angles)
tilt_kde = get_gaussian_kde(tiltangles)

Here are what the seaborn plots look like with simple code.

sns.kdeplot(x=rotation_angles, y=tiltangles)

image of resulting polar projection of seaborn kde plot

Image of normal seaborn.kdeplot

The two arrays used to make these plots are shape (2031,) and are simply angles in degrees. This is my first ever post on stack overflow so I apologize if I missed something or formatted this poorly.

Here is the code I am trying to figure use to make the contour plot from that kernel density calculation:

 x = rotation_kde[0]
 y = tilt_kde[0]
 X, Y = np.meshgrid(x,y)
 Z = ???
 plt.contourf(X, Y, Z)

I have tried a couple things such as :

Z = X * rotation_kde[1] + Y * tilt_kde[1]

this results in a contour plot that is not even close to the seaborn.kdeplot.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
hutchcha
  • 11
  • 1
  • Maybe [Adapt von Mises KDE to Seaborn](https://stackoverflow.com/questions/75637853/adapt-von-mises-kde-to-seaborn) is helpful here? – JohanC Mar 21 '23 at 17:43

0 Answers0