Anmol Durgapal
Anmol Durgapal

Reputation: 111

Placing text around the circle

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

Upvotes: 2

Views: 975

Answers (1)

Parth Shah
Parth Shah

Reputation: 1475

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.

Upvotes: 3

Related Questions