2016-04-06 64 views
5

Matplotlib'in kontur fonksiyonu ile ilgili bir problemim var. Verilerimi içe aktardığım bir txt veri dosyası var. Veri sütunlarım var (pm1 ve pm2) ve bir 2D histogramı yapıyorum. Bu verileri bir 3D histogram olarak çizmek ve maksimum değerlerin nerede bulunduğunu görmek için bir kontur çizimi yapmak istiyorum.3D histogramlar ve Kontur çizimleri Python

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() 

ben 3d çubuk grafik çizmek olabilir ama ben hata alıyorum contourf fonksiyonunda hist yerleştirirseniz, kontur bir arsa mümkün değilim: Length of x must be number of columns in z ve eğer

Bu

benim kodudur Ben dz yerleştiriyorum Input z must be a 2D array Ben de xedges ve yexges kullanmayı denedim ama bu sorunu çözmez.

Problemin histogram2D işlevinin geri dönüş şekli ile ilgili olduğunu düşünüyorum. Ama nasıl çözeceğimi bilmiyorum.

Ayrıca, en az değere kadar değişen bir renk kodu ile bir 3D çubuk arsa gerçekleştirmek istiyorum. Bunu yapmak için zaten var mı?

Eğer

cevap

1

Belki ünüz veya verilerin nasıl göründüğünü bilmiyorum çünkü yapmaya çalıştığımız tam olarak ne anlamıyorum ederim, ama sizin contourf arsa ile aynı eksene paylaşımı olması yanlış geliyor senin bar3d arsa. Eğer 3D projeksiyon olmadan bir eksen yeni bir rakam eklerseniz, hist kullanarak sadece contourf çizim yapabilmeniz gerekir. rastgele, normal dağılımdan verileri kullanarak bir örnek:

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() 

this gibi bir görüntü verir. Bir renk kodlu 3D çubuğu planıyla ilgili olarak ikinci soruya gelince

, buna ne dersin (boyutun üzerindeki ancak 1/10 ile aynı verileri kullanarak):
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() 

Ben this image olsun.

+0

Colorbar özelleştirme satırlarına yapılan başvurular: [pylab example] (http://matplotlib.org/examples/pylab_examples/hist_colormapped.html) ve [bu gönderi] (http://stackoverflow.com/questions/11950375/apply -renk-haritası-to-mpl-araci-mplot3d-axes3d-bar3d) – lanery