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

后端 未结 4 1198
攒了一身酷
攒了一身酷 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:34

    What exactly did you try?

    Were you, by chance, using a raw string (e.g. r"whatever")?

    '\n' works perfectly, but if you're using a raw string to avoid latex sequences being interpreted as an escape, it will be interpreted by python as '\' and 'n' instead of a newline.

    As an example:

    import matplotlib.pyplot as plt
    
    plt.annotate('Testing\nThis\nOut', xy=(0.5, 0.5))
    
    plt.show()
    

    enter image description here

    On the other hand, if we use a raw string:

    import matplotlib.pyplot as plt
    
    plt.annotate(r'Testing\nThis\nOut', xy=(0.5, 0.5))
    
    plt.show()
    

    enter image description here

提交回复
热议问题