gpt4 book ai didi

python - 如何在 .py 文件中使用 Matplotlib 在绘图的图例中编写 Latex 公式?

转载 作者:IT老高 更新时间:2023-10-28 20:27:45 26 4
gpt4 key购买 nike

我正在用 Python(.py 文件)编写脚本,并且正在使用 Matplotlib 绘制数组。我想在情节中添加一个带有公式的图例,但我无法做到。我以前在 IPython 或终端中做过这个。在这种情况下,写这样的东西:

legend(ur'$The_formula$')

完美运行。但是,当我从终端/IPython 调用我的 .py 脚本时,这不起作用。

最佳答案

最简单的方法是在绘制数据时分配标签,例如:

import matplotlib.pyplot as plt
ax = plt.gca() # or any other way to get an axis object
ax.plot(x, y, label=r'$\sin (x)$')

ax.legend()

关于python - 如何在 .py 文件中使用 Matplotlib 在绘图的图例中编写 Latex 公式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com