如何在 Python3 中输入提示 matplotlib Figure 对象

sps*_*pst 4 python matplotlib type-hinting

我正在尝试为 plt.subplots 返回的数据添加类型提示。这对于 plt.Axes 来说效果很好,但我似乎找不到图的解决方案。

有什么想法我可以做什么吗?

我的代码的缩写版本是:

def draw_graph() -> Tuple[plt.Figure, plt.Axes]: 

    fig, ax = plt.subplots(figsize=(14,10))
    return (fig, ax)
Run Code Online (Sandbox Code Playgroud)

我收到消息:“Figure”不是模块 Pylance 的已知成员

qwe*_*rty 5

使用最新的 Matplotlib (v3.7.1),我能够执行以下操作:

import matplotlib.pyplot as plt
import matplotlib.figure

def draw_graph() -> Tuple[matplotlib.figure.Figure, plt.Axes]: 

    fig, ax = plt.subplots(figsize=(14,10))
    return (fig, ax)
Run Code Online (Sandbox Code Playgroud)

我还没有使用 进行过测试plt.Figure,但我的 IDE(即 VS Code)没有给我任何关于 的错误plt.Figure

  • 我必须将第二行更改为“import matplotlib.figure”。然后它似乎对我有用。 (2认同)