python type_error ai_generated true

TypeError: 'figsize' is an invalid keyword argument for <class 'matplotlib.figure.Figure'>

ID: python/matplotlib-figure-size-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-01-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.7 active
3.8 active

Root Cause

Passing figsize directly to plt.figure() is correct, but if user mistakenly passes it to a subplot function like plt.subplot() or plt.subplots(), it raises a TypeError because those functions don't accept figsize.

generic

中文

将figsize直接传递给plt.figure()是正确的,但如果用户错误地将其传递给子图函数如plt.subplot()或plt.subplots(),则会引发TypeError,因为这些函数不接受figsize。

Workarounds

  1. 95% success Pass figsize only to plt.figure() or plt.subplots()
    fig, ax = plt.subplots(figsize=(8,6))
  2. 90% success Use set_size_inches() on the figure object after creation
    fig = plt.figure(); fig.set_size_inches(8,6)

Dead Ends

Common approaches that don't work:

  1. Using figsize as a positional argument 80% fail

    figsize must be a keyword argument; positional passing will be misinterpreted.

  2. Setting plt.rcParams['figure.figsize'] before creating figure 70% fail

    This changes default figure size, but the error is about passing figsize to wrong function.