Box around text in matplotlib

前端 未结 3 477
耶瑟儿~
耶瑟儿~ 2020-12-02 18:22

how is possible to make a box around text in matplotlib? I have text on three different lines and in three different colors:

 ax.text(2,1, \'alpha\', color=         


        
3条回答
  •  鱼传尺愫
    2020-12-02 18:50

    enter image description here There is some documentation online somewhere (the best I can find quickly is http://matplotlib.org/users/annotations_guide.html) for using VPacker and an AnnotationBbox to put together several texts of varying font properties.

    from matplotlib.offsetbox import TextArea, VPacker, AnnotationBbox
    from pylab import *
    fig = figure(1)
    ax = gca()
    texts = ['alpha','beta','epsilon']
    colors = ['red','cyan','black']
    Texts = []
    for t,c in zip(texts,colors):
        Texts.append(TextArea(t,textprops=dict(color=c)))
    texts_vbox = VPacker(children=Texts,pad=0,sep=0)
    ann = AnnotationBbox(texts_vbox,(.02,.5),xycoords=ax.transAxes,
                                box_alignment=(0,.5),bboxprops = 
                                dict(facecolor='wheat',boxstyle='round',color='black'))
    ann.set_figure(fig)
    fig.artists.append(ann)
    

    I'm not sure why both of the last two lines are needed. I would think the second to last would suffice.

提交回复
热议问题