在圆圈周围放置文本 [英] Placing text around the circle

查看:49
本文介绍了在圆圈周围放置文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用pyplot circle 函数制作了一个圆圈,然后我使用了 text 函数将文本(参数)放置在圆圈上(PLOT ATTACHED),但问题是如果假设我只想列出6个参数,或者说说11个参数,那么我必须对坐标以及 text 中的旋转进行修改(坐标和旋转值为手动设置).我想要可以使这些事情自动化的东西,例如给定多个参数,它将在圆圈之间以相等的间距放置参数

 将matplotlib.pyplot导入为plt从 matplotlib.patches 导入多边形图, ax = plt.subplots(figsize=(30, 20))ax.axis('等于')ax.set(xlim =(-10,23),ylim =(-10,10))circle = plt.Circle((0, 0), 4.7, fc='#cfe2f3')ax.add_patch(圆圈)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, '运球', 旋转=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')

 适用于范围(0,len(data))中的i:a = 点 [i,2]x,y = (半径*np.sin(a), 半径*np.cos(a))a = a-0.5 * np.pi如果点 [i,1] <0:a = a - np.piax.text(x,y,data [i],rotation = np.rad2deg(a),ha ="center",va ="center",fontsize = 15)

关于改变数组的顺序:

在翻转 x 和 y 值时:

解决方案

使用来自

如果您希望调整链接答案之类的内容并将标签旋转为垂直于圆圈,请更改此行:

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

注意添加的 roatation 参数.这给出:

调整问题中的示例图像之类的内容:

适用于范围(0,len(data))中的i的

 :a = 点 [i,2]x,y =(半径* np.cos(a),半径* np.sin(a))a = a - 0.5*np.pi如果点 [i,1] <0:a = a-np.piax.text(x,y,data [i],rotation = np.rad2deg(a),ha ="center",va ="center",fontsize = 15)

这给出:

列表 data 可以填充标签文本.在更改标签数量时,绘图应相应地进行调整.参数 radius 调整文本与圆心的距离.您可以根据标签的需要在 .text()函数中添加其他参数,例如 fontsize .

注意:在SO白色主题上查看此答案即可清楚地看到标签.我自由地更改了地块的大小,以适应此处的情况.非常感谢@ImportanceOfBeingErnest提供的链接问题的答案.

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')

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:

On flipping the x and y values:

解决方案

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:

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:

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:

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.

这篇关于在圆圈周围放置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆