代码之家  ›  专栏  ›  技术社区  ›  Nicholas

matplotlib-将文本标签向右移动x点

  •  1
  • Nicholas  · 技术社区  · 6 年前

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

    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

    我将文本标签垂直和水平居中(例如1304469等),但理想情况下我希望它向右移动,这样它就远离了气泡。我试过了 ha=right 但它只会轻轻地推动它。

    我能用什么把它从气泡中完全移开吗?即代码,我可以把它放在下面 for loop

    for i, txt in enumerate(labels):
        ax.annotate(txt, (x[i], y[i]), ha='center', va='center', )
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   Sheldore    6 年前

    我只需要使用偏移百分比(例如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

        2
  •  1
  •   Juan C    6 年前

    参数 xytext 属于 ax.annotate 让我们这么做:

    Fig,ax=plt.子批次(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

    编辑:如果您希望标签刚好位于每个圆的右侧,则必须创建一个位置数组,然后循环通过它。

        3
  •  0
  •   ImportanceOfBeingErnest    6 年前

    因为尺寸 s 气泡的数量是 s=z*4000 ,气泡的半径是 np.sqrt(z*4000)/2 . (解释见 scatter plot marker size

    因此,您将创建一个注释,该注释位于数据坐标中气泡的中心,并将其偏移 np.sqrt(z*4000)/2 in units of points(或可能多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