簡體   English   中英

matplotlib:在文本框中使用LaTeX時覆蓋字符串中的下划線

[英]matplotlib: override underscore in string when using LaTeX in text box

我需要在繪圖的文本框中以及在LaTeX環境中(因為我還需要編寫一些數學代碼)編寫包含下划線的變量的名稱。

問題在於,LaTeX將變量名稱中的下划線解釋為子索引命令,並且變量名稱失真。 請參閱(下面的MWE):

在此處輸入圖片說明

變量名稱為m_out

如何在不將LaTeX解釋為subindex命令的情況下編寫包含下划線的字符串?

在純LaTeX中,我可以使用\\textunderscore命令來編寫:

N = m \textunderscore out \pm 0.2

正確產生:

在此處輸入圖片說明

但這似乎在這里不起作用。


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')

一個簡單的轉義就能達到目的:

name = 'm\_out'

matplotlib生成的圖像

暫無
暫無

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

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