简体   繁体   English

如何在python3中键入提示matplotlib.axes._subplots.AxesSubplots对象

[英]How to type-hint a matplotlib.axes._subplots.AxesSubplots object in python3

I was wondering how is the "best" way to type-hint the axis-object of matplotlib-subplots.我想知道如何键入提示 matplotlib-subplots 的轴对象的“最佳”方法。

running跑步

from matplotlib import pyplot as plt

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

returns返回

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

and running和跑步

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

yields产量

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

So far a solution for type-hinting that works is as follows:到目前为止,有效的类型提示解决方案如下:

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

And the problem with it is that it isn't clear enough for code-sharing它的问题在于它对于代码共享还不够清晰

As described in Type hints for context manager :上下文管理器的类型提示中所述:

import matplotlib.pyplot as plt

def plot_func(ax: plt.Axes):
    ...
import matplotlib.axes as mpl_axes

def multi_rocker(
                 axy: mpl_axes.Axes, 
                 y_trues: np.ndarray,
                 y_preds: np.ndarray,
                 ):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM