2016-08-17 2 views
1

Ich versuche, eine Figur mit einer bestimmten Konturlinie (level = 320) zu verschiedenen Zeitpunkten zu zeichnen, weshalb eine Schleife verwendet wird. Ich möchte Plot eine Legende mit Etiketten die Zeit, während der Schleife angeben, wie hier:Matplotlib - Legende einer bestimmten Kontur zu verschiedenen Zeitpunkten

enter image description here

Ein Teil meines Codes angezeigt:

cmap = plt.cm.hot 

instant = 0 
for instant in range(0,Sigma_stockMAX.shape[2]): 

    name = 'test' 

    VM_MAX_eq = 1./np.sqrt(2)*np.sqrt((Sigma_stockMAX[:,2,instant]-Sigma_stockMAX[:,3,instant])**2 + (Sigma_stockMAX[:,3,instant])**2 + (-Sigma_stockMAX[:,2,instant])**2 + 6*Sigma_stockMAX[:,5,instant]**2) 


    VM_MAX_eq_interpolate = interpolate(VM_MAX_eq, vtx, wts).reshape(X.shape[0],X.shape[0]) 

    plt.legend(loc='upper center', shadow=True) 

    contour = plt.contour(XX_field[20:480,20:480], YY_field[20:480,20:480],ndimage.filters.gaussian_filter(VM_MAX_eq_interpolate[20:480,20:480], 5),colors=(cmap(instant/ np.float(Sigma_stockMAX.shape[2])),),levels = [320],linestyles=('-',),linewidths=(2,)) 

plt.savefig(name+ '_0' + test[instant][81:110] + ".png", dpi=250) 

Ich habe versucht, in der Schleife hinzufügen dieser Teil, aber es funktioniert nicht:

labels = ['line1', 'line2','line3','line4', 
      'line5', 'line6', 'line6', 'line6', 'line6', 'line6'] 

for i in range(len(labels)): 
    contour.collections[instant].set_label(labels[instant]) 

Antwort

1

I verwendet: http://matplotlib.org/examples/pylab_examples/contour_demo.html

cmap = plt.cm.hot 
lines = [] 
labels = [] 
i = 0 
instant = 0 
for instant in range(0,Sigma_stockMAX.shape[2]): 

    name = 'test' 

    VM_MAX_eq = 1./np.sqrt(2)*np.sqrt((Sigma_stockMAX[:,2,instant]-Sigma_stockMAX[:,3,instant])**2 + (Sigma_stockMAX[:,3,instant])**2 + (-Sigma_stockMAX[:,2,instant])**2 + 6*Sigma_stockMAX[:,5,instant]**2) 

    VM_MAX_eq_interpolate = interpolate(VM_MAX_eq, vtx, wts).reshape(X.shape[0],X.shape[0]) 

    contour = plt.contour(XX_field[20:480,20:480], YY_field[20:480,20:480],ndimage.filters.gaussian_filter(VM_MAX_eq_interpolate[20:480,20:480], 5),colors=(cmap(instant/ np.float(Sigma_stockMAX.shape[2])),),levels = [320],linestyles=('-',),linewidths=(2,)) 
    lines.extend(contour.collections) 
    labels.extend(['line'+str(i+j) for j in range(len(contour.collections))]) 
    i += len(contour.collections) 
plt.legend(lines, labels, loc='upper center', shadow=True) 
plt.savefig(name+ '_0' + test[instant][81:110] + ".png", dpi=250) 
Verwandte Themen