如何才能阻止seaborn kdeplots在不创建锐利截图的情况下调整图像大小?

2024-09-29 23:30:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用kdeplots来创建曲棍球运动员击球位置的密度图。我正在并排绘制两个kdeplot进行比较。你知道吗

在密度图上仍然有圆形边缘的情况下,如何防止图像被调整大小?你知道吗

如果我没有指定带宽(bw),我的图像会自动独立调整大小:

enter image description here

如果我指定了一个带宽,我的密度贴图有锐利的边缘。我更希望只有尖锐的边缘在溜冰场图像的边缘。你知道吗

enter image description here

带宽高于0.5会使密度过于简单,但0.5和更低的带宽会创建边。我还想避免kdeplots超出溜冰场图像的边界。你知道吗

我尝试在图像的每个角落添加一个点来“锚定”绘图,但这会使kdeplot远离实际数据。你知道吗

这是溜冰场的图片: https://pick224.com/images/half-rink.png

import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import seaborn as sns
from PIL import Image

# Create the dataframes using rink coordinates
evdatalists = [[326.2265, 102.24], [256.05, 142], [312.95, 147.68], [233.29, 142], [111.9035, 160.9337], [218.1165, 145.7863], [314.8465, 164.72], [168.8035, 380.56], [290.19, 107.92], [312.95, 121.1737], [472.27, 189.3337], [529.17, 227.2], [474.1665, 70.0537], [166.9065, 100.3463], [238.98, 198.8], [398.3, 176.08], [170.7, 121.1737], [311.0535, 107.92], [111.9035, 217.7337], [320.5365, 92.7737], [449.51, 255.6], [366.0565, 147.68], [318.64, 213.9463], [252.2565, 164.72], [271.2235, 109.8137], [174.4935, 172.2937], [267.43, 119.28], [185.8735, 195.0137], [187.77, 66.2663], [305.3635, 96.56], [225.7035, 136.32], [369.85, 159.04], [316.7435, 79.52], [362.2635, 204.48], [227.6, 196.9063], [347.09, 249.92], [309.1565, 102.24], [20.8635, 130.64], [51.21, 388.1337], [367.9535, 149.5737], [331.9165, 164.72], [303.4665, 107.92], [324.33, 117.3863], [286.3965, 200.6937], [273.12, 174.1863], [282.6035, 102.24], [322.4335, 98.4537], [267.43, 177.9737], [500.72, 253.7063], [187.77, 246.1337], [330.02, 261.28], [138.4565, 187.44], [337.6065, 119.28], [146.0435, 206.3737], [85.35, 208.2663], [328.1235, 253.7063], [396.4035, 395.7063], [396.4035, 109.8137], [282.6035, 102.24], [426.75, 183.6537], [256.05, 92.7737], [265.5335, 87.0937], [438.13, 198.8], [347.09, 100.3463], [208.6335, 202.5863], [341.4, 244.24], [331.9165, 130.64], [341.4, 54.9063], [324.33, 77.6263], [267.43, 98.4537], [273.12, 123.0663], [314.8465, 96.56], [261.74, 96.56], [312.95, 104.1337], [189.6665, 145.7863], [390.7135, 191.2263], [286.3965, 119.28], [240.8765, 96.56], [227.6, 187.44], [256.05, 126.8537], [242.7735, 143.8937], [326.2265, 98.4537], [197.2535, 257.4937], [369.85, 168.5063], [271.2235, 123.0663], [256.05, 88.9863], [377.4365, 153.36], [290.19, 229.0937], [421.06, 77.6263], [193.46, 221.52], [312.95, 162.8263], [231.3935, 130.64], [293.9835, 132.5337], [155.5265, 257.4937], [390.7135, 157.1463], [354.6765, 204.48], [278.81, 162.8263], [314.8465, 198.8], [411.5765, 219.6263], [269.3265, 136.32], [261.74, 107.92], [278.81, 104.1337], [305.3635, 107.92], [517.79, 335.12], [312.95, 155.2537], [366.0565, 100.3463], [204.84, 132.5337], [493.1335, 304.8263], [350.8835, 181.76], [377.4365, 115.4937], [312.95, 193.12], [218.1165, 134.4263], [303.4665, 285.8937], [385.0235, 253.7063], [235.1865, 94.6663], [244.67, 96.56], [367.9535, 142], [326.2265, 85.2], [280.7065, 229.0937], [278.81, 189.3337]]
ppdatalists = [[51.21, 304.8263], [466.58, 223.4137], [284.5, 181.76], [436.2335, 372.9863], [273.12, 157.1463], [130.87, 234.7737], [449.51, 159.04], [111.9035, 221.52], [166.9065, 198.8], [132.7665, 210.16], [233.29, 88.9863], [352.78, 198.8], [426.75, 230.9863], [221.91, 96.56], [233.29, 85.2], [402.0935, 193.12], [301.57, 174.1863], [85.35, 282.1063], [244.67, 176.08], [362.2635, 255.6], [312.95, 90.88], [229.4965, 107.92], [403.99, 206.3737], [210.53, 126.8537], [335.71, 90.88], [445.7165, 191.2263], [290.19, 348.3737], [409.68, 229.0937], [130.87, 210.16], [138.4565, 200.6937], [144.1465, 191.2263], [320.5365, 100.3463], [438.13, 227.2], [495.03, 219.6263], [250.36, 119.28], [358.47, 164.72], [87.2465, 265.0663], [269.3265, 107.92], [130.87, 221.52], [127.0765, 308.6137], [468.4765, 234.7737], [108.11, 221.52], [240.8765, 107.92], [375.54, 181.76], [398.3, 183.6537], [170.7, 193.12], [151.7335, 189.3337], [532.9635, 255.6], [453.3035, 177.9737], [413.4735, 266.96], [305.3635, 179.8663], [271.2235, 177.9737], [312.95, 107.92], [157.4235, 189.3337], [168.8035, 151.4663], [301.57, 176.08], [199.15, 172.2937], [348.9865, 107.92], [223.8065, 87.0937], [381.23, 215.84], [265.5335, 94.6663], [220.0135, 160.9337], [244.67, 164.72], [83.4535, 244.24], [125.18, 193.12], [153.63, 121.1737], [335.71, 87.0937], [263.6365, 134.4263], [295.88, 115.4937], [282.6035, 107.92], [335.71, 96.56]]
df = pd.DataFrame(evdatalists, columns=["LEFT_PIXELS", "TOP_PIXELS"])
dfpp = pd.DataFrame(ppdatalists, columns=["LEFT_PIXELS", "TOP_PIXELS"])

# Create the rink images and axes
IMG_DPI = 72
IMG_WIDTH = 569
IMG_HEIGHT = 568
fig = plt.figure(figsize=(IMG_WIDTH / IMG_DPI, IMG_HEIGHT / IMG_DPI), dpi=IMG_DPI)
ax = fig.add_subplot(121, frameon=False, xticks=[], yticks=[])
ax_extent = [0, 569, 568, 0]
ax1 = fig.add_subplot(122, frameon=False, xticks=[], yticks=[])
ax1_extent = [0, 569, 568, 0]
img = Image.open('half-rink.png')
ax.imshow(img, extent=ax_extent)
ax1.imshow(img, extent=ax1_extent)

# Plot all the individual shots
ax.scatter(df.LEFT_PIXELS, df.TOP_PIXELS, color="blue", s=14)
ax1.scatter(dfpp.LEFT_PIXELS, dfpp.TOP_PIXELS, color="red", s=14)

# Draw the density maps
sns.set_style("white")
sns.kdeplot(df.LEFT_PIXELS, df.TOP_PIXELS, ax=ax, cmap='Blues', shade=True, shade_lowest=True, alpha=.6, n_levels=4)
sns.kdeplot(dfpp.LEFT_PIXELS, dfpp.TOP_PIXELS, ax=ax1, cmap='Reds', shade=True, shade_lowest=True, alpha=.6, n_levels=4)

# Hide the axes
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax1.axes.get_xaxis().set_visible(False)
ax1.axes.get_yaxis().set_visible(False)

# Display the final image
plt.show()

Tags: the图像importfalsedfimgtopax

热门问题