2

Using pyplot circle function I made a circle, then I have used text function to place the text(parameters) across the circle(PLOT ATTACHED) but the thing is if let's say I want to list out only 6 or say 11 parameters equally spaced across the circle I'll have to chage the coordinates as well as the rotation in text(and the coordinates and rotation value has been manually set). I want something that'll automate these things like given a number of parameter it will place parameter with equal spacing between them around the circle

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

fig, ax = plt.subplots(figsize=(30, 20))
ax.axis('equal')

ax.set(xlim=(-10, 23), ylim = (-10, 10))

circle = plt.Circle((0, 0), 4.7, fc='#cfe2f3')
ax.add_patch(circle)

ax.text(-0.4, 4.9, 'npxG', fontsize=15)
ax.text(3.35, 3.5, 'xA', rotation=310, fontsize=15)
ax.text(4.8, -0.5, 'Shots on Target', rotation=270, fontsize=15)
ax.text(3.35, -3.55, 'Dribbles', rotation=50, fontsize=15)
ax.text(-1, -5., 'Through Balls', fontsize=15)
ax.text(-4.6, -3.6, 'Passes 1/3', rotation=305, fontsize=15)
ax.text(-5, -0.5, 'Key Passes', rotation=90, fontsize=15)
ax.text(-4., 3.3, 'Crosses', rotation=42, fontsize=15)

ax.axis('off')

enter image description here

Edit:

for i in range(0, len(data)):
    a = points[i,2] 
    x,y = (radius*np.sin(a), radius*np.cos(a))
    a = a - 0.5*np.pi
    if points[i,1] < 0:
      a = a - np.pi 
    ax.text(x, y, data[i], rotation = np.rad2deg(a), ha="center", va="center", fontsize=15)

On changing order of the array: enter image description here

On flipping the x and y values: enter image description here

1 Answers1

3

Using code and inspiration from this question and answer and a bit of coordinate geometry:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(figsize=(30, 20))
ax.axis('equal')

ax.set(xlim=(-10, 10), ylim=(-10, 10))

circle = plt.Circle((0, 0), 2.7, fc='#cfe2f3')
ax.add_patch(circle)

def kex(N):
    alpha=2*np.pi/N
    alphas = alpha*np.arange(N)
    coordX = np.cos(alphas)
    coordY = np.sin(alphas)

    return np.c_[coordX, coordY, alphas]


data = ["npxG", "xA", "Shots on Target", "Dribbles", "Through Balls", 
        "Passes 1/3", "Key Passes", "Crosses"]
radius = 3.2
points = kex(len(data))

for i in range(0, len(data)):
    a = points[i,2] 
    x,y = (radius*np.cos(a), radius*np.sin(a))
    if points[i,0] < 0:
      a = a - np.pi
    ax.text(x, y, data[i], ha="center", va="center", fontsize=15)

ax.axis("off")

plt.show()

Gives this:

first

If you wish to adapt something like the linked answer and rotate the labels as a perpendicular to the circle, change this line:

ax.text(x, y, data[i], rotation = np.rad2deg(a), ha="center", va="center", fontsize=15)

Note the added roatation parameter. This gives:

second

To adapt something like the sample image in the question:

for i in range(0, len(data)):
    a = points[i,2] 
    x,y = (radius*np.cos(a), radius*np.sin(a))
    a = a - 0.5*np.pi
    if points[i,1] < 0:
      a = a - np.pi
    ax.text(x, y, data[i], rotation = np.rad2deg(a), ha="center", va="center", fontsize=15)

This gives:

third

The list data can be populated with label text. On changing the number of labels, the plot should adapt accordingly. The parameter radius adjusts the distance of the text from the center of the circle. You can add in extra parameters to the .text() function such as fontsize as required for the labels.

Note: View this answer on the SO white theme to see the labels clearly. I took the liberty to change the plot size to fit it here. Huge thanks to @ImportanceOfBeingErnest for the linked question's answer.

Parth Shah
  • 1,237
  • 10
  • 24
  • Hey, I now have flipped the x and y values so that the lables are shown by their order in the given data-list, like npxG will come at top then xA so on so forth. But as i am flipping the x and y values the rotations does not seems to work, can you tell me the intuition behind the rotation part? I have added the code can you please check it out. – Anmol Durgapal Jul 31 '20 at 08:33
  • Have you find something?? – Anmol Durgapal Aug 01 '20 at 07:51
  • I do not understand what you mean by flipping x and y values. Please ask a new question with a [mcve] – Parth Shah Aug 01 '20 at 09:45
  • 1
    What I did was assigned the radius\*sin(a) value to x and radius\*cos(a) value to y, so that the parameters align in such a way that at the top of the circle the first paramter is placed(which in my case is npxG). The code that you wrote was placing npxG at the right not at the top. You can see the plot as well as the code(attached in the question after the Edit). I hope you get it. – Anmol Durgapal Aug 01 '20 at 15:02
  • 1
    Why would you do that? Instead just change the order of the array. – Parth Shah Aug 01 '20 at 15:44
  • Yes, I tried changing the order of the array and yes it was giving me the result. But by flipping the axis I am getting the desired result as you can see in the question(updated), like you can see how npxG is now at top of the circle but only problem is the rotation part. I hope you get it. – Anmol Durgapal Aug 01 '20 at 16:27
  • Have you got something? – Anmol Durgapal Aug 02 '20 at 07:44