2012-12-27 6 views
14

Ich möchte Plots wie diese von Hacker's Delight machen:Wie kann ich 3D-Histogramme in Python rendern?

enter image description here

Welche Möglichkeiten gibt es, dies in Python zu erreichen? Eine Lösung, die es einfach macht, den Graphen interaktiv anzupassen (das Ändern der gerade beobachteten X/Y-Schicht) wäre ideal.

Weder Matplotlib noch das Mplot3d-Modul haben diese Funktionalität AFAICT. Ich fand mayavi2, aber es ist extrem klobig (ich kann nicht einmal die Option zum Anpassen der Größen finden) und scheint nur korrekt zu funktionieren, wenn es von ipython ausgeführt wird.

Alternativ könnte gnuplot arbeiten, aber ich würde es hassen, andere Sprache Syntax nur für diese zu haben, zu lernen.

+16

Das von matplotlib unterstützt wird. Siehe hierzu: http://matplotlib.org/examples/mplot3d/hist3d_demo.html – TJD

+0

@TJD: Gut gefunden. Huch, dieses Beispiel sieht allerdings undurchdringlich aus. –

+0

Haben Sie versucht, ['barchart()'] (http://docs.enthought.com/mayavi/mayavi/mlab.html). – Developer

Antwort

19

Da das Beispiel darauf hingewiesen, durch TJD schien „undurchdringlich“ hier ist eine modifizierte Version mit ein paar Kommentare, die Dinge klären helfen könnten:

#! /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() 
Verwandte Themen