2017-05-21 1 views
0

Ich habe Probleme beim Durchlaufen jedes Teilplots. Ich erreiche die Koordinaten für das Subplot und möchte dann, dass auf jedem Subplot verschiedene Modelle erscheinen. Meine aktuelle Lösung durchläuft jedoch alle Unterplattformen, führt jedoch bei jedem einen Durchlauf durch alle Modelle durch und lässt das letzte Modell an jedem Teilplot grafisch darstellen, was bedeutet, dass alle gleich aussehen.Wie verwende ich matplotlib um einen großen Graphen von Unterplots zu erstellen?

Mein Ziel ist es, ein Modell auf jedem Subplot zu platzieren. Bitte helfen Sie!

modelInfo = csv_info(filename) # obtains information from csv file 
f, axarr = plt.subplots(4, 6) 
for i in range(4): 
    for j in range(6): 
     for model in modelInfo: 
      lat = dictionary[str(model) + "lat"] 
      lon = dictionary[str(model) + "lon"] 
      lat2 = dictionary[str(model) + "lat2"] 
      lon2 = dictionary[str(model) + "lon2"] 
      axarr[i, j].plot(lon, lat, marker = 'o', color = 'blue') 
      axarr[i, j].plot(lon2, lat2, marker = '.', color = 'red') 
      axarr[i, j].set_title(model) 

Antwort

2

Ich denke, das ist, was Sie, suchen die solange len(modelInfo) arbeitet weniger als 6x4=24:

modelInfo = csv_info(filename) # obtains information from csv file 
f, axarr = plt.subplots(4, 6) 

for n, model in enumerate(modelInfo): 
    i = int(n/4) 
    j = n % 6 
    lat = dictionary[str(model) + "lat"] 
    lon = dictionary[str(model) + "lon"] 
    lat2 = dictionary[str(model) + "lat2"] 
    lon2 = dictionary[str(model) + "lon2"] 
    axarr[i, j].plot(lon, lat, marker = 'o', color = 'blue') 
    axarr[i, j].plot(lon2, lat2, marker = '.', color = 'red') 
    axarr[i, j].set_title(model) 
4

Sie Ihre Modelle zip können und Achsen zusammen und Schleife über beide gleichzeitig Zeit. Da Ihre Subplots jedoch als 2d array kommen, müssen Sie zuerst ihre Elemente "linearisieren". Sie können das leicht tun, indem Sie die reshape-Methode für numpy Arrays verwenden. Wenn Sie dieser Methode den Wert -1 geben, wird das Array in einen 1d Vektor umgewandelt. Mangels Ihrer Eingabedaten habe ich ein Beispiel mit mathematischen Funktionen aus numpy gemacht. Die lustige getattr Linie ist nur da, damit ich leicht in der Lage war Titel zu den Stellplätzen hinzuzufügen:

from matplotlib import pyplot as plt 
import numpy as np 

modelInfo = ['sin', 'cos', 'tan', 'exp', 'log', 'sqrt'] 

f, axarr = plt.subplots(2,3) 


x = np.linspace(0,1,100) 
for model, ax in zip(modelInfo, axarr.reshape(-1)): 
    func = getattr(np, model) 
    ax.plot(x,func(x)) 
    ax.set_title(model) 

f.tight_layout() 
plt.show() 

Das Ergebnis sieht wie folgt aus: figure with different functions in different subplots.

Beachten Sie, dass, wenn Ihre Anzahl von Modellen die Anzahl der verfügbaren subplots überschreitet, die überschüssigen Modelle ohne Fehlermeldung ignoriert werden.

Hoffe, das hilft.

+1

nett! Viel besser als das, was ich gemacht habe! –

+0

@GergesDib deins erledigt das auch, das ist der wichtigste Teil;) –

Verwandte Themen