2012-12-27 13 views
14

मैं Hacker's Delight से इस तरह के भूखंडों बनाना चाहते:मैं पाइथन में 3 डी हिस्टोग्राम कैसे प्रस्तुत कर सकता हूं?

enter image description here

वहाँ पायथन में यह पूरा करने के क्या तरीके हैं? एक समाधान जो ग्राफ को अंतःक्रियात्मक रूप से समायोजित करना आसान बनाता है (वर्तमान में एक्स/वाई का टुकड़ा बदलना) आदर्श होगा।

न तो matplotlib और न ही mplot3d मॉड्यूल में यह कार्यक्षमता AFAICT है। मुझे मायावी 2 मिला लेकिन यह बेहद उलझन में है (मुझे आकारों को समायोजित करने का विकल्प भी नहीं मिला है) और आईपीथॉन से चलने पर ही सही तरीके से काम करना प्रतीत होता है।

वैकल्पिक रूप से gnuplot काम कर सकता है, लेकिन मुझे इसके लिए सिर्फ एक और भाषा वाक्यविन्यास सीखना नफरत है।

+16

यह matplotlib द्वारा समर्थित है। इसे देखें: http://matplotlib.org/examples/mplot3d/hist3d_demo.html – TJD

+0

@TJD: अच्छा खोज। हां, वह उदाहरण हालांकि अभेद्य दिखता है। –

+0

क्या आपने ['barchart()'] (http://docs.enthought.com/mayavi/mayavi/mlab.html) की कोशिश की है। – Developer

उत्तर

19

के बाद से उदाहरण TJD से कहा "अभेद्य" लग रहा था यहाँ कुछ टिप्पणी है कि चीजों को स्पष्ट मदद कर सकता है के साथ एक संशोधित संस्करण है:

#! /usr/bin/env python 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 
# 
# Assuming you have "2D" dataset like the following that you need 
# to plot. 
# 
data_2d = [ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
      [6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 
      [11, 12, 13, 14, 15, 16, 17, 18 , 19, 20], 
      [16, 17, 18, 19, 20, 21, 22, 23, 24, 25], 
      [21, 22, 23, 24, 25, 26, 27, 28, 29, 30] ] 
# 
# Convert it into an numpy array. 
# 
data_array = np.array(data_2d) 
# 
# Create a figure for plotting the data as a 3D histogram. 
# 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
# 
# Create an X-Y mesh of the same dimension as the 2D data. You can 
# think of this as the floor of the plot. 
# 
x_data, y_data = np.meshgrid(np.arange(data_array.shape[1]), 
           np.arange(data_array.shape[0])) 
# 
# Flatten out the arrays so that they may be passed to "ax.bar3d". 
# Basically, ax.bar3d expects three one-dimensional arrays: 
# x_data, y_data, z_data. The following call boils down to picking 
# one entry from each array and plotting a bar to from 
# (x_data[i], y_data[i], 0) to (x_data[i], y_data[i], z_data[i]). 
# 
x_data = x_data.flatten() 
y_data = y_data.flatten() 
z_data = data_array.flatten() 
ax.bar3d(x_data, 
      y_data, 
      np.zeros(len(z_data)), 
      1, 1, z_data) 
# 
# Finally, display the plot. 
# 
plt.show() 
संबंधित मुद्दे