2012-04-27 7 views
11

C'è un similar question - ma non riesco a far funzionare la soluzione proposta.Come si inserisce il titolo lungo?

Ecco una trama esempio con un titolo lungo:

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

dà un

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

e se sostituisco (matplotlib.pyplot).tight_layout() con fig.tight_layout() dà:

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

Quindi la mia domanda è - come posso adattare il titolo alla trama?

+1

'tight_layout' è solo nell'ultimo paio di uscite di matplotlib. Che versione stai usando? Penso che 'tight_layout' è stato aggiunto in 1.1, anche se potrebbe essere stato 1.0. –

+0

Il mio è '1.0.1'. – Adobe

+0

@Joe Kington: Probabilmente hai ragione: riprodurre la tua [risposta] (http://stackoverflow.com/a/9604442/788700) dà lo stesso errore. Sto scaricando l'ultima fonte. – Adobe

risposta

35

Ecco quello che ho finalmente usato:

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

Un modo per farlo è quello di cambiare semplicemente la dimensione del carattere del titolo:

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

Nella risposta che collegato sono parecchie altre buone soluzioni che coinvolgono a capo l'aggiunta. C'è anche un automatic solution che si ridimensiona in base alla figura!

+0

È fantastico. +1 – Adobe

+1

@Adobe la pagina di go-to per la personalizzazione di matplotlib è qui: http://matplotlib.sourceforge.net/users/customizing.html, anche se aiuta se sai già cosa stai cercando! – Hooked

+0

Funziona, ma è una cattiva soluzione. Nel tuo esempio, il carattere per il titolo è ancora più piccolo di quello delle etichette di graduazione. Ciò renderà il titolo molto difficile da leggere in una pubblicazione o in una presentazione. – gerrit

Problemi correlati