python data_error ai_generated true

ValueError: s must be a scalar or 1D array of the same length as x and y, got array of length 5 when x has length 10.

ID: python/matplotlib-3d-scatter-size-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-07-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active

Root Cause

In 3D scatter plot, the size parameter 's' must match the number of points; providing a mismatched array causes this error.

generic

中文

在3D散点图中,大小参数's'必须与点的数量匹配;提供不匹配的数组会导致此错误。

Workarounds

  1. 95% success Ensure s array has the same length as x
    ax.scatter(x, y, z, s=np.random.rand(len(x))*100)
  2. 90% success Use a scalar for uniform size
    ax.scatter(x, y, z, s=50)

Dead Ends

Common approaches that don't work:

  1. Using a 2D array for s 90% fail

    s must be 1D; 2D arrays are not accepted.

  2. Repeating the same size for all points by using a scalar 30% fail

    This works but does not achieve per-point sizing.