如何使用绘图自动设置轴? sns.kdeplot

How to automatically set the axes with the plot? sns.kdeplot

我无法使用现场绘图配置绘图图像。 数据提供者使用度量值 x = 105 和 y = 68。

即使在这张照片中,您也可以看到球门在界外。

matplotsoccer.field('white',figsize=10, show=False)
    
#Tidy Axes
plt.axis('Off')
   
    sns.kdeplot(df_acciones_ataque["x"],df_acciones_ataque["y"],n_levels=50, shade="True",cmap = 'coolwarm')
    
plt.ylim(0, 68)
plt.xlim(0,105)
    
#plt.suptitle("Acciones de Contraataques y Ataque Elaborado", fontsize=12)
plt.tight_layout(pad=0, w_pad=0, h_pad=0)
#plt.subplots_adjust(top=0.95)
    
    
#Display Pitch
plt.show()

对于 2D kdeplot,Seaborn 从给定的数据点中选择限制。在最新版本(现在是0.11.1)中,你可以设置一个阈值(thres=)来防止最底层被绘制出来。设置 thres=0 将绘制所有图层;最佳值取决于数据和您要显示的内容。在旧版本中,可以使用 shade_lowest=False.

除此之外,还有clim限制了面积(但不能变大)。为了使图像很好地填充到所需区域,可以同时使用 clim 并明确添加一个具有最低级别颜色的矩形:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
import seaborn as sns

np.random.seed(1234)
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(18, 5))
for ax in (ax1, ax2, ax3):
    ax.plot([0, 0, 105, 105, 0], [0, 68, 68, 0, 0], color='black')
    ax.plot([105 / 2, 105 / 2], [0, 68], color='black')
    ax.add_patch(plt.Circle((105 / 2, 68 / 2), 9.15, fc='none', ec='black'))
    ax.set_aspect('equal')
    cmap = plt.cm.get_cmap('coolwarm')
    sns.kdeplot(x=np.random.normal(105 / 2, 12, 200), y=np.random.normal(68 / 2, 8, 200), n_levels=50,
                shade="True", cmap=cmap, clip=((0, 105), (0, 68)), thresh=0 if ax == ax1 else 0.002, ax=ax)
    ax.set_xlim(-2, 107)
    ax.set_ylim(-2, 70)
ax1.set_title('drawing the zero level')
ax2.set_title('without drawing the zero level')
ax3.add_patch(Rectangle((0, 0), 105, 68, ec='none', fc=cmap(0), zorder=0))
ax3.set_title('adding a rectangle for the zero level')
plt.show()