2017-07-13 3 views
2

私はラテックススクリプト$ \ theta $をmatplotlibの図の軸と凡例に付けたいと思っています。だから私は次のコードを使用します。matplotlibのラテックススクリプト

y = np.linspace(0, 2*np.pi, 200) 
plt.plot(y, np.sin(y)*np.cos(y), lw=3, label='$sin(\theta) cos(\theta)$') 

plt.xlabel('$\theta$', fontsize=40) 
plt.ylabel('$P(\theta,t)$', fontsize=40) 

しかし、これは動作し、コードは$ \ $シータのために動作しますが、私は、変数名を変更したくない次の図 enter image description here

を返していません。同じ問題が$ \ rho $、$ \ tau $、$ \ alpha $、$ \ beta $で発生します。この問題を解決する方法についていくつかのヒントを挙げてください。

ありがとうございました。

答えて

3

生の文字列を使用します。そうしないと、文字列の\tTABと解釈されます。

文字列とバイトリテラルの両方に、接頭文字「 」とオプションで接頭辞「r」または「R」を付けることもできます。そのような文字列は生の文字列と呼ばれ、リテラル文字として バックスラッシュを扱います。

ラテックス式を含む文字列の場合は、バックスラッシュを使用することが多いため、一般的にそうする必要があります。

y = np.linspace(0, 2*np.pi, 200) 
plt.plot(y, np.sin(y)*np.cos(y), lw=3, label=r'$sin(\theta) cos(\theta)$') 

plt.xlabel(r'$\theta$', fontsize=40) 
plt.ylabel(r'$P(\theta,t)$', fontsize=40) 

enter image description here

2

それはタブとして解釈されていないので、代わりに、単に文字をエスケープします。

y = np.linspace(0, 2*np.pi, 200) 
plt.plot(y, np.sin(y)*np.cos(y), lw=3, label='$sin(\\theta) cos(\\theta)$') 

plt.xlabel('$\\theta$', fontsize=40) 
plt.ylabel('$P(\\theta,t)$', fontsize=40) 

NB:伝説/ラベルを表示するだけでなく

plt.legend() 

enter image description here