2017-05-18 3 views
0

Ich muss ein PDF aus vier grafischen Plots machen, die in Querformat und in einer 2x2-Matrix (vorzugsweise mit kontrollierbaren Größen) angezeigt werden sollen. Diese Plots folgen einem Titel und folgen einem beschreibenden Text. Bitte beziehen Sie sich auf die Befehle und Module, die Sie für diese Aufgabe nützlich finden.Generate PDF von Bildern und einschließlich Text [Python]

Was ich bisher habe, ist im Grunde genommen mit der Funktion add_subplot() von matplotlib, aber ich kann nicht scheinen, um Text/ein Titel mit makePdf hinzuzufügen.

Mit freundlichen Grüßen, und vielen Dank für Ihre Aufmerksamkeit!

+0

Als Nachtrag, dass meine Antwort scheint nicht zu passen: Sie sollten nicht wirklich diese Beschreibungstexte in 'matplotlib' tun, gibt es viel mehr bessere Möglichkeiten, um ein PDF zu erstellen Bei Ihren Anforderungen verwenden Sie "Latex". –

Antwort

0

zu starten, kann Titel zu jedem subplot Hinzufügen ziemlich einfach ist und die Definition der Achsen nach set_title("Title") Verwendung erfolgen: (Dieses Beispiel aus matplotlib's reference genommen wird)

import matplotlib.pyplot as plt 
import numpy as np 
# Simple data to display in various forms 
x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 

# Four axes, returned as a 2-d array 
f, axarr = plt.subplots(2, 2) 
axarr[0, 0].plot(x, y) 
axarr[0, 0].set_title('Title One') 
axarr[0, 1].scatter(x, y) 
axarr[0, 1].set_title('Title Two') 
axarr[1, 0].plot(x, y ** 2) 
axarr[1, 0].set_title('Title Three') 
axarr[1, 1].scatter(x, y ** 2) 
axarr[1, 1].set_title('Title Four') 
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots 
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False) 
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False) 

die Beschreibung unter jedem subplot hinzuzufügen wir haben einige knifflige Code zu tun, zuerst müssen wir den Raum unter jedem subplot hinzufügen subplots_adjust mit:

# Adjusting height between subplots and adding bottom space 
plt.subplots_adjust(hspace = .4, bottom = .2) 

die Texte zu schreiben, text(x,y,s), bemerken wir jedes x benötigen, y-Koordinate, und um sie zu erhalten, die b est kann ich denke an jeder axarr zu bekommen, ist ‚s coords, indem sie es immer ist bbox‘ s x0 und y0 Parameter, so etwas wie folgt aus:

x0, y0 = axarr[0,0].get_position().x0, axarr[0,0].get_position().y0 
f.text(x0,y0,description[0]) 

x0, y0 sind nur Punkte von Referenzen, dieser Code bemerken plotten die Texte innerhalb der Figur Haupt subplot selbst: enter image description here

Doch obwohl dies keine allgemeine Lösung ist in diesem Fall scheint es y0 von .05 geben sie uns ein ziemlich gutes Ergebnis Ausgleich: enter image description here

Voll Code:

import matplotlib.pyplot as plt 
import numpy as np 

# List of subplot's description 
description = ['''Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.''',''' 
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
    nisi ut aliquip ex ea commodo consequat. ''', '''Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
    pariatur.''',''' Excepteur sint occaecat cupidatat non proident, sunt in 
    culpa qui officia deserunt mollit anim id est laborum.'''] 

# Simple data to display in various forms 
x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 

# Four axes, returned as a 2-d array 
f, axarr = plt.subplots(2, 2) 
axarr[0, 0].plot(x, y) 
axarr[0, 0].set_title('Title One') 
axarr[0, 1].scatter(x, y) 
axarr[0, 1].set_title('Title Two') 
axarr[1, 0].plot(x, y ** 2) 
axarr[1, 0].set_title('Title Three') 
axarr[1, 1].scatter(x, y ** 2) 
axarr[1, 1].set_title('Title Four') 
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots 
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False) 
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False) 
# Adjusting height between subplots and adding bottom space 
plt.subplots_adjust(hspace = .4, bottom = .2) 
# Print position and adding description manually 
x0, y0 = axarr[0,0].get_position().x0, axarr[0,0].get_position().y0 
f.text(x0,y0-.05,description[0]) 
x0, y0 = axarr[0,1].get_position().x0, axarr[0,1].get_position().y0 
f.text(x0,y0-.05,description[1]) 
x0, y0 = axarr[1,0].get_position().x0, axarr[1,0].get_position().y0 
f.text(x0,y0-.05,description[2]) 
x0, y0 = axarr[1,1].get_position().x0, axarr[1,1].get_position().y0 
f.text(x0,y0-.05,description[3]) 

plt.show() 
Verwandte Themen