如何在 python 中渲染 3D 直方图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14061061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:20:43  来源:igfitidea点击:

How can I render 3D histograms in python?

pythonmatplotlibplotdata-visualizationmayavi

提问by Joseph Garvin

I want to make plots like these from Hacker's Delight:

我想从Hacker's Delight 中制作这样的情节:

enter image description here

在此处输入图片说明

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.

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

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.

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

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

或者 gnuplot 可以工作,但我不想为此学习另一种语言语法。

采纳答案by crayzeewulf

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

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