2012-04-27 17 views
11

एक similar question है - लेकिन मैं वहां समाधान प्रस्तावित समाधान नहीं कर सकता।मैं लंबे शीर्षक कैसे फिट करूं?

#!/usr/bin/env python 

import matplotlib 
import matplotlib.pyplot 
import textwrap 

x = [1,2,3] 
y = [4,5,6] 

# initialization: 
fig = matplotlib.pyplot.figure(figsize=(8.0, 5.0)) 

# lines: 
fig.add_subplot(111).plot(x, y) 

# title: 
myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all." 

fig.add_subplot(111).set_title("\n".join(textwrap.wrap(myTitle, 80))) 

# tight: 
(matplotlib.pyplot).tight_layout() 

# saving: 
fig.savefig("fig.png") 

यह एक

AttributeError: 'module' object has no attribute 'tight_layout' 

देता है और अगर मैं fig.tight_layout() साथ (matplotlib.pyplot).tight_layout() की जगह यह देता है::

AttributeError: 'Figure' object has no attribute 'tight_layout' 

तो मेरे सवाल का

यहाँ एक लंबे शीर्षक के साथ एक उदाहरण साजिश है है - मैं साजिश में शीर्षक कैसे फिट करूं? आप लिंक किए गए कई अन्य अच्छा समाधान है कि जोड़ने नई-पंक्तियों को शामिल कर रहे हैं

import pylab as plt 

plt.rcParams["axes.titlesize"] = 8 

myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all." 
plt.title(myTitle) 
plt.show() 

enter image description here

जवाब में:

+1

'tight_layout' केवल matplotlib की विज्ञप्ति के पिछले कुछ में है। आप कौन सा संस्करण उपयोग कर रहे हैं? मुझे लगता है कि 'tight_layout' 1.1 में जोड़ा गया था, हालांकि यह 1.0 हो सकता है। –

+0

मेरा '1.0.1' है। – Adobe

+0

@ जो किंगटन: शायद आप सही: अपने [उत्तर] को पुन: उत्पन्न करना (http://stackoverflow.com/a/9604442/788700) एक ही त्रुटि देता है। मैं नवीनतम स्रोत डाउनलोड कर रहा हूँ। – Adobe

उत्तर

35

यहाँ मैं अंत में इस्तेमाल किया गया है:

#!/usr/bin/env python3 

import matplotlib 
from matplotlib import pyplot as plt 
from textwrap import wrap 

data = range(5) 

fig = plt.figure() 
ax = fig.add_subplot(111) 

ax.plot(data, data) 

title = ax.set_title("\n".join(wrap("Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all.", 60))) 

fig.tight_layout() 
title.set_y(1.05) 
fig.subplots_adjust(top=0.8) 

fig.savefig("1.png") 

enter image description here

5

एक तरह से यह करने के लिए बस शीर्षक का फ़ॉन्ट आकार बदलने के लिए है। यहां तक ​​कि automatic solution भी है जो आंकड़े के आधार पर आकार बदलता है!

+0

यह अच्छा है। +1 – Adobe

+1

@ मैडप्लॉटिब को अनुकूलित करने के लिए go-to पृष्ठ पर एडोबोब यहां है: http://matplotlib.sourceforge.net/users/customizing.html, हालांकि यह पता चलता है कि आप क्या जानते हैं कि आप पहले से क्या खोज रहे हैं! – Hooked

+0

यह काम करता है, लेकिन यह एक बुरा समाधान है। आपके उदाहरण में, शीर्षक के लिए फ़ॉन्ट टिक लेबल के मुकाबले भी छोटा है। इससे प्रकाशन या प्रस्तुति में शीर्षक को पढ़ने में बहुत मुश्किल होगी। – gerrit

संबंधित मुद्दे

 संबंधित मुद्दे