Matplotlib - 将文本标签右移'x'个点

问题描述 投票:1回答:3

我有以下代码生成气泡图,然后将标签作为文本添加到绘图中:

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)

enter image description here

我有垂直和水平居中的文本标签(即1304,469等),但理想情况下我希望它向右移动,使其远离气泡。我曾尝试过ha=right,但它只是略微推动它。

有什么我可以用它来完全远离泡沫吗?即代码我可以把它下面的for loop

for i, txt in enumerate(labels):
    ax.annotate(txt, (x[i], y[i]), ha='center', va='center', )
python matplotlib
3个回答
1
投票

由于气泡的大小ss=z*4000,气泡的半径是np.sqrt(z*4000)/2。 (有关解释,请参阅scatter plot marker size)。

因此,您将创建一个注释,该注释位于数据坐标中气泡的中心,并以点为单位将其偏移np.sqrt(z*4000)/2(或者可能更多地使它看起来更好2或3个点)。

这将使用

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

完整的例子:

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

enter image description here


2
投票

我只是使用偏移百分比(例如20%)来重新定位文本的x坐标。此外,您可以关闭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) 

enter image description here


1
投票

xytext的参数ax.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)

带来这个:

enter image description here

编辑:如果你想让标签位于每个圆圈的右边,你必须创建一个位置数组然后循环它

© www.soinside.com 2019 - 2024. All rights reserved.