【发布时间】:2023-01-08 02:21:17
【问题描述】:
我需要可视化一个立方体,并决定将 matplotib 用于 python。
由于某种原因,代码拒绝正常运行。我尝试了 geeksforgeeks 和 matplotlib 文档示例中的代码,但都不起作用。
是否存在兼容性问题或我做错了什么?
例如这段代码(来自 geeksforgeeks):
# Import libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Create axis
axes = [5, 5, 5]
# Create Data
data = np.ones(axes, dtype=np.bool)
# Control Transparency
alpha = 0.9
# Control colour
colors = np.empty(axes + [4], dtype=np.float32)
colors[0] = [1, 0, 0, alpha] # red
colors[1] = [0, 1, 0, alpha] # green
colors[2] = [0, 0, 1, alpha] # blue
colors[3] = [1, 1, 0, alpha] # yellow
colors[4] = [1, 1, 1, alpha] # grey
# Plot figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Voxels is used to customizations of
# the sizes, positions and colors.
ax.voxels(data, facecolors=colors, edgecolors='grey')
输出:
C:\Users\User\Desktop\Cube visualization\cube.py:10: FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar. (This may have returned Python scalars in past versions.
data = np.ones(axes, dtype=np.bool)
Traceback (most recent call last):
File "C:\Users\User\Desktop\Cube visualization\cube.py", line 10, in <module>
data = np.ones(axes, dtype=np.bool)
^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\__init__.py", line 284, in __getattr__
raise AttributeError("module {!r} has no attribute "
AttributeError: module 'numpy' has no attribute 'bool'. Did you mean: 'bool_'?
Process finished with exit code 1
我正在使用最新版本的 python,并且刚刚使用 python -m pip install -U matplotlib 安装了 matplotlib
【问题讨论】:
标签: python matplotlib