2016-04-06 14 views
5

मुझे matplotlib के contourf फ़ंक्शन के साथ कोई समस्या है। मेरे पास एक txt डेटा फ़ाइल है जिसमें से मैं अपना डेटा आयात कर रहा हूं। मेरे पास डेटा के कॉलम हैं (pm1 और pm2) और मैं 2 डी हिस्टोग्राम कर रहा हूं। मैं इस डेटा को 3 डी हिस्टोग्राम के रूप में प्लॉट करना चाहता हूं और एक समोच्च साजिश के रूप में देखना चाहता हूं कि अधिकतम मूल्य कहां स्थित है।3 डी हिस्टोग्राम और कंटूर प्लॉट्स पायथन

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

मैं 3 डी बार ग्राफ प्लॉट कर सकते हैं, लेकिन मैं, समोच्च एक साजिश अगर मैं contourf समारोह में hist जगह मैं त्रुटि मिलती नहीं पा रहा हूँ: Length of x must be number of columns in z और अगर

यह मेरा कोड है मैं dz रखता हूं मुझे Input z must be a 2D array मिलता है मैंने xedges और yexges का उपयोग करने का भी प्रयास किया है लेकिन इससे समस्या हल नहीं होती है।

मुझे लगता है कि समस्या फ़ंक्शन हिस्टोग्राम 2 डी की वापसी के आकार से संबंधित है। लेकिन मुझे नहीं पता कि इसे कैसे हल किया जाए।

मैं एक 3 डी बार प्लॉट भी एक रंगीन कोड बदलने के साथ न्यूनतम मूल्य के लिए न्यूनतम रूप में करना चाहूंगा। क्या ऐसा करने के लिए वैसे भी है?

धन्यवाद

उत्तर

1

शायद मुझे समझ नहीं आता कि वास्तव में क्या आप के बाद से मैं अपने डेटा की तरह दिखता है क्या पता नहीं है क्या करने के लिए कोशिश कर रहे हैं, लेकिन यह आपके contourf साजिश के रूप में एक ही धुरी को साझा करने के लिए गलत लगता है आपके bar3d साजिश। यदि आप किसी नए आंकड़े के लिए 3 डी प्रोजेक्शन के बिना अक्ष जोड़ते हैं, तो आपको प्लॉट hist का उपयोग करके ठीक सेप्लॉट बनाने में सक्षम होना चाहिए। एक यादृच्छिक, सामान्य वितरण से डेटा का उपयोग कर एक उदाहरण:

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 की तरह एक छवि देता है।

अपने दूसरे प्रश्न का सवाल है, एक कलर-कोडेड 3 डी बार साजिश के बारे में, कैसे इस बारे में (ऊपर लेकिन आकार 1/10 के साथ के रूप में ही डेटा का उपयोग कर):

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

मैं this image मिलता है।

+0

कलरबार अनुकूलन लाइनों के संदर्भ: [pylab उदाहरण] (http://matplotlib.org/examples/pylab_examples/hist_colormapped.html) और [यह पोस्ट] (http://stackoverflow.com/questions/11950375/apply रंग-नक्शा करने वाली एमपीएल-उपकरणकिटें-mplot3d-axes3d-bar3d) – lanery

संबंधित मुद्दे