Setting the clip in the marine area

I am having trouble cutting off the graph seaborn(a kdeplot, in particular), as I thought it would be pretty simple for this example in matplotlibdocs .

For example, the following code:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

fig = plt.figure()
ax = fig.add_subplot(111, frameon=False, xticks=[], yticks=[])

random_points = np.array([p for p in np.random.random(size=(100, 2)) if 0 < p[0] < 1 and 0 < p[1] < 1])

kde = sns.kdeplot(random_points[:,0], random_points[:,1], ax=ax)

xmin, xmax = kde.get_xlim()
ymin, ymax = kde.get_ylim()

patch = mpl.patches.Circle(((xmin + xmax)/2, (ymin + ymax) / 2), radius=0.4)
ax.add_patch(patch)
kde.set_clip_path(patch)

Results in the next release:

enter image description here

I would like to copy this result so that the KDE contour lines do not appear outside the circle. I have not found a way to do this so far ... is this possible?

+4
source share
2 answers

, , , ( , ). , , , .

, . , matplotlib (, , ?), , shapely, , .

symmetric_difference. A - .

shapely.geometry.Polygon. , , , .

manhattan, manhattan.envelope, . :

unmanhattan = manhattan.envelope.symmetric_difference(manhattan)

, :

enter image description here

, shapely a matplotlib Patch, ? descartes .

unmanhattan_patch = descartes.PolygonPatch(unmanhattan)

, ! :

unmanhattan_patch = descartes.PolygonPatch(unmanhattan)
ax.add_patch(unmanhattan_patch)
sns.kdeplot(x=points['x_coord'], y=points['y_coord'], ax=ax)

:

enter image description here

, (-), :

enter image description here

+1

, 'imshow'.

, "" .

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import seaborn as sns

# Color plot except polygon
def mask_outside(poly_verts, facecolor = None, ax = None):
    from matplotlib.patches import PathPatch
    from matplotlib.path import Path

    if ax is None: ax = plt.gca()
    if facecolor is None: facecolor = plt.gcf().get_facecolor()

    # Construct inverse polygon
    xlim, ylim = ax.get_xlim(), ax.get_ylim()
    bound_verts = [(xlim[0], ylim[0]), (xlim[0], ylim[1]), 
                   (xlim[1], ylim[1]), (xlim[1], ylim[0]), (xlim[0], ylim[0])]
    bound_codes = [Path.MOVETO] + (len(bound_verts) - 1) * [Path.LINETO]
    poly_codes = [Path.MOVETO] + (len(poly_verts) - 1) * [Path.LINETO]

    # Plot it
    path = Path(bound_verts + poly_verts, bound_codes + poly_codes)
    ax.add_patch(PathPatch(path, facecolor = facecolor, edgecolor = 'None', zorder = 1e+3))

# Your example
fig = plt.figure()
ax = fig.add_subplot(111, frameon=False, xticks=[], yticks=[])
random_points = np.array([p for p in np.random.random(size=(100, 2)) if 0 < p[0] < 1 and 0 < p[1] < 1])
kde = sns.kdeplot(random_points[:,0], random_points[:,1], ax=ax)

xmin, xmax = kde.get_xlim()
ymin, ymax = kde.get_ylim()

patch = mpl.patches.Circle(((xmin + xmax) / 2, (ymin + ymax) / 2), radius=0.4)
mask_outside([tuple(x) for x in patch.get_verts()]) # call before add_patch!
ax.add_patch(patch)

plt.show()

enter image description here

+1

Source: https://habr.com/ru/post/1664743/


All Articles