简体   繁体   English

如何在 matplotlib 中显示一个 sympy 方程

[英]How to show a sympy equation in matplotlib

Is it possible to show an equation from sympy in matplotlib?是否可以在 matplotlib 中显示 sympy 的方程? Let's suppose that I have this simple integral expression假设我有这个简单的积分表达式

1

How can I show the integral as shown in "Out [2]" in the xlabel of a graph in matplotlib?如何在 matplotlib 图形的 xlabel 中显示积分,如“Out [2]”所示?

Yes, it can be done.是的,这是可以做到的。 Matplotlib supports TeX for text by surrounding specific text syntax between dollar signs. Matplotlib 通过在美元符号之间包围特定的文本语法来支持文本的 TeX And sympy can export an expression to latex syntax, so you don't have to do any manual parsing.并且 sympy 可以将表达式导出为 latex 语法,因此您不必进行任何手动解析。

import matplotlib.pyplot as plt
import sympy as sym
from sympy.printing import latex
    
x = sym.symbols('x')
f = sym.Integral(sym.sqrt(1/x), x)
plt.plot([])
plt.xlabel(f'${latex(f)}$')
plt.show()

Output Output

1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM