簡體   English   中英

如何在.py文件中使用Matplotlib在圖表的圖例中編寫Latex公式?

[英]How do I write a Latex formula in the legend of a plot using Matplotlib inside a .py file?

我正在用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()

為標簽編寫代碼時,它是:

import pylab

# code here

pylab.plot(x,y,'f:', '$sin(x)$')

所以也許pylab.legend('$latex here$')

編輯:

u是unicode字符串,試試r'$\\latex$'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM