首页 > 解决方案 > 如何在 python3 中键入提示 matplotlib.axes._subplots.AxesSubplots 对象

问题描述

我想知道如何输入提示 matplotlib-subplots 的轴对象的“最佳”方式。

跑步

from matplotlib import pyplot as plt

f, ax = plt.subplots()
print(type(ax))

返回

<class 'matplotlib.axes._subplots.AxesSubplot'>

并运行

from matplotlib import axes
print(type(axes._subplots))
print(type(axes._subplots.AxesSubplot))

产量

<class 'module'>
AttributeError: module 'matplotlib.axes._subplots' has no attribute 'AxesSubplots'

到目前为止,有效的类型提示解决方案如下:

def multi_rocker(
                 axy: type(plt.subplots()[1]), 
                 y_trues: np.ndarray,
                 y_preds: np.ndarray,
                 ):
  """
  One-Vs-All ROC-curve:
  """
  fpr = dict()
  tpr = dict()
  roc_auc = dict()
  n_classes = y_trues.shape[1]
  wanted = list(range(n_classes))
  for i,x in enumerate(wanted):
    fpr[i], tpr[i], _ = roc_curve(y_trues[:, i], y_preds[:, i])
    roc_auc[i] = round(auc(fpr[i], tpr[i]),2)
  extra = 0
  for i in range(n_classes):
    axy.plot(fpr[i], tpr[i],)
  return

它的问题是代码共享还不够清晰

标签: pythonmatplotlib

解决方案


上下文管理器的类型提示中所述:

import matplotlib.pyplot as plt

def plot_func(ax: plt.Axes):
    ...

推荐阅读