2017-06-27 364 views
1

我試圖用matplotlib生成一個圖並使用'stix'字體(rcParams ['mathtext.fontset'] ='stix')以便具有從文本到數學文本的平滑字體大小轉換。然而,我想要的一些數學符號是斜體(標量值),有些是斜體粗體(張量)。我不想通過使用Latex渲染的解決方案,導致其他事情混亂。如果你運行該代碼的第一個標籤是斜體和第二個標籤是大膽使用stix字體在Matplotlib中製作斜體和粗體樣式的標籤

from numpy import * 
from matplotlib.pyplot import * 

# Chaning font to stix 
rcParams['mathtext.fontset'] = 'stix' 

# Some data to constract this plotting example 
datax=[0,1,2] 
datay=[8,9,10] 
datay2=[8,15,10] 

fig, ay = subplots() 

ay.plot(datax, datay, color="0.", ls='-', label= r"$F_{\alpha}$") 
ay.plot(datax, datay2, color="0.", ls='-', label=r"$\mathbf{F_{\alpha}}$") 

# Now add the legend with some customizations. 
legend = ay.legend(loc='left', shadow=True) 

#frame = legend.get_frame() 
#frame.set_facecolor('0.90') 
xlabel(r"x label",fontsize=18) 
ylabel(r'y label', fontsize=18) 
grid() 

show() 

我會給你描繪了問題的一個小例子。我怎樣才能實現第二個標籤是粗體和斜體?

Problem with math text to be Italic and bold

+0

我不認爲這是可能的。你可能需要使用真正的乳膠(即usetex = True),然後這個問題可能會有所幫助:https://tex.stackexchange.com/questions/14395/bold-italic-vectors – ImportanceOfBeingErnest

回答

0

一些更具體的mathtext PARAMS需要:

from numpy import * 
from matplotlib.pyplot import * 

# Changing font to stix; setting specialized math font properties as directly as possible 
rcParams['mathtext.fontset'] = 'custom' 
rcParams['mathtext.it'] = 'STIXGeneral:italic' 
rcParams['mathtext.bf'] = 'STIXGeneral:italic:bold' 

# Some data to construct this plotting example 
datax=[0,1,2] 
datay=[8,9,10] 
datay2=[8,15,10] 

fig, ay = subplots() 

ay.plot(datax, datay, color="0.", ls='-', label= r"$\mathit{F_{\alpha}}$") 
ay.plot(datax, datay2, color="0.", ls='-', label=r"$\mathbf{F_{\alpha}}$") 

# Now add the legend with some customizations. 
legend = ay.legend(loc='left', shadow=True) 

# Using the specialized math font again 
xlabel(r"$\mathbf{x}$ label",fontsize=18) 
ylabel(r'y label', fontsize=18) 
grid() 

show() 

enter image description here

注意,我在軸標籤使用的mathbf了。可能會將標籤的其餘部分更改爲STIX字體,您可以定義一個非斜體 - 非粗體的情況:請參閱「自定義字體」下的docs

+0

謝謝!那就是我需要的! – Alex

+0

如果這是一個完整的答案,請檢查它,以便其他尋找問題/答案的人知道這是一個什麼狀態。 此外,它很容易回答,因爲你的例子很簡單明瞭,_complete_。 – cphlewis

-1

所以MatplotLib用LaTeX的語法,所以我最好的猜測是,你可以使用LaTeX的語法如何斜體字和大膽,這是

\textbf{\textit{text}} 

所以你的情況它會是

ay.plot(datax, datay2, color="0.", ls='-', label= r"$\mathbf{ \textit{F_{\alpha}} }$") 
+0

答案不起作用。 – ImportanceOfBeingErnest

相關問題