How do I write a Latex formula in the legend of a plot using Matplotlib inside a .py file?(如何在 .py 文件中使用 Matplotlib 在绘图的图例中编写 Latex 公式?)
问题描述
我正在用 Python(.py 文件)编写脚本,并且正在使用 Matplotlib 绘制数组.我想在情节中添加一个带有公式的图例,但我无法做到.我以前在 IPython 或终端中做过这个.在这种情况下,写这样的东西:
I am writing a script in Python (.py file) and I am using Matplotlib to plot an array. I want to add a legend with a formula to the plot, but I haven't been able to do it. I have done this before in IPython or the terminal. In this case, writing something like this:
legend(ur'$The_formula$')
完美运行.但是,当我从终端/IPython 调用我的 .py 脚本时,这不起作用.
worked perfectly. However, this doesn't work when I call my .py script from the terminal/IPython.
推荐答案
最简单的方法是在绘制数据时分配标签,例如:
The easiest way is to assign the label when you plot the data, e.g.:
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()
这篇关于如何在 .py 文件中使用 Matplotlib 在绘图的图例中编写 Latex 公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 .py 文件中使用 Matplotlib 在绘图的图例中编写 Latex 公式?
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
