2015-03-31 91 views
1

我需要在圖的文本框中寫入,並且在LaTeX環境(因爲我還需要編寫一些數學代碼)中寫入一個變量的名稱包含一個下劃線。matplotlib:在文本框中使用LaTeX時,在字符串中覆蓋下劃線

問題是LaTeX將變量名稱中的下劃線解釋爲子索引命令,並且該變量的名稱是失真的。見(MWE下文):

enter image description here

其中變量的名稱是m_out

如何在沒有LaTeX將其解釋爲subindex命令的情況下編寫包含下劃線的字符串?

在純乳膠我可以使用\textunderscore命令寫入:

N = m \textunderscore out \pm 0.2 

其正常生產:

enter image description here

,但是這似乎並沒有在這裏工作。


MWE

import matplotlib.pyplot as plt 
import matplotlib.offsetbox as offsetbox 
import random 

# Generate random data. 
x = [random.random() for i in xrange(10)] 
y = [random.random() for i in xrange(10)] 

# Define string with underscore. 
name = 'm_out' 

# Create plot. 
fig = plt.figure() 
ax = plt.subplot() 

# Add text box 
text = r'$N={}\pm0.2$'.format(name) 
ob = offsetbox.AnchoredText(text, loc=1, prop=dict(size=12)) 
ax.add_artist(ob) 
plt.scatter(x, y) 

# Save plot to file. 
fig.tight_layout() 
plt.savefig('out.png') 

回答

2

一個簡單的逃生的伎倆:

name = 'm\_out' 

matplotlib-generated image

+0

確實非常簡單,應該已經想到這一點。謝謝! – Gabriel 2015-03-31 16:13:53

相關問題