Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Post a Comment for "Placing Text Around The Circle"