Python : Matplotlib annotate line break (with and without latex)

后端 未结 4 1201
攒了一身酷
攒了一身酷 2020-12-17 08:58

I have a very basic question : how to do a line break with matplotlib in python with an \"annotate\" command. I tried \"\\\" and \"\\n\" but it does not work. And how to do

4条回答
  •  悲&欢浪女
    2020-12-17 09:54

    However, if you needed both, consider the following example:

    import matplotlib.pyplot as plt
    
    a = 1.23
    b = 4.56
    
    annotation_string = r"Need 1$^\mathsf{st}$ value here = %.2f" % (a) 
    annotation_string += "\n"
    annotation_string += r"Need 2$^\mathsf{nd}$ value here = %.2f" % (b)
    
    plt.annotate(annotation_string, xy=(0.5, 0.5))
    
    plt.show()
    

    Which gives you:

    enter image description here

    The key is to assemble the string beforehand, using +=. That way, you can have the raw-string commands (indicated by r) and the line-break (\n) in the same annotation.

提交回复
热议问题