为了账号安全,请及时绑定邮箱和手机立即绑定

如何在matplotlib中进行多列文本注释?

如何在matplotlib中进行多列文本注释?

料青山看我应如是 2023-09-05 20:29:37
matplotlib 的legend()方法有一个ncol参数可以将内容布置在多列中。我想在文本注释中做同样的事情。我可以将多行文本字符串(即包含\ns 的文本字符串)传递给annotate(),但如何将内容排列为两列?使用制表符?他们似乎什么也没做使用两个单独的文本注释?但我想要在两列周围有一个圆形边框(bbox)使用类似 ncol 的东西?它可以根据我要求的列数换行
查看完整描述

1 回答

?
aluckdog

TA贡献1847条经验 获得超7个赞

我找不到一个好的方法来做到这一点,所以我编写了一个函数来完成工作。尝试一下,看看它是否满足您的需要。


def place_column_text(ax, text, xy, wrap_n, shift, bbox=False, **kwargs):

    """ Creates a text annotation with the text in columns.

    The text columns are provided by a list of strings.

    A surrounding box can be added via bbox=True parameter.

    If so, FancyBboxPatch kwargs can be specified.

    

    The width of the column can be specified by wrap_n,

    the shift parameter determines how far apart the columns are.

    The axes are specified by the ax parameter.


    Requires:

    import textwrap

    import matplotlib.patches as mpatches

    """

    # place the individual text boxes, with a bbox to extract details from later

    x,y = xy

    n = 0

    text_boxes = []

    for i in text:

        text = textwrap.fill(i, wrap_n)

        box = ax.text(x = x + n, y = y, s=text, va='top', ha='left',

                         bbox=dict(alpha=0, boxstyle='square,pad=0'))

        text_boxes.append(box)

        n += shift

    

    if bbox == True: # draw surrounding box

        # extract box data

        plt.draw() # so we can extract real bbox data

        # first let's calulate the height of the largest bbox

        heights=[]

        for box in text_boxes:

            heights.append(box.get_bbox_patch().get_extents().transformed(ax.transData.inverted()).bounds[3])

        max_height=max(heights)

        # then calculate the furthest x value of the last bbox

        end_x = text_boxes[-1].get_window_extent().transformed(ax.transData.inverted()).xmax

        # draw final

        width = end_x - x

        fancypatch_y = y - max_height

        rect = mpatches.FancyBboxPatch(xy=(x,fancypatch_y), width=width, height=max_height, **kwargs)

        ax.add_patch(rect)

这是它的使用情况:


import matplotlib.patches as mpatches

import textwrap


fig, ax = plt.subplots(2,2,sharex=True, sharey=True,figsize=(16,12))

fig.subplots_adjust(hspace=0.05, wspace=0.05)

ax1, ax2, ax3, ax4 = ax.flatten()


for a in ax.flatten():

    a.plot(range(0,20),range(0,20), alpha=0)


# the text to be split into columns and annotated

col_text = ['Colum 1 text is this sentence here.',

            'The text for column two is going to be longer',

            'Column 3 is the third column.',

            'And last but not least we have column 4. In fact this text is the longest.']


# use the function to place the text

place_column_text(ax=ax1,text=col_text, xy=(1,10), wrap_n=10, shift=4.2)

place_column_text(ax=ax2,text=col_text, xy=(0,19), wrap_n=17, bbox=True, shift=5, ec='red', fc='w', boxstyle='square')

place_column_text(ax=ax3,text=col_text, xy=(2,18), wrap_n=6, bbox=True, shift=2.7, ec='blue', fc = 'blue' , alpha=0.3, boxstyle='round,pad=1')

place_column_text(ax=ax4,text=col_text, xy=(3,12), wrap_n=10, bbox=True, shift=3, ec='red', fc='w', boxstyle='circle, pad=3')


plt.show()

结果:


https://img1.sycdn.imooc.com//64f71f6500016b1809780722.jpg

查看完整回答
反对 回复 2023-09-05
  • 1 回答
  • 0 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信