2016-04-06 90 views
5

Ho un problema con la funzione di contouring di matplotlib. Ho un file di dati txt da cui sto importando i miei dati. Ho colonne di dati (pm1 e pm2) e sto eseguendo un istogramma 2D. Voglio tracciare questi dati come un istogramma 3D e come grafico di contorno per vedere dove si trovano i valori massimi.Istogrammi 3D e contorni di contorno Python

Questo è il mio codice:

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
rows = np.arange(200,1300,10) 

hist, xedges, yedges = np.histogram2d (pm1_n, pm2_n, bins = (rows, rows)) 
elements = (len(xedges) - 1) * (len(yedges) - 1) 


xpos, ypos = np.meshgrid(xedges[:-1], yedges[:-1]) 

xpos = xpos.flatten() 
ypos = ypos.flatten() 
zpos = np.zeros(elements) 
dx = 0.1 * np.ones_like(zpos) 
dy = dx.copy() 
dz = hist.flatten() 

#####The problem is here##### 

#ax.contourf(xpos,ypos,hist) 
#ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average') 

plt.show() 

posso tracciare il grafico a barre 3D, ma io non sono in grado di tracciare il contorno uno, se metto hist nella funzione contourf ottengo l'errore: Length of x must be number of columns in z e se Inserisco dz Ottengo Input z must be a 2D array Ho anche provato a usare xedges e yexges ma questo non risolve il problema.

Penso che il problema sia correlato alla forma del ritorno dell'istogramma funzione2D. Ma non so come risolverlo.

Vorrei anche eseguire un grafico a barre 3D con un codice colore che cambia dal valore minimo al valore massimo. C'è comunque da fare questo?

Grazie

risposta

1

Forse non si capisce che cosa esattamente si sta cercando di fare dato che non so che cosa i vostri dati sembra, ma sembra sbagliato per avere il vostro contourf trama condividono lo stesso asse come bar3d trama. Se aggiungi un asse senza la proiezione 3D a una nuova figura, dovresti essere in grado di creare una trama contourf semplicemente usando hist. Un esempio utilizzando i dati di una casuale distribuzione normale:

import numpy as np 
import matplotlib.pyplot as plt 

n_points = 1000 
x = np.random.normal(0, 2, n_points) 
y = np.random.normal(0, 2, n_points) 

hist, xedges, yedges = np.histogram2d(x, y, bins=np.sqrt(n_points)) 

fig2D = plt.figure() 
ax2D = fig2D.add_subplot(111) 
ax2D.contourf(hist, interpolation='nearest', 
       extent=(xedges[0], xedges[-1], yedges[0], yedges[-1])) 
plt.show() 

restituisce un'immagine come this.

Per quanto riguarda la seconda domanda, per quanto riguarda un grafico a barre 3D color-coded, come su questo (utilizzando gli stessi dati come sopra ma con 1/10 delle dimensioni):

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
import matplotlib.colors as colors 

n_points = 100 
x = np.random.normal(0, 2, n_points) 
y = np.random.normal(0, 2, n_points) 

hist, xedges, yedges = np.histogram2d(x, y, bins=np.sqrt(n_points)) 

# Following your data reduction process 
xpos, ypos = np.meshgrid(xedges[:-1], yedges[:-1]) 

length, width = 0.4, 0.4 
xpos = xpos.flatten() 
ypos = ypos.flatten() 
zpos = np.zeros(n_points) 
dx = np.ones(n_points) * length 
dy = np.ones(n_points) * width 
dz = hist.flatten() 

# This is where the colorbar customization comes in 
dz_normed = dz/dz.max() 
normed_cbar = colors.Normalize(dz_normed.min(), dz_normed.max()) 
# Using jet, but should work with any colorbar 
color = cm.jet(normed_cbar(dz_normed)) 

fig3D = plt.figure() 
ax3D = fig3D.add_subplot(111, projection='3d') 
ax3D.bar3d(xpos, ypos, zpos, dx, dy, dz, color=color) 
plt.show() 

I Get this image.

+0

Riferimenti alle linee di personalizzazione colorbar: [esempio pylab] (http://matplotlib.org/examples/pylab_examples/hist_colormapped.html) e [questo post] (http://stackoverflow.com/questions/11950375/apply -color-map-to-MPL-toolkit-mplot3d-axes3d-bar3d) – lanery