Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Finding the total probability under a shape in a bivariate KDE plot in Python

$
0
0

I have a set of points stored as (x,y) values. My goal is the map these onto a coordinate plane and create a continuous PDF distribution.

I would like to apply polygons to the figure and get the total summed probability under the shape and return that to the user. The shape would be stored as a series of coordinates, so [(0,0), (1,0), (1,1),(0,1),(0,0)] would represent a square.

So far, I have plotted the points using a seaborn.kdeplot, and that generates a beautiful plot, which the sum of every point adds to around 100%.

However, I am struggling to effectively apply shapes directly the the graph. I've tried a few online solutions, but have been unable to find any method to get the cumulative probability under a shape directly on the kde plot. My code is below:

def get_field_matrix(csv_name, coordinates):    # ... Some code that loads in a csv and names it df_heatmap    # df_heatmap has two columns, ActualX and ActualY which are the series of points for the kde    # Create a KDE-based heatmap using seaborn    kde = sns.kdeplot(x='ActualX', y='ActualY', data=df_heatmap, cmap='viridis', fill=True, cbar=True, weights=np.linalg.norm(df_heatmap, axis=1) ** .3)    # Set labels and title    plt.xlabel('X-axis')    plt.ylabel('Y-axis')    plt.title('KDE-based Heatmap of X, Y Coordinates')    # Coordinates is the list of tuples that make up the shape to be applied.     # Any shape may be represented, but it will always be a shape draw with a single line    # Creates a matplotlib path out of the coordinates    shape_path = Path(coordinates)    shape_patch = PathPatch(shape_path, lw=0)    plt.gca().add_patch(shape_patch)    print("Summed Probability over the shape:", "A")    # Set aspect ratio to be equal    plt.gca().set_aspect('equal', adjustable='box')    # Show the plot    plt.show()    return kde

Apologies for the long code.Is there some library function I am missing to apply the cdf?

Thank you for your help!


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>