簡體   English   中英

如何修復 matplotib 不兼容問題

[英]How to fix matplotib incompatibility

我需要可視化一個立方體,並決定對 python 使用 matplotib。

由於某種原因,代碼拒絕正常運行。 我嘗試了 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

np.bool很久以前就被棄用了,最近被移除以支持內置的bool ,所以只需將np.bool更改為bool

“教程”站點幾乎總是過時的,因為沒有人修改它們,因此您應該查看matplotlibnumpy的官方文檔,例如立方體文檔

您還缺少代碼末尾的plt.show()

我已經對 Ahmed 的回答投了贊成票,但我忍不住要發布生成的圖片。

在此處輸入圖像描述 當然,這是使用data = np.ones(axes, dtype=bool)代替data = np.ones(axes, dtype=np.bool) (以及其他與 OP 問題無關的技巧)獲得的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM