将文本注释到轴并对齐为圆形 [英] annotate text to axes and align as a circle

查看:67
本文介绍了将文本注释到轴并对齐为圆形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在轴上绘制文本并将此文本对齐到一个圆上.

更精确地说,位于该圆内的点具有不同的余弦(x,y),并通过以下方式创建:

  ax.scatter(x,y,s = 100)

我想用圆连接并标记每个点(Cnameb).文本的坐标用(xp,yp)定义.

因此点和圆之间的箭头长度不同,但中心和圆的整体距离如下图所示-->(注:蓝线是应该打印的线在点和圆之间.红线仅用于说明.):

因为我已经定义了半径并且我知道点的坐标以及中心点的坐标,所以我应该能够执行以下步骤:

  1. 计算单点到中心的距离DxMP
  2. 计算单点与圆之间的距离
  3. 计算角度alpha
  4. 计算点 xp 和 yp 应该是文本的坐标(圆上的坐标)

因此,我使用了以下代码:

  def legpoints(x,y):DxMP = np.sqrt((((x-521953)** 2)+(y-435179)** 2)#521953,435179是圆心的x和y坐标DxCirc = np.sqrt((900000-DxMP)** 2)#点x与圆之间的距离alpha = np.arccos((np.sqrt((x-521953)** 2))/(DxMP))xp = x +(np.cos(alpha)* DxCirc)yp = y + (np.sin(alpha) * DxCirc)返回xp,ypxp=legpoints(x,y)[0]yp = legpoints(x,y)[1]

压缩后的数据的形状为(Cnameb,x,y,xp,yp):

[('Berlin',735951.59991561132,617707.36153527966,1206703.3293253453,1019231.2121256208),(''Berlin',735965.58122088562,617712.48195467936,1206714.0793803122,1019218.6083879157),('Bremen',425896.1266258678675,330448.62508515653,502638.58154814231,987816.52265995357,734203.8568234311),( '杜伊斯堡',281456.9370223835,495636.46544709487,913803.62749559013,654599.89177739131),( '杜塞尔多夫',283849.70917473407,471649.47447504522,935371.04632360162,571443.52693890885),( 'Essen的鲁尔',298590.41880710673,497973.49884993531,941640.19382135477,678755.74152428762),('美因河畔法兰克福',412037.5979210182,345052.92773266998,998077.35579369171,825581.23014117288),('汉堡',505147.96843631176,726635.42284052074,540149.82358692121,1333686.6774791574科隆, 292146.52126941859, 439340.70884408138, 962192.49751825235,451474.98930779565),('München',623290.92919537693,125422.12264187855,801795.74103644479,671052.90026201855),('斯图加特',445743.44744934322,196109.08652145317,642879.16415181267,814525

使用以下代码,我想将文本添加到轴并对齐为一个圆圈:

  [ax.annotate(s = nme,xy =(x,y),xytext =(xpoint,ypoint),textcoords ="data",arrowprops = {"arrowstyle":->"},color ="black",alpha = 0.8,fontsize = 12)表示zip(Cnameb,x,y,xp,yp)中的nme,x,y,xpoint,ypoint]

但结果并不理想,因为文本没有对齐为圆形而是未定义....

有人可以帮我吗...?

解决方案

我看不出距离在等式中会发生什么.您要确定注释的定位点,并将此点提供给注释功能.

将 numpy 导入为 np导入matplotlib.pyplot作为plt文本=[德累斯顿",柏林",弗莱堡"]xy = np.array([[3.5,1],[3.,2.],[0,-2]])center = np.array([1.,1.])半径= 5#半径x,y = zip(* xy)cx,cy =中心plt.scatter(x,y)plt.scatter(cx, cy)#绘制圆ct = np.linspace(0,2 * np.pi)circx, circy = 半径*np.cos(ct)+cx, 半径*np.sin(ct)+cyplt.plot(circx, circy, ls=":")def ann(x,y,cx,cy,r,text):角度 = np.arctan2(y-cy, x-cx)xt,yt = r * np.cos(angle)+ cx,r * np.sin(angle)+ cyplt.annotate(text, xy=(x,y), xytext=(xt, yt), arrowprops={"arrowstyle":"->"})plt.scatter(xt, yt, c=0)对于 zip(texts, x,y) 中的 t, xi,yi:ann(xi,yi,cx,cy,radius,t)plt.gca().set_aspect("相等")plt.show()

I am trying to plot text on axes and align this text to a circle.

More precisely there are points with different coordiantes (x,y) which are located inside this circle and created with:

ax.scatter(x,y,s=100)

I want to connect and label each point (Cnameb) with the circle. The coordinates of the text are defined with (xp,yp).

Thus the arrows between the points and the circle are different in length but the overall distance between the center and the circle is the same as the following figure shows --> (Note: the blue lines are the lines which should be printed between the points and the circle. The red lines are just for illustration.):

Because I have defined the radius and I know the coordinates of the points as well as the coordinates of the center point, I should be able to do the following steps:

  1. Calculate the distance DxMP between the single points and the center
  2. Calculate the distance between the the single points and the circle
  3. Calculate the angle alpha
  4. Calculate the points xp and yp which should be the coordinates of the text (the coordinates on the circle)

Wherefore I used the following code:

def legpoints(x,y):
    DxMP = np.sqrt(((x - 521953) ** 2) + (y - 435179) ** 2)#521953, 435179 are the x and y coordinates of the center of the circle
    DxCirc = np.sqrt((900000 - DxMP)**2)#The distance between the point x and the circle
    alpha = np.arccos((np.sqrt((x - 521953)**2)) / (DxMP))
    xp = x + (np.cos(alpha) * DxCirc) 
    yp = y + (np.sin(alpha) * DxCirc)
    return xp,yp


xp=legpoints(x,y)[0]
yp=legpoints(x,y)[1]

The zipped data have the shape (Cnameb,x,y,xp,yp):

[('Berlin', 735951.59991561132, 617707.36153527966, 1206703.3293253453, 1019231.2121256208), ('Berlin', 735965.58122088562, 617712.48195467936, 1206714.0793803122, 1019218.6083879157), ('Bremen', 425896.14258295257, 673875.68843362806, 665833.6191604546, 1270108.8219153266), ('Dortmund', 330448.62508515653, 502638.58154814231, 987816.52265995357, 734203.8568234311), ('Duisburg', 281456.9370223835, 495636.46544709487, 913803.62749559013, 654599.89177739131), ('Düsseldorf', 283849.70917473407, 471649.47447504522, 935371.04632360162, 571443.52693890885), ('Essen, Ruhr', 298590.41880710673, 497973.49884993531, 941640.19382135477, 678755.74152428762), ('Frankfurt am Main', 412037.5979210182, 345052.92773266998, 998077.35579369171, 825581.23014117288), ('Hamburg', 505147.96843631176, 726635.42284052074, 540149.82358692121, 1333686.6774791477), ('Hannover', 487540.73893698538, 594957.33199132804, 642620.87620513374, 1315004.3411755674), ('Köln', 292146.52126941859, 439340.70884408138, 962192.49751825235, 451474.98930779565), ('München', 623290.92919537693, 125422.12264187855, 801795.74103644479, 671052.90026201855), ('Stuttgart', 445743.44744934322, 196109.08652145317, 642879.16415181267, 814525.24510293454)]

With the following code I want to add the text to the axes and allign as a circle:

[ax.annotate(s=nme,xy=(x,y),xytext=(xpoint,ypoint),textcoords="data",arrowprops={"arrowstyle":"->"},color="black",alpha=0.8,fontsize=12) for nme,x,y,xpoint,ypoint in zip(Cnameb,x,y,xp,yp)]

But the result is not as desired because the text is not alligned as circle but undefined....

Can anybody help me please...?

解决方案

I don't see what the distance would do in the equation. You want to determine the point at which to locate the annotation and give this point to the annotation function.

import numpy as np
import matplotlib.pyplot as plt

texts=["Dresden","Berlin", "Freiburg"]
xy = np.array([[3.5,1],[3.,2.],[0,-2]])

center = np.array([1.,1.])
radius = 5 # radius

x,y = zip(*xy)
cx, cy = center
plt.scatter(x,y)
plt.scatter(cx, cy)

#plot a cirle
ct = np.linspace(0,2*np.pi)
circx, circy = radius*np.cos(ct)+cx, radius*np.sin(ct)+cy
plt.plot(circx, circy, ls=":")



def ann(x,y, cx, cy, r, text):
    angle = np.arctan2(y-cy, x-cx)
    xt, yt = r*np.cos(angle)+cx, r*np.sin(angle)+cy
    plt.annotate(text, xy=(x,y), xytext=(xt, yt), arrowprops={"arrowstyle":"->"})
    plt.scatter(xt, yt, c=0)

for t, xi,yi in zip(texts, x,y):
    ann(xi,yi, cx, cy, radius, t)

plt.gca().set_aspect("equal")
plt.show()

这篇关于将文本注释到轴并对齐为圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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