python - 如何在 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
它的问题是代码共享还不够清晰
解决方案
如上下文管理器的类型提示中所述:
import matplotlib.pyplot as plt
def plot_func(ax: plt.Axes):
...
推荐阅读
- javascript - 从 ReactJS 中的 API 获取数据后如何重新渲染前端
- c - cs50 恢复,恢复的图像不匹配
- reactjs - 延迟加载 ReactJS 组件
- amazon-web-services - AWS IoT Core 中是否有任何规定可以获取以前的影子状态?是否有任何 API / 配置历史?
- sql - 每天都有窗口函数的 SQL 性能问题
- python - 正则表达式从python中的文本中识别固定字符字母数字词
- c - 如何在 C 项目中获取状态进程?
- git - 如何仅提交与现有 github 远程存储库不同的文件?
- python - 单线制造轮子失败
- python - Skimage.io.read 读取一个 PIL 对象