在matplotlib中对齐圆圈中的标签 [英] Align labels in a circle in matplotlib

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

问题描述

我在pyplot中的文本对齐方面遇到困难.我正在尝试注释以圆形树状图以圆形方式排列的点,因此,使标签指向远离点并保持直角的角度非常重要.这是我到目前为止的相关部分.

I have difficulties with text alignment in pyplot. I am trying to annotate points arranged in a circular fashion in a circular dendrogram, so it is important that the labels are pointing away from the dots and keeping the right angle. Here is the relevant part of what I have so far.

水平标签的作用就像一个魅力,但垂直标签显然是关闭的.似乎 horizo​​ntalalignment/verticalalignment 应用于原始坐标和边界框.是否有任何选项/方法可以正确对齐标签,而无需执行一些疯狂的特技,例如弄清楚文本的高度并相应地移动标签.我想知道用极坐标覆盖第二个图/轴并在上面放上文字是否有意义,但是我不确定这是否会带我到任何地方.还是我错过了一些非常明显的东西...

The horizontal labels work like a charm, but the vertical ones are obviously off. It seems that horizontalalignment / verticalalignment is applied on the original coordinates and on the bounding box. Is there any option / way to correctly align the labels without performing some crazy stunts like figuring out the text hight and moving the labels accordingly. I was wondering if it would make sense to overlay a second plot / axis with polar coordinates and put the text on it, but I am not sure if this will lead me anywhere. Or wether I am missing something really obvious...

这是一个最小的工作示例:

Here is a minimal working example:

import matplotlib.pyplot as plt

(fig, ax) = plt.subplots(figsize = (4,4))

def kex(N):
    alpha = 360. / N
    coordX = []
    coordY = []
    alphas = []
    for i in range(0,N):
        alpha_loop = alpha * i
        coordX.append( math.cos(math.radians(alpha_loop)) )
        coordY.append( math.sin(math.radians(alpha * i)) )
        alphas.append(alpha_loop)
    return [coordX, coordY, alphas]

N = 10
points = kex(N)
ax.scatter(points[0], points[1])
for i in range(0,N):
    x = points[0][i]
    y = points[1][i]
    a = points[2][i]
    if x > 0:
        ax.text(x + x * 0.1, y + y * 0.1, "AAA", rotation = a,
                    bbox=dict(facecolor = "none", edgecolor ="red"))
    else:
        ax.text(x + x * 0.1, y + y * 0.1, "AAA", rotation = a - 180,
                    bbox=dict(facecolor = "none", edgecolor ="red"), ha = "right")

ax.axis("off")

plt.show()

感谢您的帮助!

推荐答案

您可以对文本进行足够的偏移,以免它与点重叠.然后,想法是使文本居中对齐( ha ="center",va ="center" ),这样文本将位于图形的中点和点之间的延伸(虚拟)线上它有注释.

You may offset the text enough not to have it overlap with the points. The idea is then to center-align (ha="center", va="center") the text, such that will be sitting on an extended (virtual) line between the graph's midpoint and the dot it annotates.

import matplotlib.pyplot as plt
import numpy as np

(fig, ax) = plt.subplots(figsize = (4,4))

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]

N = 10
r = 1.2
points = kex(N)
ax.scatter(points[:,0], points[:,1])

for i in range(0,N):
    a = points[i,2] 
    x,y = (r*np.cos(a), r*np.sin(a))
    if points[i,0] < 0: a = a - np.pi
    ax.text(x,y, "AAA", rotation = np.rad2deg(a), ha="center", va="center",
                    bbox=dict(facecolor = "none", edgecolor ="red"))

ax.axis("off")

plt.show()

这篇关于在matplotlib中对齐圆圈中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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