Integrating within a matplotlib contour

37 Views Asked by At

I am trying to write a python script that calculates the area within a matplotlib contour and sums the variable within that same contour in order to calculate the volume and transport within a velocity field.

I have so far tried this method (native to matplotlib and the shoelace method) however I haven't had any success in integrating the two areas outlined in my contour plot

enter image description here

cc_cont = ax1.contour(X,Y,CC_vel,levels=[int(np.nanmax(CC_vel)*CC_percentage)],colors='k')
plt.clabel(cc_cont, inline=True, fontsize=15)

for c in cc_cont.collections:
    polygon = c.get_paths()[0]
    vertices = polygon.vertices
    # print(np.shape(vertices))
    area = 0.5 * np.abs(np.dot(vertices[:, 0], np.roll(vertices[:, 1], 1)) - np.dot(vertices[:, 1], np.roll(vertices[:, 0], 1)))  # Shoelace formula
    ax1.add_patch(Polygon(vertices, color='red', alpha=0.6))
    print(f'Area within contour: {area}')
    variable_sum = np.sum(CC_vel[(X >= min(vertices[:, 0])) & (X <= max(vertices[:, 0])) & (Y >= min(vertices[:, 1])) & (Y <= max(vertices[:, 1]))])  # Sum variable within contour
    print(f'Sum of variable within contour: {variable_sum}')
0

There are 0 best solutions below