python注释换行_Python:Matplotlib注释换行符(带和不带胶乳)

"这篇博客解答了如何在Python的Matplotlib库中使用`annotate`命令进行文本注解时实现换行的问题。作者指出,使用` `作为换行符通常有效,但如果在原始字符串(如`r"string"`)中使用,则会被解释为单独的字符。正确的做法是在非原始字符串中使用` `来插入新行。示例代码展示了如何为正常文本和LaTeX注解添加换行。"
摘要由CSDN通过智能技术生成

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 this for a "Latex" annotation and for a normal text annotation ?

Thank you very much.

解决方案

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()

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值