2017-11-28 6 views
0

Was ist das Problem? Die Beschriftung der oberen Grafik befindet sich unter der unteren Grafik ... Ich kann mir wirklich nicht vorstellen, wie ich das beheben kann. Ich dachte, es wird normal funktionieren.Unterer Graph überlappt das Etikett des oberen Graphen

Der folgende Code ist von der Website http://www.physics.nyu.edu/pine/pymanual/html/chap5/chap5_plot.html#basic-plotting.

Das Bild, wie das Diagramm sein muss, ist in der Verbindung http://www.physics.nyu.edu/pine/pymanual/html/_images/subplotDemo.png.

Das Bild meines Diagramms, das durch Kopieren des Codes von der obigen Website erhalten wurde, befindet sich im Link My overlapped graph.

import numpy as np 
import matplotlib.pyplot as plt 

theta = np.arange(0.01, 8., 0.04) 
y = np.sqrt((8./theta)**2-1.) 
ytan = np.tan(theta) 
ytan = np.ma.masked_where(np.abs(ytan)>20., ytan) 
ycot = 1./np.tan(theta) 
ycot = np.ma.masked_where(np.abs(ycot)>20., ycot) 

plt.figure(1) 

plt.subplot(2, 1, 1) 
plt.plot(theta, y) 
plt.plot(theta, ytan) 
plt.ylim(-8, 8) 
plt.axhline(color="gray", zorder=-1) 
plt.axvline(x=np.pi/2., color="gray", linestyle='--', zorder=-1) 
plt.axvline(x=3.*np.pi/2., color="gray", linestyle='--', zorder=-1) 
plt.axvline(x=5.*np.pi/2., color="gray", linestyle='--', zorder=-1) 
plt.xlabel("theta") 
plt.ylabel("tan(theta)") 

plt.subplot(2, 1, 2) 
plt.plot(theta, -y) 
plt.plot(theta, ycot) 
plt.ylim(-8, 8) 
plt.axhline(color="gray", zorder=-1) 
plt.axvline(x=np.pi, color="gray", linestyle='--', zorder=-1) 
plt.axvline(x=2.*np.pi, color="gray", linestyle='--', zorder=-1) 
plt.xlabel("theta") 
plt.ylabel("cot(theta)") 

plt.show() 

Antwort

0

Es ist eine ziemlich einfache Lösung: Legen Sie die Linie plt.tight_layout(), bevor Sie Ihr Diagramm zeigen. In dieser Zeile werden die Ränder zwischen Unterplots automatisch angepasst, sodass alles gut aussieht.

In Ihrem Code würde es so aussehen:

plt.tight_layout() 
plt.show() 

Mehr Informationen über die tight_layout finden Sie hier: Tight Layout Guide

+0

Es funktioniert perfekt! Vielen Dank! –

+0

Kennen Sie andere Lösungen? –

+0

Ja, Sie könnten 'subplots_adjust' verwenden, um den Abstand genauer anzupassen. Hier finden Sie weitere Informationen: [sublots_adjust] (https://stackoverflow.com/a/6541454/7846567) – Max

Verwandte Themen