2010-03-10 12 views
32

Mit, sagen wir, 3 Zeilen Subplots in Matplotlib, xlabels einer Zeile kann den Titel des nächsten überlappen. Man muss mit pl.subplots_adjust(hspace) herumspielen, was nervig ist.Matplotlib subplots_adjust hspace so überlappen Titel und xlabels nicht?

Gibt es ein Rezept für hspace, das Überschneidungen verhindert und funktioniert für jede nrow?

""" matplotlib xlabels overlap titles ? """ 
import sys 
import numpy as np 
import pylab as pl 

nrow = 3 
hspace = .4 # of plot height, titles and xlabels both fall within this ?? 
exec "\n".join(sys.argv[1:]) # nrow= ... 

y = np.arange(10) 
pl.subplots_adjust(hspace=hspace) 

for jrow in range(1, nrow+1): 
    pl.subplot(nrow, 1, jrow) 
    pl.plot(y**jrow) 
    pl.title(5 * ("title %d " % jrow)) 
    pl.xlabel(5 * ("xlabel %d " % jrow)) 

pl.show() 

Meine Versionen:

  • matplotlib 0.99.1.1,
  • Python 2.6.4,
  • Mac OSX 10.4.11,
  • Backend: Qt4Agg (TkAgg => Ausnahme in Tkinter Rückruf)

(Für viele Extrapunkte, kann jemand umreißen, wie matplotlib packer/spacer arbeitet, entlang der Linien von Kapitel 17 "der Packer" in der Tcl/Tk Buch?)

+2

Sie wollen wahrscheinlich einen Fehler/Wunschliste Eintrag für diese auf dem matplotlib Bugtracker http://sourceforge.net/tracker/?group_id=80706 –

+2

Haben Sie versucht, 'pl Datei. Tight_layout() 'vor' pl.show() 'für eine" automatische "Lösung? – Sebastian

+0

@Sebastian Raschka, "UserWarning: tight_layout: zurück zum Aggendarender", matplotlib 1.4.3 auf einem Mac. (Die Frage war vor 5 Jahren.) – denis

Antwort

16

Ich finde das ziemlich schwierig, aber es gibt einige Informationen darüber here at the MatPlotLib FAQ . Es ist ziemlich umständlich und erfordert über, welchen Raum einzelne Elemente (ticklabels) herauszufinden, nimmt ...

Update: Die Seite heißt es, dass die tight_layout() Funktion der einfachste Weg ist, zu gehen, die automatisch versucht zu korrigieren Abstand.

Andernfalls zeigt es Möglichkeiten, die Größen verschiedener Elemente (z. B. Etiketten) zu erfassen, damit Sie dann die Abstände/Positionen Ihrer Achsenelemente korrigieren können. Hier ist ein Beispiel aus der oben FAQ-Seite, die die Breite einer sehr breiten y-Achsenbeschriftung bestimmt und stellt die Achsenbreite entsprechend:

import matplotlib.pyplot as plt 
import matplotlib.transforms as mtransforms 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.plot(range(10)) 
ax.set_yticks((2,5,7)) 
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels')) 

def on_draw(event): 
    bboxes = [] 
    for label in labels: 
     bbox = label.get_window_extent() 
     # the figure transform goes from relative coords->pixels and we 
     # want the inverse of that 
     bboxi = bbox.inverse_transformed(fig.transFigure) 
     bboxes.append(bboxi) 

    # this is the bbox that bounds all the bboxes, again in relative 
    # figure coords 
    bbox = mtransforms.Bbox.union(bboxes) 
    if fig.subplotpars.left < bbox.width: 
     # we need to move it over 
     fig.subplots_adjust(left=1.1*bbox.width) # pad a little 
     fig.canvas.draw() 
    return False 

fig.canvas.mpl_connect('draw_event', on_draw) 

plt.show() 
+0

akzeptieren, aber umständlich in der Tat - mttiw, mehr Ärger als es wert ist – denis

+0

Link ist weg. Diese Antwort wurde de facto nutzlos. – jojo

+1

Link existiert wieder - sagt 'tight_layout()' ist jetzt der Weg zu gehen, was es tatsächlich ist. – Demis

39

Sie plt.subplots_adjust verwenden können, um den Abstand zwischen den Nebenhandlungen sich ändern Link

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) 

left = 0.125 # the left side of the subplots of the figure 
right = 0.9 # the right side of the subplots of the figure 
bottom = 0.1 # the bottom of the subplots of the figure 
top = 0.9  # the top of the subplots of the figure 
wspace = 0.2 # the amount of width reserved for blank space between subplots 
hspace = 0.2 # the amount of height reserved for white space between subplots 
+3

Die Frage lautete: "Man muss mit plplots_adjust (hspace) herumspielen, nervig." – denis

Verwandte Themen