python - Matplotlib - 在我要显示的行中使用 $ 符号时如何添加多行文本框?

标签 python matplotlib

我需要在图形文本框中的两行中绘制 r 平方和幂律方程,但我不能使用 '$a=3$\n$b=2$,因为我的代码中已经有 $ 登录。因此,每当我尝试添加 '&\&' 时,它都不起作用。

'y='+str(round(m,3))+'x$^{'+str(round(j,3))+'}$'

r$^{2}$=0.95

我怎样才能在图中的一个框中用两行显示这些?

谢谢

最佳答案

如果 OP 想要这个:

enter image description here

代码如下:

#!/usr/bin/python

import matplotlib
import matplotlib.pyplot

matplotlib.rc('text', usetex=True) #use latex for text

# add amsmath to the preamble
matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]

# data:
m, j = 5.3421, 2.6432

# insert a multiline latex expression
matplotlib.pyplot.text(0.2,0.2,

    r'\[' # every line is a separate raw string...
    r'\begin{split}' # ...but they are all concatenated by the interpreter :-)
    r'    y      &= ' + str(round(m,3)) + 'x^{' + str(round(j,3)) + r'}\\'
    r'    r^2    &= 0.95 '
    r'\end{split}'
    r'\]',

    size=50) # make it big so we can see it

matplotlib.pyplot.savefig("test.png")

关于python - Matplotlib - 在我要显示的行中使用 $ 符号时如何添加多行文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33915815/

相关文章:

python - 如何从频谱图中提取numpy数组特征?

python - pyplot : Dotted line with FancyArrowPatch

python - 你知道其他有像 Python 这样的交互模式的编程语言吗?

python - python中递归收集所有选中的QTreeview项

python - 安装 astropy 时出现问题

python - Django 检查单个字段是否有错误并且不运行 clean()?

python - 在不涉及元组的情况下将两个列表变成 1

python - 将 Matplotlib 和 Sympy 的图放在一起

python - 当x值为pandas时间序列时如何为区域着色

python-3.x - Matplotlib WSL2 Pycharm