You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Matplotlib绘制3D图报错:numpy.ndarray无show属性

Hey, let's fix this issue quickly! The error you're seeing is super straightforward—X is a numpy array (the result of your PCA transformation), and numpy arrays don't have a show() method. That method belongs to matplotlib, which is the library you're using to plot the 3D figure.

Here's what you need to do to get your 3D plot working correctly:

The Core Fix

Instead of calling X.show(), you need to call matplotlib's show() method (since you imported matplotlib.pyplot as plot in your code). That's the only change you need for the basic plot, but we can add some polish to make the visualization clearer too.

Corrected Full Code

# Make sure you've imported the required libraries first:
# import matplotlib.pyplot as plot
# from sklearn.decomposition import PCA

# Run your PCA transformation
X = pca1.transform(d1)

# Create the figure and 3D axis (using the modern recommended syntax)
fig2 = plot.figure(1, figsize=(10, 6))
ax = fig2.add_subplot(projection='3d')

# Plot your 3D scatter points
ax.scatter(X[:, 0], X[:, 1], X[:, 2])

# Optional but helpful: Add axis labels and a title
ax.set_xlabel('PCA Component 1')
ax.set_ylabel('PCA Component 2')
ax.set_zlabel('PCA Component 3')
ax.set_title('3D Scatter Plot of PCA Results')

# This line tells matplotlib to render and display your plot
plot.show()

Why This Works

  • X is just a numerical array storing your transformed data—it has no built-in ability to display plots.
  • You already used ax.scatter() to draw your data onto the 3D axis. The final step is to tell matplotlib to show the entire figure window with plot.show().

Quick Troubleshooting Tip

If you're working in a Jupyter Notebook and don't see the plot, add %matplotlib inline at the top of your cell to enable inline plotting. For non-interactive environments, you might need to adjust matplotlib's backend settings, but that's a rare edge case.

内容的提问来源于stack exchange,提问作者AFS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:49:19