31

同様の質問がありますが、そこで提案された解決策を機能させることはできません。

タイトルが長いプロットの例を次に示します。

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

それに置き換える(matplotlib.pyplot).tight_layout()と次のようにfig.tight_layout()なります。

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

だから私の質問は-タイトルをプロットに合わせるにはどうすればよいですか?

4

5 に答える 5

88

これが私が最終的に使用したものです:

#!/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")

ここに画像の説明を入力

于 2012-05-17T11:30:37.783 に答える
8

これを行う1つの方法は、タイトルのフォントサイズを変更することです。

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

ここに画像の説明を入力してください

あなたがリンクした答えには、改行を追加することを含む他のいくつかの良い解決策があります。フィギュアに基づいてサイズを変更する自動ソリューションもあります!

于 2012-04-27T13:38:57.917 に答える