2012-04-27 6 views
11

similar questionがありますが、そこで提案されている解決策を作成することはできません。どのように長いタイトルに合うのですか?

ここでは、長いタイトルのプロット例です:それは

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

を与え、私はfig.tight_layout()(matplotlib.pyplot).tight_layout()を交換した場合、それが与える

#!/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: '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

@Adobe matplotlibをカスタマイズするためのgo-toページは、http://matplotlib.sourceforge.net/users/customizing.htmlですが、探しているものが分かっていれば役立ちます。 – Hooked

+0

これは動作しますが、それは悪い解決策です。あなたの例では、タイトルのフォントは目盛りのラベルよりも小さくなっています。それは出版物やプレゼンテーションでタイトルを非常に読みにくくします。 – gerrit

関連する問題