我想知道如何键入提示 matplotlib-subplots 的轴对象的“最佳”方法。
跑步
from matplotlib import pyplot as plt
f, ax = plt.subplots()
print(type(ax))
Run Code Online (Sandbox Code Playgroud)
返回
<class 'matplotlib.axes._subplots.AxesSubplot'>
Run Code Online (Sandbox Code Playgroud)
和跑步
from matplotlib import axes
print(type(axes._subplots))
print(type(axes._subplots.AxesSubplot))
Run Code Online (Sandbox Code Playgroud)
产量
<class 'module'>
AttributeError: module 'matplotlib.axes._subplots' has no attribute 'AxesSubplots'
Run Code Online (Sandbox Code Playgroud)
到目前为止,有效的类型提示解决方案如下:
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
Run Code Online (Sandbox Code Playgroud)
它的问题在于它对于代码共享还不够清晰
fel*_*ice 31
如上下文管理器的类型提示中所述:
import matplotlib.pyplot as plt
def plot_func(ax: plt.Axes):
...
Run Code Online (Sandbox Code Playgroud)