LoginSignup
11
11

More than 5 years have passed since last update.

matplotlibでSymPyの数式をLaTeXを使って綺麗に描画

Posted at

はじめに

matplotlibでSymPyの数式をLaTeXを使って綺麗に描画したい

import numpy as np 
import matplotlib.pyplot as plt 
import sympy

# おまじない
plt.rcParams['text.usetex'] = True

準備

x  = sympy.Symbol('x')
xx = np.linspace(0,1,1000)

f  = sympy.sin(sympy.pi*x)
ff = sympy.lambdify(x,f) 

plt.figure() 
plt.plot(xx,ff(xx))

LaTeXコマンド

LaTeXコマンドを使うときは$マークで囲います.

plt.title('$f(x) = \sin(\pi x)$')

SymPyから変換して描画

sympy.latex()でLaTeXコマンドに変換して$マークで囲います.

plt.title('$f(x) = ' + sympy.latex(f)+'$')

コード

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11