0

I have a code which plots a football pitch:

import matplotlib.pyplot as plt import random import pandas as pd df = pd.read_csv(r'C:\Users\ADMIN\Desktop\Abhishek\kanemc.csv') #print(df) def draw_pitch(pitch, line, orientation,view): orientation = orientation view = view line = line pitch = pitch if orientation.lower().startswith("h"): if view.lower().startswith("h"): fig,ax = plt.subplots(figsize=(6.8,10.4)) plt.xlim(49,105) plt.ylim(-1,69) else: fig,ax = plt.subplots(figsize=(10.4,6.8)) plt.xlim(-1,105) plt.ylim(-1,69) ax.axis('off') # this hides the x and y ticks plt.scatter(df['x'],df['y']) # side and goal lines # ly1 = [0,0,68,68,0] lx1 = [0,104,104,0,0] plt.plot(lx1,ly1,color=line,zorder=5) # boxes, 6 yard box and goals #outer boxes# ly2 = [13.84,13.84,54.16,54.16] lx2 = [104,87.5,87.5,104] plt.plot(lx2,ly2,color=line,zorder=5) ly3 = [13.84,13.84,54.16,54.16] lx3 = [0,16.5,16.5,0] plt.plot(lx3,ly3,color=line,zorder=5) #goals# ly4 = [30.34,30.34,37.66,37.66] lx4 = [104,104.2,104.2,104] plt.plot(lx4,ly4,color=line,zorder=5) ly5 = [30.34,30.34,37.66,37.66] lx5 = [0,-0.2,-0.2,0] plt.plot(lx5,ly5,color=line,zorder=5) #6 yard boxes# ly6 = [24.84,24.84,43.16,43.16] lx6 = [104,99.5,99.5,104] plt.plot(lx6,ly6,color=line,zorder=5) ly7 = [24.84,24.84,43.16,43.16] lx7 = [0,4.5,4.5,0] plt.plot(lx7,ly7,color=line,zorder=5) #Halfway line, penalty spots, and kickoff spot ly8 = [0,68] lx8 = [52,52] plt.plot(lx8,ly8,color=line,zorder=5) plt.scatter(93,34,color=line,zorder=5) plt.scatter(11,34,color=line,zorder=5) plt.scatter(52,34,color=line,zorder=5) circle1 = plt.Circle((93.5,34), 9.15,ls='solid',lw=1.5,color=line, fill=False, zorder=1,alpha=1) circle2 = plt.Circle((10.5,34), 9.15,ls='solid',lw=1.5,color=line, fill=False, zorder=1,alpha=1) circle3 = plt.Circle((52, 34), 9.15,ls='solid',lw=1.5,color=line, fill=False, zorder=2,alpha=1) ## Rectangles in boxes rec1 = plt.Rectangle((87.5,20), 16,30,ls='-',color=pitch, zorder=1,alpha=1) rec2 = plt.Rectangle((0, 20), 16.5,30,ls='-',color=pitch, zorder=1,alpha=1) ## Pitch rectangle rec3 = plt.Rectangle((-1, -1), 106,70,ls='-',color=pitch, zorder=1,alpha=1) ax.add_artist(rec3) ax.add_artist(circle1) ax.add_artist(circle2) ax.add_artist(rec1) ax.add_artist(rec2) ax.add_artist(circle3) plt.scatter(df.x,df.y) else: if view.lower().startswith("h"): fig,ax = plt.subplots(figsize=(10.4,6.8)) plt.ylim(49,105) plt.xlim(-1,69) else: fig,ax = plt.subplots(figsize=(6.8,10.4)) plt.ylim(-1,105) plt.xlim(-1,69) ax.axis('off') # this hides the x and y ticks # side and goal lines # lx1 = [0,0,68,68,0] ly1 = [0,104,104,0,0] plt.plot(lx1,ly1,color=line,zorder=5) # boxes, 6 yard box and goals #outer boxes# lx2 = [13.84,13.84,54.16,54.16] ly2 = [104,87.5,87.5,104] plt.plot(lx2,ly2,color=line,zorder=5) lx3 = [13.84,13.84,54.16,54.16] ly3 = [0,16.5,16.5,0] plt.plot(lx3,ly3,color=line,zorder=5) #goals# lx4 = [30.34,30.34,37.66,37.66] ly4 = [104,104.2,104.2,104] plt.plot(lx4,ly4,color=line,zorder=5) lx5 = [30.34,30.34,37.66,37.66] ly5 = [0,-0.2,-0.2,0] plt.plot(lx5,ly5,color=line,zorder=5) #6 yard boxes# lx6 = [24.84,24.84,43.16,43.16] ly6 = [104,99.5,99.5,104] plt.plot(lx6,ly6,color=line,zorder=5) lx7 = [24.84,24.84,43.16,43.16] ly7 = [0,4.5,4.5,0] plt.plot(lx7,ly7,color=line,zorder=5) #Halfway line, penalty spots, and kickoff spot lx8 = [0,68] ly8 = [52,52] plt.plot(lx8,ly8,color=line,zorder=5) plt.scatter(34,93,color=line,zorder=5) plt.scatter(34,11,color=line,zorder=5) plt.scatter(34,52,color=line,zorder=5) circle1 = plt.Circle((34,93.5), 9.15,ls='solid',lw=1.5,color=line, fill=False, zorder=1,alpha=1) circle2 = plt.Circle((34,10.5), 9.15,ls='solid',lw=1.5,color=line, fill=False, zorder=1,alpha=1) circle3 = plt.Circle((34,52), 9.15,ls='solid',lw=1.5,color=line, fill=False, zorder=2,alpha=1) ## Rectangles in boxes rec1 = plt.Rectangle((20, 87.5), 30,16.5,ls='-',color=pitch, zorder=1,alpha=1) rec2 = plt.Rectangle((20, 0), 30,16.5,ls='-',color=pitch, zorder=1,alpha=1) ## Pitch rectangle rec3 = plt.Rectangle((-1, -1), 70,106,ls='-',color=pitch, zorder=1,alpha=1) ax.add_artist(rec3) ax.add_artist(circle1) ax.add_artist(circle2) ax.add_artist(rec1) ax.add_artist(rec2) ax.add_artist(circle3) draw_pitch("#195905","#faf0e6","h","full") plt.show() 

I have read in a csv file in the beginning and here's the sample data:

 x y Outcome Assist Type Play State 0 5.2 29.0 save cross header open NaN 1 13.7 14.4 right miss open NaN NaN 2 9.9 26.8 miss cross header open NaN 3 10.2 22.8 block left pass open NaN 4 8.9 21.8 set-piece cross header miss NaN 5 13.6 29.9 miss pass right open NaN 6 17.1 29.7 block self right set-piece NaN 

At the end of the code, I tried a simple plt.scatter(df['x'],df['y']) expecting to plot the x, y co-ordinates on the pitch. However, I can see nothing of that sort. I tried entering the line within the function as well but that didn't work out either. The code runs without errors and the pitch is plotted but not the data from the dataframe. What mistake am I making?

1 Answer 1

2

This is because the plotted items have been given a zorder, i.e are viewed layered on top of each other with most of them plotted at zorder=5. See for example the difference between:

 plt.scatter(55,39,color="b",zorder=5) ## can be seen plt.scatter(56,39,color="b") ## is per default 2, i.e. "below" the football pitch 

So simply give your scatter plot a zorder above 5 and everything should work.

Sign up to request clarification or add additional context in comments.

4 Comments

Cheers, I knew I was missing something obvious. Any idea why plt.plot() as well as df.plot.scatter() worked without any issues but only plt.scatter() needed azorder argument?
Thats because the default zorder for a line plot is 2 (see here, for example), which is above the pitch layer (that's at zorder=1, coinciding with the scatter default). The reason I suggested to keep everything above 5 was to be on the safe side, well above everything else. As a general idea, I would suggest that once you start using zorder, you should consistently use it for all plot elements (plot, scatter, text), especially when it comes to saving as an image (as not all backends pick sensible defaults, e.g. in PDFs)
Tausend dank @Asmus
This was the reason my scatter wasn't showing as well. I assumed that by simply putting my ax.scatter on the line after my ax.add_patch that it would plot "on top". I think it's very strange that that is not the default (i.e. each successive plot given a higher zorder). I believe that's how ggplot works in R (each plot you add being placed "on top").

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.