I have been recording the xy cordinates of the ball bounce location on a tennis serve. I am just using this data to play around with different visualisations, and keen to try a heatmap using seaborn (I am going to use this visual within power bi so I am ‘limited’ to the libraries I can use’)
The image i am using for the tennis court is:
The coordinate system is a little different from convention:
I have set the cooridnates as 0 – 100 on both axis.i.e. the whole tennis court image is broken up into 100 x 100 blocks.
An example of the data generated would be:
data['1stServeX'] = np.random.uniform(0, 100, 200)
data['1stServeY'] = np.random.uniform(0, 100, 200)
The problem I am having is with the code I am using the heatmap overlay is too big for the image / or the image is too small for the heatmap….
This is the code that I have used:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Arc
import seaborn as sns
import matplotlib.image as mpimg
# Load the data
data = pd.read_csv(r"C:UsersmarckDesktopTest_python_heatmapPython_test_data_edit.csv")
# Load the image
img = mpimg.imread(r"C:UsersmarckDesktopTest_python_heatmapPython_test.jpg")
# Create a plot
fig, ax = plt.subplots()
fig.set_size_inches(7, 5)
# Display the image as the background
ax.imshow(img, extent=[data['1stServeX'].min(), data['1stServeX'].max(), data['1stServeY'].min(),
data['1stServeY'].max()], zorder=-1)
# Plot the KDE on top of the image
sns.kdeplot(x=data['1stServeX'], y=data['1stServeY'], zorder=1, ax=ax)
plt.show()
I am a beginner to python and have no idea of how to make this work.