如何在python中渲染3D直方图? [英] How can I render 3D histograms in python?

查看:141
本文介绍了如何在python中渲染3D直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Hacker's Delight

在Python中有什么方法可以做到这一点?一种易于交互调整图形(更改当前观察到的X / Y切片)的解决方案将是理想的。

What ways are there to accomplish this in Python? A solution that makes it easy to interactively adjust the graph (changing the slice of X/Y currently being observed) would be ideal.

matplotlib和mplot3d模块都没有此功能功能AFAICT。我找到了mayavi2,但是它非常笨拙(我什至找不到调整大小的选项),而且似乎只能在从ipython运行时正常工作。

Neither matplotlib nor the mplot3d module have this functionality AFAICT. I found mayavi2 but it's extremely clunky (I can't even find the option for adjusting the sizes) and only seems to work correctly when run from ipython.

或者gnuplot可以可以,但是我不愿意为此学习另一种语言语法。

Alternatively gnuplot could work, but I'd hate to have to learn another language syntax just for this.

推荐答案

由于TJD指出了该示例似乎是难以理解的,这里是经过修改的版本,其中包含一些注释,可能有助于澄清以下内容:

Since the example pointed out by TJD seemed "impenetrable" here is a modified version with a few comments that might help clarify things:

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

这篇关于如何在python中渲染3D直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆