Python Matplotlib dichtPython

Python-Programme
Anonymous
 Python Matplotlib dicht

Post by Anonymous »

Ich verwende Matplotlib, um meine Daten in einem 4x2 -Gitter von Nebenhandlungen zu zeichnen. Die matplotlib.pyplot.TIGHT_LAYOUT passt automatisch die Nebenhandlungen, Legenden und Textbezeichnungen in eine Figur, die ich als PNG speichern kann. Wenn die Legende jedoch extrem lang ist, scheint es einige Nebenhandlungen einen zusätzlichen horizontalen Platz zu verleihen. Die Funktion subplots_adjust sieht vielversprechend aus, aber es gibt viel Versuch und Irrtum, um alles anzupassen, und ich hoffe, eine schnellere automatisierte Lösung mit dight_layout zu finden. Mein MWE ist: < /p>

Code: Select all

import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(-5,5,100)
x1 = np.sin(t)
x2 = np.cos(t)
x3 = np.sin(2*t)
x4 = np.cos(2*t)
x5 = 2*np.sin(t)
x6 = 2*np.cos(t)
x7 = np.sin(0.5*t)
x8 = np.cos(0.5*t)

fig, ax = plt.subplots(nrows=4, ncols=2, figsize=(10, 7))
for r in range(4):
for c in range(2):
ax[r,c].plot(t,x1,label='preliminary 1')
ax[r,c].plot(t,x2,label='preliminary 2')
ax[r,c].plot(t,x3,label='trial 1, result 1')
ax[r,c].plot(t,x4,label='trial 1, result 2')
ax[r,c].plot(t,x5,label='trial 1, result 6')
ax[r,c].plot(t,x6,label='trial 4, result 1')
ax[r,c].plot(t,x7,label='trial 12, result 2')
ax[r,c].plot(t,x8,label='trial 15, result 2')
ax[0,1].legend(loc='best', bbox_to_anchor = (0.3, -1.1, 1.2, 2))
plt.tight_layout()
plt.savefig('myfig.png')
plt.show()

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post