如何在.py文件中使用Matplotlib在图的图例中编写Latex公式? [英] How do I write a Latex formula in the legend of a plot using Matplotlib inside a .py file?

查看:74
本文介绍了如何在.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公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆