简体   繁体   English

Matplotlib - 将文本标签向右移动“x”点

[英]Matplotlib - Move text label right by 'x' points

I have the following code which produces a bubble chart, and then adds the labels as text to the plot:我有以下代码可以生成气泡图,然后将标签作为文本添加到图中:

fig, ax = plt.subplots(figsize = (5,10))

# create data
x = [1,1,1,1,1,1,1,1,1,1]
y = ['A','B','C','D',
     'E','F','G','H','I','']
z = [10,20,80,210,390,1050,2180,4690,13040,0]

labels = [1,2,8,21,39,105,218,469,1304]

plt.xlim(0.9,1.1)

for i, txt in enumerate(labels):
    ax.annotate(txt, (x[i], y[i]), ha='center', va='center', )

plt.scatter(x, y, s=z*4000, c="#8C4799", alpha=0.3)

在此处输入图片说明

I have the text labels centered vertically and horizontally (ie the 1304,469 etc), but ideally I want it shifted to the right so it is away from the bubble.我有垂直和水平居中的文本标签(即 1304,469 等),但理想情况下我希望它向右移动,以便远离气泡。 I have tried ha=right , but it only nudges it a tiny bit.我试过ha=right ,但它只是微调了一点。

Is there anything I can use to move it completely away from the bubble?有什么我可以用来将它完全远离气泡的东西吗? Ie code I can put it the following for loop :即代码我可以把它放在以下for loop

for i, txt in enumerate(labels):
    ax.annotate(txt, (x[i], y[i]), ha='center', va='center', )

Since the size s of the bubbles is s=z*4000 , a bubble's radius is np.sqrt(z*4000)/2 .由于气泡的大小ss=z*4000 ,因此气泡的半径为np.sqrt(z*4000)/2 (For an explanation see scatter plot marker size ). (有关解释,请参阅散点图标记大小)。

You would hence create an annotation which is positionned at the center of the bubbles in data coordinates and offset it by np.sqrt(z*4000)/2 in units of points (or possibly 2 or 3 points more to have it look nicely).因此,您将创建一个注释,该注释位于数据坐标中气泡的中心,并以点为单位将其偏移np.sqrt(z*4000)/2 (或可能多 2 或 3 个点以使其看起来很好) .

This would be done using这将使用

annotate("text", xy=(x[i],y[i]), 
         xytext=(np.sqrt(z[i]*4000)/2+2, 0),  textcoords="offset points")

Complete example:完整示例:

import matplotlib.pyplot as plt
import numpy as np


fig, ax = plt.subplots(figsize = (5,10))

# create data
x = [1,1,1,1,1,1,1,1,1,1]
y = ['A','B','C','D',
     'E','F','G','H','I','']
z = [10,20,80,210,390,1050,2180,4690,13040,0]

labels = [1,2,8,21,39,105,218,469,1304]

plt.xlim(0.9,1.1)


sc = plt.scatter(x, y, s=z*4000, c="#8C4799", alpha=0.3)

for txt, size, xi, yi in zip(labels, sc.get_sizes(), x,y):
    ax.annotate(txt, xy=(xi,yi), xytext=(np.sqrt(size)/2+2, 0),
                textcoords="offset points",
                ha='left', va='center', )

plt.show()

在此处输入图片说明

the parameter xytext of ax.annotate lets you do this:参数xytextax.annotate让你这样做:

fig, ax = plt.subplots(figsize = (5,10))

# create data
x = [1,1,1,1,1,1,1,1,1,1]
y = ['A','B','C','D',
     'E','F','G','H','I','']
z = [10,20,80,210,390,1050,2180,4690,13040,0]

labels = [1,2,8,21,39,105,218,469,1304]

plt.xlim(0.9,1.1)

for i, txt in enumerate(labels):
    ax.annotate(txt, (x[i], y[i]), ha='center', va='center', xytext=(1.05,y[i]) )

plt.scatter(x, y, s=z*4000, c="#8C4799", alpha=0.3)

Brings this:带来了这个:

在此处输入图片说明

Edit: if you want the labels to be just to the right of every circle, you'll have to create an array of positions and then loop through it编辑:如果您希望标签正好位于每个圆圈的右侧,则必须创建一个位置数组,然后循环遍历它

I would simply use an offset percentage (20% for example) to reposition the x-coordinate of the text.我会简单地使用偏移百分比(例如 20%)来重新定位文本的 x 坐标。 Additionally you can turn off the manual setting of x-limits.此外,您可以关闭 x 限制的手动设置。

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

x = [1,1,1,1,1,1,1,1,1,1]
y = ['A','B','C','D',
     'E','F','G','H','I','']
z = [10,20,80,210,390,1050,2180,4690,13040,0]

labels = [1,2,8,21,39,105,218,469,1304]

for i, txt in enumerate(labels):
    ax.annotate(txt, (x[i]*1.2, y[i]), ha='center', va='center', )

plt.scatter(x, y, s=z*4000, c="#8C4799", alpha=0.3) 

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM