【问题标题】:How to fix matplotib incompatibility如何修复 matplotlib 不兼容问题
【发布时间】: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


    【解决方案1】:

    np.bool 很久以前就被弃用了,最近被移除以支持内置的 bool,所以只需将 np.bool 更改为 bool

    “教程”站点几乎总是过时的,因为没有人修改它们,因此您应该查看matplotlibnumpy的官方文档,例如cube documentation

    您还缺少代码末尾的 plt.show()

    【讨论】:

    • 例如,如果 OP 使用的是 Jupyter 笔记本,则完全不需要 plt.show()
    猜你喜欢
    • 2019-10-25
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多