2011-12-22 18 views
14

में किसी आकृति का विभाजन शीर्षक मैं इसमें 4 उप-भूखंडों के साथ एक आकृति बनाने के लिए matplotlib का उपयोग करता हूं।matplotlib

मैं एक सबप्लॉट के अपने शीर्षक में से एक को विभाजित करना चाहता हूं, जैसे कि प्रत्येक पंक्ति subplot के संबंध में केंद्रित होगी।

मैं

import matplotlib.pylab as plt 

fig = plt.figure(num=0,figsize=(8.27, 11.69), dpi=300) 
ax = fig.add_subplot(2, 2, 1) 
ax.set_title(r'Normalized occupied \\ Neighbors') 

की कोशिश की और मैं क्या मिलता है कि Neighbors बाईं ओर से मांगपत्र है।

मैं इसे कैसे ठीक कर सकता हूं?

उत्तर

22

मैं सही संरेखण मिल जब मैं स्ट्रिंग इस तरह से प्रारूप:

import matplotlib.pylab as plt 

fig = plt.figure()#num=0,figsize=(8.27, 11.69), dpi=300) 
ax = fig.add_subplot(2, 2, 1) 
ax.set_title('Normalized occupied \n Neighbors') 

plt.show() 

enter image description here

7

एक बेहतर समाधान जो r उपसर्ग (विशेष रूप से जब एक LaTeX मार्कअप का उपयोग करने की जरूरत है) करने के लिए है के साथ संगत है शीर्षक पाठ को दो (या अधिक भागों) में विभाजित करें, उदाहरण के लिए,

import matplotlib.pylab as plt 

fig = plt.figure() 
plt.title('My Title\n' + r'$\alpha - \omega$ are LaTeX Markup') 
plt.plot([1,2,3,4]) 
plt.show() 
+0

आपकी टिप्पणी के लिए धन्यवाद - क्या मेरे पास शीर्षक और अलग-अलग गुणों में 2 लाइनें हो सकती हैं? जैसे कि बड़े फ़ॉन्ट आकार और ऊपरी रेखा के लिए बोल्ड और निचले स्तर के लिए सामान्य? – spiff

0

पिछले उत्तरों के अलावा, अगर कोई ऑपरेशन स्वचालित करना चाहता है तो मैंने इसे आसान बनाने के लिए एक फ़ंक्शन को कोड किया है।

def split_title_line(title_text, split_on='(', max_words=5): # , max_words=None): 
    """ 
    A function that splits any string based on specific character 
    (returning it with the string), with maximum number of words on it 
    """ 
    split_at = title_text.find (split_on) 
    ti = title_text 
    if split_at > 1: 
     ti = ti.split (split_on) 
     for i, tx in enumerate (ti[1:]): 
      ti[i + 1] = split_on + tx 
    if type (ti) == type ('text'): 
     ti = [ti] 
    for j, td in enumerate (ti): 
     if td.find (split_on) > 0: 
      pass 
     else: 
      tw = td.split() 
      t2 = [] 
      for i in range (0, len (tw), max_words): 
       t2.append (' '.join (tw[i:max_words + i])) 
      ti[j] = t2 
    ti = [item for sublist in ti for item in sublist] 
    ret_tex = [] 
    for j in range (len (ti)): 
     for i in range(0, len(ti)-1, 2): 
      if len (ti[i].split()) + len (ti[i+1].split()) <= max_words: 
       mrg = " ".join ([ti[i], ti[i+1]]) 
       ti = [mrg] + ti[2:] 
       break 

    if len (ti[-2].split()) + len (ti[-1].split()) <= max_words: 
     mrg = " ".join ([ti[-2], ti[-1]]) 
     ti = ti[:-2] + [mrg] 
    return '\n'.join (ti) 

उदाहरण::

में: split_title_line ('Primary school completion (% of girls)')

आउट:

Primary school completion 
(% of girls) 
यहाँ यह है

में:split_title_line ('Primary school completion in the country as % of girls') आउट:

Primary school completion in the 
country as % of girls 

अपने प्रश्न matplotlib या तो में खिताब को विभाजित करने के लिए, आप है कि हर कोई इस से लाभ इस ax.set_title(split_title_line(r'Normalized occupied Neighbors', max_words=2))

आशा जोड़ सकते हैं ।